I plan to commit the attached patch shortly and backport in a days.

This fixes a regression caused by my previous commit for this PR which only showed on certain systems with -m32. See the PR for a reproducer I found on my system here.

Regression tested on x86_64 and manually tested using -fdefault-integer-8.

Best regards,

Jerry

---


ortran: [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:

        PR fortran/127187
        * resolve.cc (resolve_assoc_var): A selector with a LEN or KIND
        inquiry reference is not a variable.

gcc/testsuite/ChangeLog:

        PR fortran/127187
        * gfortran.dg/associate_90.f90: Add -fdump-tree-original and check
        that no associate-name points to a string length.
---

From b31b67a57406ed190ca7a9e382041e23f5d36910 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Tue, 29 Sep 2026 10:04:39 -0700
Subject: [PATCH] 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:

	PR fortran/127187
	* resolve.cc (resolve_assoc_var): A selector with a LEN or KIND
	inquiry reference is not a variable.

gcc/testsuite/ChangeLog:

	PR fortran/127187
	* gfortran.dg/associate_90.f90: Add -fdump-tree-original and check
	that no associate-name points to a string length.
---
 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 7ad9eeb2db5..dd9ae1817e4 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -10844,6 +10844,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 2190aa83693..5d071e57c16 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.
@@ -67,3 +68,4 @@ program assoc_len_kind
   end associate
   deallocate (p)
 end program
+! { dg-final { scan-tree-dump-not "n = &" "original" } }
-- 
2.55.0

Reply via email to