Bug 37626 - ICE for automatic deallocation of character result variable
ICE for automatic deallocation of character result variable
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: fortran
4.3.1
: P3 normal
: ---
Assigned To: Tobias Burnus
: ice-on-valid-code
Depends on:
Blocks: 32834
  Show dependency treegraph
 
Reported: 2008-09-23 18:17 UTC by Rich Townsend
Modified: 2008-09-25 16:21 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2008-09-23 19:43:30


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Rich Townsend 2008-09-23 18:17:03 UTC
The sample code below produces the following internal compiler error:

unique_A.f90: In function 'unique_a':
unique_A.f90:42: internal compiler error: Segmentation fault

The error seems to be associated with use of the PACK() intrinsic.

--CUT HERE--
function unique_A (x, sorted) result (x_unique)

  character(*), dimension(:), intent(in)       :: x
  logical, intent(in), optional                :: sorted
  character(LEN(x)), dimension(:), allocatable :: x_unique

  logical                                      :: sorted_
  character(LEN(x)), dimension(SIZE(x))        :: x_sorted
  integer                                      :: n_x
  logical, dimension(SIZE(x))                  :: mask

  integer, external                            :: b3ss_index

! Set up sorted_

  if(PRESENT(sorted)) then
     sorted_ = sorted
  else
     sorted_ = .FALSE.
  endif

! If necessary, sort x
  
  if(sorted_) then
     x_sorted = x
  else
     x_sorted = x(b3ss_index(x))
  endif

! Set up the unique array

  n_x = SIZE(x)

  mask = (/.TRUE.,x_sorted(2:n_x) /= x_sorted(1:n_x-1)/)

  allocate(x_unique(COUNT(mask)))

  x_unique = PACK(x_sorted, MASK=mask)

! Finish

  return

end function unique_A
Comment 1 Dominique d'Humieres 2008-09-23 18:48:46 UTC
On ppc/intel Darwin9, I get

pr37626.f90: In function 'unique_a':
pr37626.f90:42: internal compiler error: in gfc_conv_descriptor_data_get, at fortran/trans-array.c:147

with 4.2.3, 4.3.2, and trunk.

Comment 2 Tobias Burnus 2008-09-23 19:43:30 UTC
It fails for the following assert:

gfc_conv_descriptor_data_get (tree desc)
{
  tree field, type, t;

  type = TREE_TYPE (desc);
  gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));


Reduced test case:

function unique_A (x) result (x_unique)
  implicit none
  character(*), dimension(:), intent(in)       :: x
  character(LEN(x)), dimension(:), allocatable :: x_unique
  character(LEN(x)), dimension(5)              :: x_sorted
  logical, dimension(4)                        :: mask
  mask = (/ x_sorted(2:5) /= x_sorted(1:4) /)
  allocate(x_unique(COUNT(mask)))
end function unique_A

If one one changes the program to "function x_unique(x)", i.e. sym == sym->result, it works. The same is true if the character length is explicitly set for x_unique (or for x, which implies the latter).

Debugging shows the following call tree for "x_unique":
  #0 gfc_conv_descriptor_data_get
  #1 gfc_trans_dealloc_allocated
  #2 gfc_trans_deferred_array
Thus the ICE happens for the automatic deallocation of "x_unique". But as x_unique is a result variable that should not happen!

Patch:

--- trans-array.c       (Revision 140615)
+++ trans-array.c       (Arbeitskopie)
@@ -5754,7 +5754,7 @@ gfc_trans_deferred_array (gfc_symbol * s
       gfc_add_expr_to_block (&fnblock, tmp);
     }

-  if (sym->attr.allocatable && !sym->attr.save)
+  if (sym->attr.allocatable && !sym->attr.save && !sym->attr.result)
     {
       tmp = gfc_trans_dealloc_allocated (sym->backend_decl);
       gfc_add_expr_to_block (&fnblock, tmp);
Comment 3 Tobias Burnus 2008-09-24 07:02:42 UTC
Subject: Bug 37626

Author: burnus
Date: Wed Sep 24 07:01:18 2008
New Revision: 140624

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=140624
Log:
2008-09-24  Tobias Burnus  <burnus@net-b.de>

        PR fortran/37626
        * trans-array.c (gfc_trans_deferred_array): Don't auto-deallocate
        result variables.

2008-09-24  Tobias Burnus  <burnus@net-b.de>

        PR fortran/37626
        * gfortran.dg/allocatable_function_4.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/allocatable_function_4.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/testsuite/ChangeLog

Comment 4 Tobias Burnus 2008-09-25 16:20:09 UTC
Subject: Bug 37626

Author: burnus
Date: Thu Sep 25 16:18:45 2008
New Revision: 140667

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=140667
Log:
2008-09-25  Tobias Burnus  <burnus@net-b.de>

        PR fortran/37626
        * trans-array.c (gfc_trans_deferred_array): Don't
        * auto-deallocate
        result variable.

2008-09-25  Tobias Burnus  <burnus@net-b.de>

        PR fortran/37626
        * gfortran.dg/allocatable_function_4.f90: New test.


Added:
    branches/gcc-4_3-branch/gcc/testsuite/gfortran.dg/allocatable_function_4.f90
Modified:
    branches/gcc-4_3-branch/gcc/fortran/ChangeLog
    branches/gcc-4_3-branch/gcc/fortran/trans-array.c
    branches/gcc-4_3-branch/gcc/testsuite/ChangeLog

Comment 5 Tobias Burnus 2008-09-25 16:21:18 UTC
FIXED on the trunk (4.4.0) and on the 4.3 branch.

Thanks for the bug report!