[gcc r16-9277] fortran: [PR126234] Fix regression in SPEC benchmarks.

Jerry DeLisle via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:b02b1bb88c08588f10051aee509c026212b29821

commit r16-9277-gb02b1bb88c08588f10051aee509c026212b29821
Author: Jerry DeLisle <[email protected]>
Date:   Mon Jul 13 11:26:13 2026 -0700

    fortran: [PR126234] Fix regression in SPEC benchmarks.
    
    The fix for pr126170 broke one of the SPEC tests. The orginal fix
    used gfc_find_symbol which had a side effect of causing the error.
    The gfc_find_symbol calls gfc_find_sym_tree which then calls
    ambiguous_symbol where the error is issued.
    
            PR fortran/126234
    
    gcc/fortran/ChangeLog:
    
            * module.cc (read_module): Walk the symtree instead of
            using gfc_find_symbol.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/pr126234.f90: New test.
    
    (cherry picked from commit 3a92ef9f6e91b5493838feee44ba1f01065b119a)

Diff:
---
 gcc/fortran/module.cc                  | 15 ++++++++++++---
 gcc/testsuite/gfortran.dg/pr126234.f90 | 26 ++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/gcc/fortran/module.cc b/gcc/fortran/module.cc
index 7206048c407f..b728c63c7cc2 100644
--- a/gcc/fortran/module.cc
+++ b/gcc/fortran/module.cc
@@ -5882,13 +5882,22 @@ read_module (void)
 	    continue;
 
 	  /* Skip re-importing a derived type already visible via host
-	     association from the same module.  */
+	     association from the same module.  Walk the symtree since
+	     using gfc_find_symbol can give a wrong error.  */
 	  if (!only_flag && !info->u.rsym.renamed
 		&& strcmp (name, module_name) != 0
 		&& gfc_current_ns->parent)
 	    {
-	      gfc_symbol *host_sym;
-	      gfc_find_symbol (name, gfc_current_ns, 1, &host_sym);
+	      gfc_symbol *host_sym = NULL;
+	      for (gfc_namespace *pns = gfc_current_ns; pns; pns = pns->parent)
+		{
+		  gfc_symtree *host_st = gfc_find_symtree (pns->sym_root, name);
+		  if (host_st)
+		    {
+		      host_sym = host_st->n.sym;
+		      break;
+		    }
+		}
 	      if (host_sym && host_sym->attr.flavor == FL_DERIVED
 		  && host_sym->module
 		  && strcmp (host_sym->module, module_name) == 0)
diff --git a/gcc/testsuite/gfortran.dg/pr126234.f90 b/gcc/testsuite/gfortran.dg/pr126234.f90
new file mode 100644
index 000000000000..c7cf24884689
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr126234.f90
@@ -0,0 +1,26 @@
+! { dg-do compile }
+!
+! PR fortran/126234
+! Test case derived from the PR example
+module amod
+  real, parameter :: a1 = 1.0
+end module amod
+
+module bmod
+  real, parameter :: a1 = 2.0
+end module bmod
+
+module cmod
+  use amod
+end module cmod
+
+module mmod
+  use amod
+  use bmod
+contains
+  subroutine sub (x)
+    use cmod
+    real :: x
+    x = 1.0
+  end subroutine sub
+end module mmod
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.