https://gcc.gnu.org/g:54d98be48041fc47501ab1d9c127c98b6ff1d55d

commit r14-12900-g54d98be48041fc47501ab1d9c127c98b6ff1d55d
Author: Jerry DeLisle <[email protected]>
Date:   Tue Sep 29 10:04:39 2026 -0700

    fortran: [PR127187] Type parameter inquiry selector is not a variable
    
    A selector that is a LEN or KIND type parameter inquiry, e.g. s%len, was
    treated as a variable, so the associate-name pointed to the string length
    of s and followed later changes of it.  On targets where the charlen type
    is default INTEGER, associate_90.f90 failed with ERROR STOP 8.
    
    Assisted-by: Claude Opus 5.5
    
            PR fortran/127187
    
    gcc/fortran/ChangeLog:
    
            * resolve.cc (resolve_assoc_var): A selector with a LEN or KIND
            inquiry reference is not a variable.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/associate_90.f90: Add -fdump-tree-original and check
            that no associate-name points to a string length.
    
    (cherry picked from commit ea52b53ad8fe0025f973a70e32d32a6e9d342ca6)

Diff:
---
 gcc/fortran/resolve.cc                     | 10 ++++++++++
 gcc/testsuite/gfortran.dg/associate_90.f90 |  2 ++
 2 files changed, 12 insertions(+)

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 14bf2dcf9ecd..29eb137632bc 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -9605,6 +9605,16 @@ resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
                           && !gfc_has_vector_subscript (target))
                          || gfc_is_ptr_fcn (target));
 
+  /* A type parameter inquiry is not a variable.  */
+  if (sym->assoc->variable && target->expr_type == EXPR_VARIABLE)
+    for (gfc_ref *ref = target->ref; ref; ref = ref->next)
+      if (ref->type == REF_INQUIRY
+         && (ref->u.i == INQUIRY_LEN || ref->u.i == INQUIRY_KIND))
+       {
+         sym->assoc->variable = false;
+         break;
+       }
+
   /* Finally resolve if this is an array or not.  */
   if (target->expr_type == EXPR_FUNCTION && target->rank == 0
       && (sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED))
diff --git a/gcc/testsuite/gfortran.dg/associate_90.f90 
b/gcc/testsuite/gfortran.dg/associate_90.f90
index 4f7a768efb21..b62fdb9d603f 100644
--- a/gcc/testsuite/gfortran.dg/associate_90.f90
+++ b/gcc/testsuite/gfortran.dg/associate_90.f90
@@ -1,4 +1,5 @@
 ! { dg-do run }
+! { dg-options "-fdump-tree-original" }
 !
 ! PR 127187
 ! %LEN and %KIND inquiries of deferred-length character in ASSOCIATE.
@@ -68,3 +69,4 @@ program assoc_len_kind
   end associate
   deallocate (p)
 end program
+! { dg-final { scan-tree-dump-not "n = &" "original" } }

Reply via email to