[flang-commits] [flang] [Flang][Semantics] Diagnose missing MODULE prefix in submodule procedures (PR #197173)
Eugene Epshteyn via flang-commits
flang-commits at lists.llvm.org
Thu Sep 3 07:04:08 PDT 2026
================
@@ -5976,6 +5976,26 @@ const Symbol *SubprogramVisitor::CheckExtantProc(
Symbol *SubprogramVisitor::PushSubprogramScope(const parser::Name &name,
Symbol::Flag subpFlag, const parser::LanguageBindingSpec *bindingSpec,
bool hasModulePrefix) {
+ if (!inInterfaceBlock() && currScope().IsSubmodule() && !hasModulePrefix) {
+ const Scope &parent{currScope().parent()};
+ if (parent.IsModule() || parent.IsSubmodule()) {
+ if (const Symbol *host{parent.FindSymbol(name.source)}) {
+ const Symbol &hostUlt{host->GetUltimate()};
+ const auto *hostSubp{hostUlt.detailsIf<SubprogramDetails>()};
+ if (hostSubp && hostSubp->isInterface() &&
+ hostUlt.attrs().test(Attr::MODULE)) {
+ context().messages().Warn(/*isInModuleFile=*/InModuleFile(),
+ context().languageFeatures(), common::UsageWarning::Portability,
+ name.source,
+ "Subprogram '%s' in this submodule is missing the MODULE prefix "
+ "to implement the module procedure interface from its parent; "
+ "did you mean 'MODULE %s'?"_port_en_US,
+ name.source,
+ subpFlag == Symbol::Flag::Subroutine ? "SUBROUTINE" : "FUNCTION");
----------------
eugeneepshteyn wrote:
Consider the following case:
```fortran
module m2
interface
module subroutine sub()
end subroutine
end interface
end module
submodule (m2) s2
contains
integer function sub()
sub = 2
end function
end submodule
```
Here the interface declares `sub` as subroutine, but `sub` in a submodule is a function.
Compiling it results in the following message:
```
$ flang -fsyntax-only -pedantic ex-kind.f90
ex-kind.f90:10:20: portability: Subprogram 'sub' in this submodule is missing the MODULE prefix to implement the module procedure interface from its parent; did you mean 'MODULE FUNCTION'? [-Wportability]
```
This is not bad, but if user follows instructions and changes it to `module function`, it still cannot implement subroutine's interface. I'm wondering if we change the check to `subpFlag == hostSubp->isFunction() ? "FUNCTION" : "SUBROUTINE"`, maybe this error message would be more faithful to the reality.
This is not a blocking issue, just putting it out for your consideration.
https://github.com/llvm/llvm-project/pull/197173
More information about the flang-commits
mailing list