]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: Allow for iterator substitution in array constructors [PR119106]
authorAndre Vehreschild <vehre@gcc.gnu.org>
Fri, 27 Jun 2025 09:42:54 +0000 (11:42 +0200)
committerAndre Vehreschild <vehre@gcc.gnu.org>
Mon, 28 Jul 2025 14:57:52 +0000 (16:57 +0200)
PR fortran/119106

gcc/fortran/ChangeLog:

* expr.cc (simplify_constructor): Do not simplify constants.
(gfc_simplify_expr): Continue to simplify expression when an
iterator is present.

gcc/testsuite/ChangeLog:

* gfortran.dg/array_constructor_58.f90: New test.

(cherry picked from commit 3a7fcf4f54ecffdbad03787d4f734c1fb2291ef5)

gcc/fortran/expr.cc
gcc/testsuite/gfortran.dg/array_constructor_58.f90 [new file with mode: 0644]

index 95ea055b8af79a5c9b9eed41fe4714a9e6c5299e..aea77260859c1275923a70c96a747e90b270743e 100644 (file)
@@ -1372,7 +1372,7 @@ simplify_constructor (gfc_constructor_base base, int type)
              || !gfc_simplify_expr (c->iterator->step, type)))
        return false;
 
-      if (c->expr)
+      if (c->expr && c->expr->expr_type != EXPR_CONSTANT)
        {
          /* Try and simplify a copy.  Replace the original if successful
             but keep going through the constructor at all costs.  Not
@@ -2469,7 +2469,8 @@ gfc_simplify_expr (gfc_expr *p, int type)
        {
          if (!simplify_parameter_variable (p, type))
            return false;
-         break;
+         if (!iter_stack)
+           break;
        }
 
       if (type == 1)
diff --git a/gcc/testsuite/gfortran.dg/array_constructor_58.f90 b/gcc/testsuite/gfortran.dg/array_constructor_58.f90
new file mode 100644 (file)
index 0000000..1473be0
--- /dev/null
@@ -0,0 +1,17 @@
+!{ dg-do run }
+
+! Contributed by  Federico Perini <federico.perini@gmail.com>
+! Check that PR fortran/119106 is fixed.
+
+program char_param_array
+implicit none
+character, parameter :: p(5) = ['1','2','3','4','5']
+character, save      :: n(5) = ['1','2','3','4','5']
+integer :: i(10), j
+
+i = 4
+if (any([(n(i(j)),j=1,10)] /= '4')) stop 1 ! OK
+if (any([(p(i(j)),j=1,10)] /= '4')) stop 2 ! used to runtime out-of-bounds error
+
+end program char_param_array
+