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

Ali Bahrani via flang-commits flang-commits at lists.llvm.org
Sat Sep 26 06:49:30 PDT 2026


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

>From d4d2baecf5f255397f30d75e444d7972844224a7 Mon Sep 17 00:00:00 2001
From: Ubuntu <ubuntu at ip-172-31-16-239.ec2.internal>
Date: Mon, 24 Aug 2026 19:53:26 +0000
Subject: [PATCH 1/5] flang semantics - change common block condition to check
 the owner scope instead of the name to allow the same name binding in
 different scopes

---
 flang/lib/Semantics/check-declarations.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index 41ddb61c4340d..f879af7a3b1f4 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 {
@@ -3129,6 +3131,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) {
@@ -3136,7 +3139,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)) &&

>From 3b0c14f5cfdfdb49abf5796be53ff5e348f9a117 Mon Sep 17 00:00:00 2001
From: Ubuntu <ubuntu at ip-172-31-16-239.ec2.internal>
Date: Tue, 25 Aug 2026 00:56:11 +0000
Subject: [PATCH 2/5] flang Semantic tests - update declaration03.f90 to test
 the bug fix on bind c error on commons with different symbol names

---
 flang/test/Semantics/declarations03.f90 | 13 +++++++++++++
 1 file changed, 13 insertions(+)

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

>From 94c1d419d5a215a81a188fcf18f0a44401faf736 Mon Sep 17 00:00:00 2001
From: abahrani <ali.bahrani94521036 at gmail.com>
Date: Tue, 25 Aug 2026 10:52:11 -0400
Subject: [PATCH 3/5] flang semantics - remove debug code from
 check-declarations.cpp

---
 flang/lib/Semantics/check-declarations.cpp | 2 --
 1 file changed, 2 deletions(-)

diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index 856b44bc6cf5b..c5a1018f759fd 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -21,7 +21,6 @@
 #include "flang/Semantics/tools.h"
 #include "flang/Semantics/type.h"
 #include <algorithm>
-#include <iostream>
 #include <map>
 #include <ostream>
 #include <string>
@@ -3144,7 +3143,6 @@ 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) {

>From 4eee3014e4b8e6d83043b25d42f8b97b5d055505 Mon Sep 17 00:00:00 2001
From: Ali Bahrani <ali.bahrani94521036 at gmail.com>
Date: Sat, 26 Sep 2026 09:32:34 -0400
Subject: [PATCH 4/5] Update flang/lib/Semantics/check-declarations.cpp

Remove debug leftovers.

Co-authored-by: Eugene Epshteyn <eepshteyn at nvidia.com>
---
 flang/lib/Semantics/check-declarations.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index c5a1018f759fd..22e828719f5fd 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -22,7 +22,6 @@
 #include "flang/Semantics/type.h"
 #include <algorithm>
 #include <map>
-#include <ostream>
 #include <string>
 
 namespace Fortran::semantics {

>From bfed8cd7dfa4d61287180897824ad6798bc1dfeb Mon Sep 17 00:00:00 2001
From: Ali Bahrani <ali.bahrani94521036 at gmail.com>
Date: Sat, 26 Sep 2026 09:49:18 -0400
Subject: [PATCH 5/5] Update flang/lib/Semantics/check-declarations.cpp to
 match comment and implementation

Co-authored-by: Eugene Epshteyn <eepshteyn at nvidia.com>
---
 flang/lib/Semantics/check-declarations.cpp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index 22e828719f5fd..8760e3d4ca778 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -3150,8 +3150,11 @@ void CheckHelper::CheckGlobalName(const Symbol &symbol) {
         // don't pile on
       } else if (symbol.has<CommonBlockDetails>() &&
           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.
+        // Common blocks with the same name in different scoping units are one
+        // entity (F2023 8.10.2.4).  As an extension, differently named common
+        // blocks in different scoping units may share one binding label: their
+        // object names are the label, so they share storage.  Otherwise 19.2 p2
+        // applies and the collision is an error.
       } else if ((IsProcedure(symbol) || IsBlockData(symbol)) &&
           (IsProcedure(other) || IsBlockData(other)) &&
           (!IsExternalProcedureDefinition(symbol) ||



More information about the flang-commits mailing list