https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93365

markeggleston at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |markeggleston at gcc 
dot gnu.org

--- Comment #5 from markeggleston at gcc dot gnu.org ---
(In reply to Steve Kargl from comment #4)
> On Thu, Jan 23, 2020 at 03:37:04PM +0000, markeggleston at gcc dot gnu.org
> wrote:
> > 
> > If the parameter attribute is removed or a non zero sized array is used, the
> > ICE does not occur.
> > 
> > The ICE occurs in gfc_match_varspec (primary.c) a temporary gfc_ref 
> > structure
> > has been created and has the type REF_INQUIRY. If the primary expression has
> > been passed in is EXPR_CONSTANT the check is done. When the ICE occurs the 
> > type
> > is EXPR_STRUCTURE, so the value of the REF_INQUIRY taken from u.i is used 
> > for
> > the inquiry type instead of being 2 (INQUIRY_KIND) it is random 
> > e.g.73176720.
> > 
> 
> Not quite, right.  If you have
> 
>    complex, parameter :: a(1) = 0
>    integer b
>    data b/a%kind%
> 
> then at line 2337 in primary.c
> 
>    gfc_simplify_expr (primary, 0);
> 
> collapses primary to an EXPR_CONSTANT, and then things seem to work.
> 
> If you have
> 
>    complex, parameter :: a(0) = 0
>    integer b
>    data b/a%kind%
> 
> then "gfc_simplify_expr (primary, 0)" does not collapse primary
> to EXPR_CONSTANT as it seems gfc_simplify_expr does not handle
> zero size arrays (as there is nothing to simplify!).  gfortran

gfc_simplify_expr calls simplify_parameter_variable which for zero size
arrays it creates a new gfc_expr structure with the expr_type set to EXPR_ARRAY
and replaces primary. When primary is replaced its chain of ref structures is
deleted so tmp->u.i is invalid causing the ICE the following lines are never
reached:

> then enters the switch statement and get to lines 2384-2385
> 
>        primary->ts.type = BT_INTEGER;
>        primary->ts.kind = gfc_default_integer_kind;
> 
> This just resets a part of primary, but it does not fix up
> it up correctly.  Likely, need to check for zero size
> arrays do extra work.

I have modified gfc_simplify_expr:

--- a/gcc/fortran/expr.c
+++ b/gcc/fortran/expr.c
@@ -2069,8 +2069,13 @@ simplify_parameter_variable (gfc_expr *p, int type)
       e->value.constructor = NULL;
       e->shape = gfc_copy_shape (p->shape, p->rank);
       e->where = p->where;
-      gfc_replace_expr (p, e);
-      return true;
+      e->ref = gfc_copy_ref (p->ref);
+      t = gfc_simplify_expr (e, type);
+      if (t && !e->ref)
+       gfc_replace_expr (p, e);
+      else
+       gfc_free_expr (e);
+      return t;
     }

   e = gfc_copy_expr (p->symtree->n.sym->value);

This results in primary being reduced to a EXPR_CONSTANT just like it is for
non-zero length arrays.

I've checked the 8 and 9 branches, 8 does not support this syntax and 9.2
behaves differently:

program p
   real, parameter :: a(0) = 0.5
   write(*,*) a%kind
end

compiles but the output is:
  0.500000000    

and 

program p
   real, parameter :: a(1) = 0.5
   write(*,*) a%kind
end

fails to compile with an ICE. I'll investigate these further and ad either 1 or
2 new PRs.

I'm now getting failures for several test cases in gfortran.dg, more work to
do...

Reply via email to