[flang-commits] [flang] [Flang] Fix spurious errors for common blocks with binding labels (PR #218566)

via flang-commits flang-commits at lists.llvm.org
Mon Aug 24 19:56:22 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Ali Bahrani (mrbahrani)

<details>
<summary>Changes</summary>

fixes #<!-- -->212682 
Instead of checking the scope (owner) of commons in the bind, the semantics checked the symbol name and returned the error since they didn't match.
I have also updated declartions04.f90 to incorporate the case.
I have used Claude Ai as an advisory tool, not a direct coding agent.

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


2 Files Affected:

- (modified) flang/lib/Semantics/check-declarations.cpp (+4-1) 
- (modified) flang/test/Semantics/declarations03.f90 (+13) 


``````````diff
diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index 07c6b88f0562a..856b44bc6cf5b 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -21,7 +21,9 @@
 #include "flang/Semantics/tools.h"
 #include "flang/Semantics/type.h"
 #include <algorithm>
+#include <iostream>
 #include <map>
+#include <ostream>
 #include <string>
 
 namespace Fortran::semantics {
@@ -3142,6 +3144,7 @@ static std::optional<std::string> DefinesGlobalName(const Symbol &symbol) {
 
 // 19.2 p2
 void CheckHelper::CheckGlobalName(const Symbol &symbol) {
+  // std::cout << symbol.name().ToString() << " " << symbol.GetParentComponent()->name().ToString()<< "\n";
   if (auto global{DefinesGlobalName(symbol)}) {
     auto pair{globalNames_.emplace(std::move(*global), symbol)};
     if (!pair.second) {
@@ -3149,7 +3152,7 @@ void CheckHelper::CheckGlobalName(const Symbol &symbol) {
       if (context_.HasError(symbol) || context_.HasError(other)) {
         // don't pile on
       } else if (symbol.has<CommonBlockDetails>() &&
-          other.has<CommonBlockDetails>() && symbol.name() == other.name()) {
+          other.has<CommonBlockDetails>() && symbol.owner() != other.owner()) {
         // Two common blocks can have the same global name so long as
         // they're not in the same scope.
       } else if ((IsProcedure(symbol) || IsBlockData(symbol)) &&
diff --git a/flang/test/Semantics/declarations03.f90 b/flang/test/Semantics/declarations03.f90
index 8e6f0a4aaf6bd..4e720e822cc77 100644
--- a/flang/test/Semantics/declarations03.f90
+++ b/flang/test/Semantics/declarations03.f90
@@ -67,6 +67,19 @@ subroutine common2()
   bind(c, name='xcom') /com/ ! no error
 end subroutine
 
+subroutine common3()
+  real :: y
+  common /comA/ y
+  bind(c, name='xcom2') /comA/ ! no error
+end subroutine
+
+subroutine common4()
+  real :: y
+  common /comB/ y
+  bind(c, name='xcom2') /comB/ ! no error
+end subroutine
+
+
 module a
   integer, bind(c, name="int") :: i
 end module

``````````

</details>


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


More information about the flang-commits mailing list