[flang-commits] [flang] [Flang][Semantics] Fix incorrect merging of separate module procedure interfaces during USE association (PR #197173)

via flang-commits flang-commits at lists.llvm.org
Tue May 12 06:17:41 PDT 2026


https://github.com/ShashwathiNavada updated https://github.com/llvm/llvm-project/pull/197173

>From 8415b3c447406cb8306a91883405320cf5e1789a Mon Sep 17 00:00:00 2001
From: ShashwathiNavada <shashwathinavada at gmail.com>
Date: Tue, 12 May 2026 07:27:05 -0500
Subject: [PATCH 1/2] [Flang][Semantics] Fix incorrect merging of separate
 module procedure interfaces during USE association

---
 flang/lib/Semantics/resolve-names.cpp | 36 ++++++++++++++++++++++---
 flang/test/Semantics/resolve128.f90   | 39 +++++++++++++++++++++++++++
 2 files changed, 71 insertions(+), 4 deletions(-)
 create mode 100644 flang/test/Semantics/resolve128.f90

diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index b6907cc792d76..102978816eab2 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -1511,9 +1511,9 @@ void AccVisitor::CopySymbolWithDevice(const parser::Name *name) {
   // attribute.
   if (context_.languageFeatures().IsEnabled(common::LanguageFeature::CUDA) &&
       name && name->symbol) {
-    if (Symbol * copy{currScope().CopySymbol(*name->symbol)}) {
+    if (Symbol * copy{currScope().CopySymbol(name->symbol->GetUltimate())}) {
       name->symbol = copy;
-      if (auto *object{copy->detailsIf<ObjectEntityDetails>()}) {
+      if (auto *object{copy->GetUltimate().detailsIf<ObjectEntityDetails>()}) {
         object->set_cudaDataAttr(common::CUDADataAttr::Device);
       }
     }
@@ -4107,7 +4107,27 @@ void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
         if (classification == ProcedureDefinitionClass::External) {
           const auto *subp1{p1.detailsIf<SubprogramDetails>()};
           const auto *subp2{p2.detailsIf<SubprogramDetails>()};
-          return subp1 && subp1->isInterface() && subp2 && subp2->isInterface();
+          if (subp1 && subp1->isInterface() && subp2 && subp2->isInterface()) {
+            // Don't allow merging when either module has a submodule
+            // that provides a body for this procedure.
+            auto hasSubmoduleBody{[](const Symbol &p) {
+              const Scope &owner{p.owner()};
+              if (!owner.IsModule()) {
+                return false;
+              }
+              for (const Scope &child : owner.children()) {
+                if (child.IsSubmodule()) {
+                  auto it{child.find(p.name())};
+                  if (it != child.end() &&
+                      IsProcedure((*it->second).GetUltimate())) {
+                    return true;
+                  }
+                }
+              }
+              return false;
+            }};
+            return !hasSubmoduleBody(p1) && !hasSubmoduleBody(p2);
+          }
         } else if (classification == ProcedureDefinitionClass::Module) {
           return AreSameModuleSymbol(p1, p2);
         }
@@ -4687,6 +4707,10 @@ bool SubprogramVisitor::HandleStmtFunction(const parser::StmtFunctionStmt &x) {
           "Name '%s' from host scope should have a type declaration before its local statement function definition"_port_en_US,
           name.source);
       MakeSymbol(name, Attrs{}, UnknownDetails{});
+      // 'name' may still point to a host-associated SubprogramNameDetails
+      // symbol. Reset it so statement-function processing
+      // re-resolves to the new local SubprogramDetails.
+      name.symbol = nullptr;
     } else if (auto *entity{ultimate.detailsIf<EntityDetails>()};
                entity && !ultimate.has<ProcEntityDetails>()) {
       resultType = entity->type();
@@ -6012,7 +6036,11 @@ bool DeclarationVisitor::Pre(const parser::CUDAAttributesStmt &x) {
       if (!symbol) {
         symbol = &MakeSymbol(name, ObjectEntityDetails{});
       }
-      SetCUDADataAttr(name.source, *symbol, attr);
+      if (attr == common::CUDADataAttr::Value) {
+        SetExplicitAttr(*symbol, Attr::VALUE);
+      } else {
+        SetCUDADataAttr(name.source, *symbol, attr);
+      }
     }
   }
   return false;
diff --git a/flang/test/Semantics/resolve128.f90 b/flang/test/Semantics/resolve128.f90
new file mode 100644
index 0000000000000..8924ce265e29f
--- /dev/null
+++ b/flang/test/Semantics/resolve128.f90
@@ -0,0 +1,39 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+
+module submodules_03_one
+   integer :: one_i
+   interface
+      subroutine inside_one()
+      end subroutine
+   end interface
+ end module
+
+ submodule (submodules_03_one) submodules_03_sub_one
+ contains
+   subroutine inside_one()
+   one_i = 6
+   end subroutine
+ end submodule
+
+ module submodules_03_two
+   integer :: two_i
+   interface
+      subroutine inside_one()
+      end subroutine
+   end interface
+ end module
+
+ submodule (submodules_03_two) sub_one
+   contains
+   subroutine inside_one()
+   two_i = 6
+   end subroutine
+ end submodule
+
+ program p
+ use submodules_03_one
+ use submodules_03_two
+ !ERROR: Reference to 'inside_one' is ambiguous
+ call inside_one()
+ end program
+ 
\ No newline at end of file

>From 695e6ec058b710c5de6ea363e9c55a5f49ae3784 Mon Sep 17 00:00:00 2001
From: ShashwathiNavada <shashwathinavada at gmail.com>
Date: Tue, 12 May 2026 18:46:28 +0530
Subject: [PATCH 2/2] Rename resolve128.f90 to resolve130.f90

---
 flang/test/Semantics/{resolve128.f90 => resolve130.f90} | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
 rename flang/test/Semantics/{resolve128.f90 => resolve130.f90} (99%)

diff --git a/flang/test/Semantics/resolve128.f90 b/flang/test/Semantics/resolve130.f90
similarity index 99%
rename from flang/test/Semantics/resolve128.f90
rename to flang/test/Semantics/resolve130.f90
index 8924ce265e29f..e3d3165748a9b 100644
--- a/flang/test/Semantics/resolve128.f90
+++ b/flang/test/Semantics/resolve130.f90
@@ -36,4 +36,4 @@ program p
  !ERROR: Reference to 'inside_one' is ambiguous
  call inside_one()
  end program
- 
\ No newline at end of file
+ 



More information about the flang-commits mailing list