https://gcc.gnu.org/g:3a92ef9f6e91b5493838feee44ba1f01065b119a

commit r17-2375-g3a92ef9f6e91b5493838feee44ba1f01065b119a
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.

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 e098bd17725f..b79db02a30cc 100644
--- a/gcc/fortran/module.cc
+++ b/gcc/fortran/module.cc
@@ -6054,13 +6054,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

Reply via email to