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

--- Comment #8 from Steve Kargl <sgk at troutmask dot apl.washington.edu> ---
On Wed, Mar 10, 2021 at 10:59:39PM +0000, sgk at troutmask dot
apl.washington.edu wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99506
> 
> --- Comment #7 from Steve Kargl <sgk at troutmask dot apl.washington.edu> ---
> On Wed, Mar 10, 2021 at 10:22:45PM +0000, anlauf at gcc dot gnu.org wrote:
> > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99506
> > 
> > --- Comment #6 from anlauf at gcc dot gnu.org ---
> > (In reply to Richard Biener from comment #4)
> > > I don't know fortran enough for what 'parameter' means in this context:
> > > 
> > >    real(double),  parameter:: latt(jmax) = [(latt100(i)/100.d0, j=1,jmax)]
> > > 
> > > but the middle-end sees a readonly global (TREE_STATIC) variable.
> > 
> > There is a possibly related issue in pr91960.
> > 
> 
> Yes, same problem.  An uninitialized *variable* is used in
> an implied-do loop, which is an ac-value expression.  Perhaps,
> resolve.c(resolve_fl_parameter) needs to do some deeper 
> checking.
> 

This patch fixes both 99506 and 91960.  I cannot commit.
Whoever decides to commit the patch needs to convert the
code in 91960 into a testcase.  Otherwise, this patch 
will fester in bugzilla with other 20 or so patches I've
included in audit trails of those PRs.

diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index 2a91ae743ea..84e93dbc1fd 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -15179,6 +15186,20 @@ resolve_fl_parameter (gfc_symbol *sym)
       return false;
     }

+  /* Some programmers can have a typo when using an implied-do loop to 
+     initialize an array constant.  For example, 
+       INTEGER I,J
+       INTEGER, PARAMETER :: A(3) = [(I, I = 1, 3)]    ! OK
+       INTEGER, PARAMETER :: B(3) = [(A(J), I = 1, 3)]  ! Not OK
+     This check catches the typo.  */
+  if (sym->attr.dimension
+      && sym->value && sym->value->expr_type == EXPR_ARRAY
+      && !gfc_is_constant_expr (sym->value))
+    {
+      gfc_error ("Expecting constant expression near %L", &sym->value->where);
+      return false;
+    }
+
   return true;
 }

Reply via email to