]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: Fix assumed length chars and len inquiry [PR103716]
authorPaul Thomas <pault@gcc.gnu.org>
Tue, 23 May 2023 05:46:37 +0000 (06:46 +0100)
committerHarald Anlauf <anlauf@gmx.de>
Fri, 26 Apr 2024 19:15:46 +0000 (21:15 +0200)
2023-05-23  Paul Thomas  <pault@gcc.gnu.org>

gcc/fortran
PR fortran/103716
* resolve.cc (gfc_resolve_ref): Conversion of array_ref into an
element should be done for all characters without a len expr,
not just deferred lens, and for integer expressions.
* trans-expr.cc (conv_inquiry): For len and kind inquiry refs,
set the se string_length to NULL_TREE.

gcc/testsuite/
PR fortran/103716
* gfortran.dg/pr103716.f90 : New test.

(cherry picked from commit 842a432b02238361ecc601d301ac400a7f30f4fa)

gcc/fortran/resolve.cc
gcc/fortran/trans-expr.cc
gcc/testsuite/gfortran.dg/pr103716.f90 [new file with mode: 0644]

index 9264322f671855194bf4e00d67c59f852a1939b7..6a7325e15e722e2140384b21f2ae7c9c13e2e6f1 100644 (file)
@@ -5461,7 +5461,9 @@ gfc_resolve_ref (gfc_expr *expr)
        case REF_INQUIRY:
          /* Implement requirement in note 9.7 of F2018 that the result of the
             LEN inquiry be a scalar.  */
-         if (ref->u.i == INQUIRY_LEN && array_ref && expr->ts.deferred)
+         if (ref->u.i == INQUIRY_LEN && array_ref
+             && ((expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->length)
+                 || expr->ts.type == BT_INTEGER))
            {
              array_ref->u.ar.type = AR_ELEMENT;
              expr->rank = 0;
index 11ee1931b8eac773d56e2c816539cc0792113a06..e78a01003c9cbf0fab4c4a9eec6892137b7b2bfa 100644 (file)
@@ -2854,11 +2854,13 @@ conv_inquiry (gfc_se * se, gfc_ref * ref, gfc_expr *expr, gfc_typespec *ts)
     case INQUIRY_KIND:
       res = build_int_cst (gfc_typenode_for_spec (&expr->ts),
                           ts->kind);
+      se->string_length = NULL_TREE;
       break;
 
     case INQUIRY_LEN:
       res = fold_convert (gfc_typenode_for_spec (&expr->ts),
                          se->string_length);
+      se->string_length = NULL_TREE;
       break;
 
     default:
diff --git a/gcc/testsuite/gfortran.dg/pr103716.f90 b/gcc/testsuite/gfortran.dg/pr103716.f90
new file mode 100644 (file)
index 0000000..4f78900
--- /dev/null
@@ -0,0 +1,15 @@
+! { dg-do compile }
+!
+! The gimplifier used to throw a fit on thes two functions.
+!
+! Contributed by Gerhard Steinmetz  <gscfq@t-online.de>
+!
+function f1(x)
+   character(*) :: x(*)
+   print *, g(x%len)
+end
+
+function f2(x)
+   character(*) :: x(3)
+   print *, g(x%len)
+end