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
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)
--- /dev/null
+! { dg-do compile }
+!
+! PR fortran/122046
+! The check for illegal recursion used to trigger on assertion when resolving
+! the array spec of the dummy argument in the submodule
+!
+! Contributed by Tomáš Trnka <trnka@scm.com>
+
+module ChemicalSystemModule
+
+ implicit none
+ private
+
+ type, public :: ChemicalSystemType
+ contains
+ procedure, public :: NumAtoms
+ end type
+
+contains
+
+ elemental integer function NumAtoms(self)
+ class(ChemicalSystemType), intent(in) :: self
+
+ NumAtoms = 123
+
+ end function
+
+end module
+
+module ChemicalSystemUtilsModule
+
+ use ChemicalSystemModule
+
+ implicit none
+ private
+
+ public :: ChemicalSystemRMSD
+
+ interface
+
+ module subroutine ChemicalSystemRMSD(modelSys, rmsdGrad)
+ type(ChemicalSystemType), intent(in) :: modelSys
+ real , intent(out) :: rmsdGrad(3,modelSys%NumAtoms())
+ end subroutine
+
+ end interface
+
+end module
+
+submodule(ChemicalSystemUtilsModule) ChemicalSystemUtilsSubModule
+ use ChemicalSystemModule
+
+ implicit none
+
+contains
+
+ module subroutine ChemicalSystemRMSD(modelSys, rmsdGrad)
+ type(ChemicalSystemType), intent(in) :: modelSys
+ real , intent(out) :: rmsdGrad(3,modelSys%NumAtoms())
+ end subroutine
+
+end submodule
+