[flang-commits] [flang] [flang] Handle indirect USE of ancestor module into submodule (PR #124969)

via flang-commits flang-commits at lists.llvm.org
Wed Jan 29 10:42:08 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

<details>
<summary>Changes</summary>

A USE statement within a submodule (possibly in a nested scope) is not allowed to USE the submodule's ancestor module directly, but it is permissible to USE that ancestor module indirectly via another unrelated module.  Don't emit "already present in scope" errors for this case.

Fixes https://github.com/llvm/llvm-project/issues/124731.

---
Full diff: https://github.com/llvm/llvm-project/pull/124969.diff


3 Files Affected:

- (modified) flang/lib/Semantics/resolve-names.cpp (+9) 
- (added) flang/test/Semantics/bug124731.f90 (+24) 
- (modified) flang/test/Semantics/self-use.f90 (-1) 


``````````diff
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 695c8265293a80..e5aa928f8d3bf5 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -3354,6 +3354,15 @@ void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
     // use-associating the same symbol again -- ok
     return;
   }
+  if (useUltimate.owner().IsModule() && localUltimate.owner().IsSubmodule() &&
+      DoesScopeContain(&useUltimate.owner(), localUltimate)) {
+    // Within a submodule, USE'ing a symbol that comes indirectly
+    // from the ancestor module, e.g. foo in:
+    //  MODULE m1; INTERFACE; MODULE SUBROUTINE foo; END INTERFACE; END
+    //  MODULE m2; USE m1; END
+    //  SUBMODULE m1(sm); USE m2; CONTAINS; MODULE PROCEDURE foo; END; END
+    return; // ok, ignore it
+  }
 
   if (localUltimate.name() == useUltimate.name() &&
       localUltimate.owner().IsModule() && useUltimate.owner().IsModule() &&
diff --git a/flang/test/Semantics/bug124731.f90 b/flang/test/Semantics/bug124731.f90
new file mode 100644
index 00000000000000..924b41dd1db48c
--- /dev/null
+++ b/flang/test/Semantics/bug124731.f90
@@ -0,0 +1,24 @@
+!RUN: %flang_fc1 -fsyntax-only %s  2>&1 | FileCheck %s --allow-empty
+!CHECK-NOT: error:
+module m1
+  interface
+    module subroutine foo
+    end
+  end interface
+  real x
+end
+module m2
+  use m1
+end
+submodule(m1) sm1
+  use m2 ! ok
+ contains
+  module procedure foo
+  end
+end
+submodule(m1) sm2
+ contains
+  subroutine bar
+    use m2 ! ok
+  end
+end
diff --git a/flang/test/Semantics/self-use.f90 b/flang/test/Semantics/self-use.f90
index 4bc66a24343c6a..12433732fc3300 100644
--- a/flang/test/Semantics/self-use.f90
+++ b/flang/test/Semantics/self-use.f90
@@ -15,7 +15,6 @@ subroutine modsub
  contains
   module subroutine separate
     !ERROR: Module 'm' cannot USE itself from its own submodule 'submod1'
-    !ERROR: Cannot use-associate 'separate'; it is already declared in this scope
     use m
   end
 end

``````````

</details>


https://github.com/llvm/llvm-project/pull/124969


More information about the flang-commits mailing list