The attach patch fixes PR Fortran/91960.  This PR is another
one from Gerhard in 2019-10.01.  A patch has been lingering
in the PR since 2023-05-30.

The patch checks that an array constructor in a parameter
statement is in fact a constant expression.  I'll note
that the patch requires a special carve out to accommodate
the fix for Fortran/117070. 

2026-01-11  Steven G. Kargl  <[email protected]>

        PR Fortran/91960
        * resolve.cc (resolve_fl_parameter):  Check the righthand symbol
        is a constant expression.

2026-01-11  Steven G. Kargl  <[email protected]>

        PR Fortran/91960
        * gfortran.dg/pr69962.f90: Adjust testcase to ignore new error message.
        * gfortran.dg/pr91960_1.f90: New test.
        * gfortran.dg/pr91960_2.f90: Ditto.

-- 
Steve
diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 2e8ce074c24..8584153c068 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -17871,6 +17871,26 @@ 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, J undefined
+     This check catches the typo.  */
+  if (sym->attr.dimension
+      && sym->value && sym->value->expr_type == EXPR_ARRAY
+      && !gfc_is_constant_expr (sym->value))
+    {
+      /* PR fortran/117070 argues a nonconstant proc pointer can appear in
+	 the array constructor of a paramater.  I don't buy it, but... */
+      if (sym->value->ts.type == BT_DERIVED
+	  && sym->value->ts.u.derived
+	  && sym->value->ts.u.derived->attr.proc_pointer_comp)
+	return true;
+      gfc_error ("Expecting constant expression near %L", &sym->value->where);
+      return false;
+    }
+
   return true;
 }
 
diff --git a/gcc/testsuite/gfortran.dg/pr69962.f90 b/gcc/testsuite/gfortran.dg/pr69962.f90
index 2684398ee31..30aaedfdf98 100644
--- a/gcc/testsuite/gfortran.dg/pr69962.f90
+++ b/gcc/testsuite/gfortran.dg/pr69962.f90
@@ -4,3 +4,4 @@ program p
    character(3), parameter :: x(2) = ['abc', 'xyz']
    character(2), parameter :: y(2) = [x(2)(2:3), x(n)(1:2)] ! { dg-error "CHARACTER length must be a constant" }
 end
+! { dg-prune-output "Expecting constant expression" }
diff --git a/gcc/testsuite/gfortran.dg/pr91960_1.f90 b/gcc/testsuite/gfortran.dg/pr91960_1.f90
new file mode 100644
index 00000000000..90abcaf6934
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr91960_1.f90
@@ -0,0 +1,6 @@
+! { dg-do compile }
+module m
+   integer :: i, j
+   integer, parameter :: a(3) = [1, 2, 3]
+   integer, parameter :: b(3) = [(a(j), i=1,3)] ! { dg-error "Expecting constant expression" }
+end
diff --git a/gcc/testsuite/gfortran.dg/pr91960_2.f90 b/gcc/testsuite/gfortran.dg/pr91960_2.f90
new file mode 100644
index 00000000000..a7d41d9c417
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr91960_2.f90
@@ -0,0 +1,8 @@
+! { dg-do compile }
+module m
+  implicit none
+  integer :: i, j
+  integer, parameter :: a(3) = [1, 2, 3]
+  integer, parameter :: c    = a(j)
+  integer            :: d    = c       ! { dg-error "initialization expression at" }
+end

Reply via email to