https://gcc.gnu.org/g:3a7fcf4f54ecffdbad03787d4f734c1fb2291ef5

commit r16-2372-g3a7fcf4f54ecffdbad03787d4f734c1fb2291ef5
Author: Andre Vehreschild <ve...@gcc.gnu.org>
Date:   Fri Jun 27 11:42:54 2025 +0200

    Fortran: Allow for iterator substitution in array constructors [PR119106]
    
            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.

Diff:
---
 gcc/fortran/expr.cc                                |  5 +++--
 gcc/testsuite/gfortran.dg/array_constructor_58.f90 | 17 +++++++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/gcc/fortran/expr.cc b/gcc/fortran/expr.cc
index b0495b7733ee..b8d04ff6f365 100644
--- a/gcc/fortran/expr.cc
+++ b/gcc/fortran/expr.cc
@@ -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
index 000000000000..1473be0da015
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/array_constructor_58.f90
@@ -0,0 +1,17 @@
+!{ dg-do run }
+
+! Contributed by  Federico Perini <federico.per...@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
+

Reply via email to