Bug 43990 - [OOP] ICE in output_constructor_regular_field, at varasm.c:4995
Summary: [OOP] ICE in output_constructor_regular_field, at varasm.c:4995
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: ---
Assignee: janus
URL:
Keywords: ice-on-valid-code
Depends on:
Blocks:
 
Reported: 2010-05-05 09:26 UTC by janus
Modified: 2010-05-17 20:05 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-05-16 22:11:03


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description janus 2010-05-05 09:26:03 UTC
module m

  type :: t
     logical :: l = .true.
     class(t),pointer :: cp => null()
  end type

  type(t),save :: default_t

end module


Compiling this currently results in:

f951: internal compiler error: in output_constructor_regular_field, at varasm.c:4995


Reported by Hans-Werner Boschmann.
Comment 1 Tobias Burnus 2010-05-05 12:41:17 UTC
  if ((local->field != NULL_TREE || local->index != NULL_TREE)
      && fieldpos != local->total_bytes)
    {
      gcc_assert (fieldpos >= local->total_bytes);

Debugging shows:
  field != NULL_TREE, index == NULL_TREE
  fieldpos = 0 >= 4 = local->total_bytes


If one sets a break point at 5327, one gets 7 times:
  #0  output_constructor (exp=0x2aaaaac16200, size=32, align=256, outer=0x0) at gcc/gcc/varasm.c:5327
but then one gets
  Breakpoint 1, output_constructor (exp=0x2aaaaac16160, size=24, align=128, outer=0x0) at gcc/gcc/varasm.c:5327
which is the case with the ICE; only in the latter case there is field != NULL_TREE. The variable has then the following value - and in particular offset == 0 and thus also fieldpos == 0.

 <field_decl 0x2aaaaac2c098 l
    type <boolean_type 0x2aaaaab58b28 logical(kind=4) public unsigned SI
        size <integer_cst 0x2aaaaab36960 constant 32>
        unit size <integer_cst 0x2aaaaab36668 constant 4>
        align 32 symtab 0 alias set -1 canonical type 0x2aaaaab58b28 precision 1 min <integer_cst 0x2aaaaab553c0 0> max <integer_cst 0x2aaaaab55410 1>>
    unsigned SI file test.f90 line 4 col 0 size <integer_cst 0x2aaaaab36960 32> unit size <integer_cst 0x2aaaaab36668 4>
    align 32 offset_align 128
    offset <integer_cst 0x2aaaaab36690 type <integer_type 0x2aaaaab45000> constant 0>
    bit offset <integer_cst 0x2aaaaab36d98 type <integer_type 0x2aaaaab450a8 bit_size_type> constant 0> context <record_type 0x2aaaaac28498 t> chain <field_decl 0x2aaaaac2c130 cp>>

(gdb) call debug_tree(exp)
 <constructor 0x2aaaaac16160
    type <record_type 0x2aaaaac28498 t BLK
        size <integer_cst 0x2aaaaab55190 constant 192>
        unit size <integer_cst 0x2aaaaab55140 constant 24>
        align 64 symtab 0 alias set -1 canonical type 0x2aaaaac28498
        fields <field_decl 0x2aaaaac2c098 l type <boolean_type 0x2aaaaab58b28 logical(kind=4)>
            unsigned SI file test.f90 line 4 col 0
            size <integer_cst 0x2aaaaab36960 constant 32>
            unit size <integer_cst 0x2aaaaab36668 constant 4>
            align 32 offset_align 128
            offset <integer_cst 0x2aaaaab36690 constant 0>
            bit offset <integer_cst 0x2aaaaab36d98 constant 0> context <record_type 0x2aaaaac28498 t> chain <field_decl 0x2aaaaac2c130 cp>>
        pointer_to_this <pointer_type 0x2aaaaac28738> chain <type_decl 0x2aaaaab5f730 D.1544>>
    constant lngt 2 idx <field_decl 0x2aaaaac2c098 l>
    val <integer_cst 0x2aaaaab55410 type <boolean_type 0x2aaaaab58b28 logical(kind=4)> constant 1>
    idx <field_decl 0x2aaaaac24ed8 $data
        type <pointer_type 0x2aaaaac28738 type <record_type 0x2aaaaac28498 t>
            public unsigned DI
            size <integer_cst 0x2aaaaab36a28 constant 64>
            unit size <integer_cst 0x2aaaaab36a50 constant 8>
            align 64 symtab 0 alias set -1 canonical type 0x2aaaaac28738>
        unsigned DI file test.f90 line 5 col 0 size <integer_cst 0x2aaaaab36a28 64> unit size <integer_cst 0x2aaaaab36a50 8>
        align 64 offset_align 128 offset <integer_cst 0x2aaaaab36690 0> bit offset <integer_cst 0x2aaaaab36d98 0> context <record_type 0x2aaaaac28540 .class.t.p>
        chain <field_decl 0x2aaaaac2c000 $vptr type <pointer_type 0x2aaaaac28690>
            unsigned DI file test.f90 line 5 col 0 size <integer_cst 0x2aaaaab36a28 64> unit size <integer_cst 0x2aaaaab36a50 8>
            align 64 offset_align 128 offset <integer_cst 0x2aaaaab36690 0> bit offset <integer_cst 0x2aaaaab36a28 64> context <record_type 0x2aaaaac28540 .class.t.p>>>
    val <integer_cst 0x2aaaaac27910 type <pointer_type 0x2aaaaac28738> constant 0>>
Comment 2 Tobias Burnus 2010-05-05 14:12:54 UTC
The problem seems to be the initialization at gfc_conv_structure (init = 1) in trans-expr.c:

We have:
     logical :: l = .true.
     class(t),pointer :: cp => null()
and thus:

(gdb) p expr->ts.u.derived->components->name
$12 = 0x2aaaaab41ea8 "l"
(gdb) p expr->ts.u.derived->components->next->name
$13 = 0x2aaaaab41eb8 "cp"
(gdb) p expr->ts.u.derived->components->next->ts.type
$14 = BT_CLASS
(gdb) p expr->ts.u.derived->components->next->attr.pointer
$15 = 0

The later is simply not true: We have a pointer.

      if (cm->ts.type == BT_CLASS && !cm->attr.proc_pointer)
	{
	  gfc_component *data;
	  data = gfc_find_component (cm->ts.u.derived, "$data", true, true);

Somehow this gives the wrong result in terms of the offset, i.e. one tries to initialize:

t.l at position (&t + (0 byte)) and then one tries to initialize t.$data (!) at (&t + 0)  but it should be (&t + 4) - or (&(t.class) + 0).

The following patch fixes the problem, though I have the feeling it papers over some logic problem (cf. below):

--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -4328,10 +4328,9 @@ gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
        c; c = gfc_constructor_next (c), cm = cm->next)
     {
       /* Skip absent members in default initializers and allocatable
-        components.  Although the latter have a default initializer
-        of EXPR_NULL,... by default, the static nullify is not needed
-        since this is done every time we come into scope.  */
-      if (!c->expr || cm->attr.allocatable)
+        components, and NULL initialization of pointers, which are
+        automatically handled through zero initialization.  */
+      if (!c->expr || cm->attr.allocatable || c->expr->expr_type == EXPR_NULL)
         continue;

       if (cm->ts.type == BT_CLASS && !cm->attr.proc_pointer)

(By the way, the comment above looked bogus: "since this is done every time we come in scope" is only true if the variable is STATIC just due to -fmax-stack-var-size. Otherwise, it will be ("only") zero initialized through "= {}".)

 * * *

The "fix" above is likely to break if one uses
  <type>, pointer :: ptr => nonNULLtarget
(allowed since Fortran 2008) where the RHS can be something different than EXPR_NULL. The problem seems to be that already the class struct is a pointer and not only its content, i.e. "t.class.$data = NULL" vs. "t.class == NULL". Or maybe only the reference goes wrong; cf. t.class.$data (OK) vs. t.$data (wrong).


F2008 has:

R442 component-initialization is = constant-expr
                              or => null-init
                              or => initial-data-target
R443 initial-data-target is designator
C461 (R443) The designator shall designate a nonallocatable variable that has the TARGET and SAVE attributes and does not have a vector subscript. Every subscript, section subscript, substring starting point, and substring ending point in designator shall be a constant expression."

 * * *

Failing dump (for PROGRAM - not shown for MODULE):
  static struct t default_t = {.l=1, .$data=0B};
working with the patch above:
  static struct t default_t = {.l=1};
Comment 3 Tobias Burnus 2010-05-06 16:27:33 UTC
See also PR 43895 - the issue seems to be similar though one does not have an except that there one has no static-memory initialization. [I might also be completely off.]
Comment 4 Tobias Burnus 2010-05-06 16:37:26 UTC
(In reply to comment #3)
> See also PR 43895

As I feared there: There seems to be an inconsistency with CLASS with POINTER or ALLOCATABLE attribute: Is "class.$DATA" or "class" the pointer variable. If one adds "b = ALLOCATED(x)" one finds:

  x.a.$data = 0B;   ! Default initialization sets class.$data to NULL
  D.1577 = &x->a;   ! ALLOCATED check looks at "(class != NULL)"
  b = D.1577 != 0B;

Which does not make sense.  [This might be unrelated to the ICE.]
Comment 5 janus 2010-05-16 22:11:03 UTC
The ICE is fixed by removing this unneeded (and buggy) piece of code:

Index: gcc/fortran/trans-expr.c
===================================================================
--- gcc/fortran/trans-expr.c	(revision 159444)
+++ gcc/fortran/trans-expr.c	(working copy)
@@ -4337,21 +4337,8 @@ gfc_conv_structure (gfc_se * se, gfc_expr * expr,
       if (!c->expr || cm->attr.allocatable)
         continue;
 
-      if (cm->ts.type == BT_CLASS && !cm->attr.proc_pointer)
+      if (strcmp (cm->name, "$size") == 0)
 	{
-	  gfc_component *data;
-	  data = gfc_find_component (cm->ts.u.derived, "$data", true, true);
-	  if (!data->backend_decl)
-	    gfc_get_derived_type (cm->ts.u.derived);
-	  val = gfc_conv_initializer (c->expr, &cm->ts,
-				      TREE_TYPE (data->backend_decl),
-				      data->attr.dimension,
-				      data->attr.pointer);
-
-	  CONSTRUCTOR_APPEND_ELT (v, data->backend_decl, val);
-	}
-      else if (strcmp (cm->name, "$size") == 0)
-	{
 	  val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
 	  CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
 	}
Comment 6 Dominique d'Humieres 2010-05-17 17:09:25 UTC
The patch in comment #5 fixes the ICE without side effects (AFAICT!-), but it does not fix PR43895 (see comments #3 and #4).
Comment 7 janus 2010-05-17 17:41:06 UTC
(In reply to comment #6)
> but it does not fix PR43895 (see comments #3 and #4).

... which I take as an indication that the two are indeed not so much related.
Comment 8 janus 2010-05-17 19:59:13 UTC
Subject: Bug 43990

Author: janus
Date: Mon May 17 19:58:48 2010
New Revision: 159511

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=159511
Log:
2010-05-17  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/43990
	* trans-expr.c (gfc_conv_structure): Remove unneeded and buggy code.
	This is now handled via 'gfc_class_null_initializer'.


2010-05-17  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/43990
	* gfortran.dg/class_21.f03: New.

Added:
    trunk/gcc/testsuite/gfortran.dg/class_21.f03
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/testsuite/ChangeLog

Comment 9 janus 2010-05-17 20:05:19 UTC
Fixed with r159511. Closing.