When substituting an array-valued character parameter variable, the call to
gfc_copy_expr returns character length 1.  Fix up the resulting length.

I could not figure out whether this is a bug or a feature of gfc_copy_expr.
But the fix to simplify_parameter_variable would not do any harm in any case.

Regtested on x86_64-pc-linux-gnu.  OK for mainline, and since this is a
bad code regression, backports down to 8-branch?

Thanks,
Harald


PR fortran/98017 - Suspected regression using PACK

When substituting a parameter variable of type character, the character
length was reset to 1.  Fix this by copying the length.

gcc/fortran/ChangeLog:

        * expr.c (simplify_parameter_variable): Fix up character length
        after copying an array-valued expression.

gcc/testsuite/ChangeLog:

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

diff --git a/gcc/fortran/expr.c b/gcc/fortran/expr.c
index 32d905ad179..8f1a3a34053 100644
--- a/gcc/fortran/expr.c
+++ b/gcc/fortran/expr.c
@@ -2096,6 +2096,10 @@ simplify_parameter_variable (gfc_expr *p, int type)
 	return false;

       e->rank = p->rank;
+
+      /* Fix up character length since gfc_copy_expr may not preserve it.  */
+      if (e->ts.type == BT_CHARACTER && p->ts.u.cl)
+	e->ts.u.cl = gfc_new_charlen (gfc_current_ns, p->ts.u.cl);
     }

   if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL)
diff --git a/gcc/testsuite/gfortran.dg/pr98017.f90 b/gcc/testsuite/gfortran.dg/pr98017.f90
new file mode 100644
index 00000000000..24d7adadb40
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr98017.f90
@@ -0,0 +1,9 @@
+! { dg-do run }
+! PR98017 - [8/9/10/11 Regression] Suspected regression using PACK
+
+program p
+  implicit none
+  character(*), parameter :: s(1) = ['abc()']
+  if (len (pack (s, s(:)(:1) =='a')) /= len (s)) stop 1
+  if (any (pack (s, s(:)(:1) =='a')  /=      s)) stop 2
+end

Reply via email to