Skip to content

[flang] silence bogus error with BIND(C) variable in hermetic module #143737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025

Conversation

jeanPerier
Copy link
Contributor

@jeanPerier jeanPerier commented Jun 11, 2025

The global name semantic check was firing in a bogus way when BIND(C) variables are in hermetic module.

The added test would fail to compile with error:

error: Semantic errors in flang/test/Semantics/modfile76.F90
./modfile75b.mod:6:21: error: Two entities have the same global name 'x'
  integer(4),bind(c)::x
                      ^
./modfile75a.mod:3:21: Conflicting declaration
  integer(4),bind(c)::x

Do not raise the error if one of the symbol with the conflicting global name is an "hermetic variant" of the other.

@jeanPerier jeanPerier requested a review from klausler June 11, 2025 15:50
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Jun 11, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 11, 2025

@llvm/pr-subscribers-flang-semantics

Author: None (jeanPerier)

Changes

The global name semantic check was firing in a bogus way when BIND(C) variables are in hermetic module.

The added test would fail to compile with error:

error: Semantic errors in flang/test/Semantics/modfile76.F90
./modfile75b.mod:6:21: error: Two entities have the same global name 'x'
  integer(4),bind(c)::x
                      ^
./modfile75a.mod:3:21: Conflicting declaration
  integer(4),bind(c)::x

Do not raise the error if one of the symbol with the conflicting global name is an "hermetic variant" of the other.


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

2 Files Affected:

  • (modified) flang/lib/Semantics/check-declarations.cpp (+10)
  • (added) flang/test/Semantics/modfile76.F90 (+24)
diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index 46a5b970fdf0c..f9d64485f1407 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -2958,6 +2958,14 @@ static std::optional<std::string> DefinesGlobalName(const Symbol &symbol) {
   return std::nullopt;
 }
 
+static bool IsSameSymbolFromHermeticModule(
+    const Symbol &symbol, const Symbol &other) {
+  return symbol.name() == other.name() && symbol.owner().IsModule() &&
+      other.owner().IsModule() && symbol.owner() != other.owner() &&
+      symbol.owner().GetName() &&
+      symbol.owner().GetName() == other.owner().GetName();
+}
+
 // 19.2 p2
 void CheckHelper::CheckGlobalName(const Symbol &symbol) {
   if (auto global{DefinesGlobalName(symbol)}) {
@@ -2975,6 +2983,8 @@ void CheckHelper::CheckGlobalName(const Symbol &symbol) {
           (!IsExternalProcedureDefinition(symbol) ||
               !IsExternalProcedureDefinition(other))) {
         // both are procedures/BLOCK DATA, not both definitions
+      } else if (IsSameSymbolFromHermeticModule(symbol, other)) {
+        // Both symbols are the same thing.
       } else if (symbol.has<ModuleDetails>()) {
         Warn(common::LanguageFeature::BenignNameClash, symbol.name(),
             "Module '%s' conflicts with a global name"_port_en_US,
diff --git a/flang/test/Semantics/modfile76.F90 b/flang/test/Semantics/modfile76.F90
new file mode 100644
index 0000000000000..50ee9a088e119
--- /dev/null
+++ b/flang/test/Semantics/modfile76.F90
@@ -0,0 +1,24 @@
+!RUN: %flang_fc1 -fsyntax-only -fhermetic-module-files -DSTEP=1 %s
+!RUN: %flang_fc1 -fsyntax-only %s
+
+! Tests that a BIND(C) variable in a module A captured in a hermetic module
+! file USE'd in a module B is not creating bogus complaints about BIND(C) name
+! conflict when both module A and B are later accessed.
+
+#if STEP == 1
+module modfile75a
+  integer, bind(c) :: x
+end
+
+module modfile75b
+  use modfile75a ! capture hermetically
+end
+
+#else
+subroutine test
+  use modfile75a
+  use modfile75b
+  implicit none
+  print *, x
+end subroutine
+#endif

@jeanPerier jeanPerier merged commit 621a7d0 into llvm:main Jun 11, 2025
10 checks passed
@jeanPerier jeanPerier deleted the hermetic_bindc branch June 11, 2025 17:02
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
…lvm#143737)

The global name semantic check was firing in a bogus way when BIND(C)
variables are in hermetic module.

Do not raise the error if one of the symbol with the conflicting global
name is an "hermetic variant" of the other.
akuhlens pushed a commit to akuhlens/llvm-project that referenced this pull request Jun 24, 2025
…lvm#143737)

The global name semantic check was firing in a bogus way when BIND(C)
variables are in hermetic module.

Do not raise the error if one of the symbol with the conflicting global
name is an "hermetic variant" of the other.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants