https://gcc.gnu.org/g:5f80e4fe49c245933e646ef8229e53b7fb5c2ec1
commit 5f80e4fe49c245933e646ef8229e53b7fb5c2ec1 Author: Mikael Morin <mik...@gcc.gnu.org> Date: Wed Sep 24 13:33:16 2025 +0200 Correction pr122046 Diff: --- gcc/fortran/symbol.cc | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/gcc/fortran/symbol.cc b/gcc/fortran/symbol.cc index 81aa81df2eec..8211d926cf63 100644 --- a/gcc/fortran/symbol.cc +++ b/gcc/fortran/symbol.cc @@ -5500,7 +5500,16 @@ gfc_namespace * gfc_get_procedure_ns (gfc_symbol *sym) { if (sym->formal_ns - && sym->formal_ns->proc_name == sym) + && sym->formal_ns->proc_name == sym + /* For module procedures used in submodules, there are two namespaces. + The one generated by the host association of the module is directly + accessible through SYM->FORMAL_NS but doesn't have any parent set. + The one generated by the parser is only accessible by walking the + contained namespace but has its parent set. Prefer the one generated + by the parser below. */ + && !(sym->attr.used_in_submodule + && sym->attr.contained + && sym->formal_ns->parent == nullptr)) return sym->formal_ns; /* The above should have worked in most cases. If it hasn't, try some other @@ -5515,6 +5524,10 @@ gfc_get_procedure_ns (gfc_symbol *sym) if (ns->proc_name == sym) return ns; + if (sym->formal_ns + && sym->formal_ns->proc_name == sym) + return sym->formal_ns; + if (sym->formal) for (gfc_formal_arglist *f = sym->formal; f != nullptr; f = f->next) if (f->sym)