[flang-commits] [flang] [FLANG][Semantics] Add check for DATA statement in interface body. (PR #221397)

via flang-commits flang-commits at lists.llvm.org
Tue Sep 15 03:02:51 PDT 2026


https://github.com/laoshd updated https://github.com/llvm/llvm-project/pull/221397

>From e6a35d039bc6cb04c51623a269156de75d00b919 Mon Sep 17 00:00:00 2001
From: Shandong Lao <shandong.lao at hpe.com>
Date: Sat, 5 Sep 2026 00:58:45 -0500
Subject: [PATCH 1/4] [FLANG][Semantics] Add check for DATA statement in
 interface body.

Will check and reject DATA statements in interface bodies before lowering. A regression test is also added.
---
 flang/lib/Semantics/resolve-names.cpp        | 13 +++++++++++++
 flang/test/Semantics/interface-body-data.f90 | 11 +++++++++++
 2 files changed, 24 insertions(+)
 create mode 100644 flang/test/Semantics/interface-body-data.f90

diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 6517232a1e21c..1b70985066ac7 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -1356,6 +1356,7 @@ class ConstructVisitor : public virtual DeclarationVisitor {
   bool Pre(const parser::AcSpec &);
   bool Pre(const parser::AcImpliedDo &);
   bool Pre(const parser::DataImpliedDo &);
+  bool Pre(const parser::DataStmt &);
   bool Pre(const parser::DataIDoObject &);
   bool Pre(const parser::DataStmtObject &);
   bool Pre(const parser::DataStmtValue &);
@@ -8922,6 +8923,18 @@ bool ConstructVisitor::Pre(const parser::DataStmtObject &x) {
   return false;
 }
 
+bool ConstructVisitor::Pre(const parser::DataStmt &) {
+  // C1506: an interface body may not contain a DATA statement.
+  if (const Symbol *symbol{currScope().symbol()}) {
+    if (const auto *subp{symbol->detailsIf<SubprogramDetails>()};
+        subp && subp->isInterface()) {
+      Say(currStmtSource().value(),
+          "A DATA statement may not appear in an interface body"_err_en_US);
+    }
+  }
+  return true;
+}
+
 bool ConstructVisitor::Pre(const parser::DataStmtValue &x) {
   const auto &data{std::get<parser::DataStmtConstant>(x.t)};
   auto &mutableData{const_cast<parser::DataStmtConstant &>(data)};
diff --git a/flang/test/Semantics/interface-body-data.f90 b/flang/test/Semantics/interface-body-data.f90
new file mode 100644
index 0000000000000..bdd522ed0eee8
--- /dev/null
+++ b/flang/test/Semantics/interface-body-data.f90
@@ -0,0 +1,11 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+
+interface
+  subroutine s
+    integer :: x
+    !ERROR: A DATA statement may not appear in an interface body
+    data x /1/
+  end subroutine
+end interface
+
+end
\ No newline at end of file

>From e428e73b9cd98e01f6f600e0a626236cd52272b7 Mon Sep 17 00:00:00 2001
From: Shandong Lao <shandong.lao at hpe.com>
Date: Sun, 6 Sep 2026 06:46:57 -0500
Subject: [PATCH 2/4] [FLANG][Semantics] Remove unused variable and data
 statement in call10.f90.

The removed code violates C1506/R1505 (F2023).
---
 flang/test/Semantics/call10.f90 | 2 --
 1 file changed, 2 deletions(-)

diff --git a/flang/test/Semantics/call10.f90 b/flang/test/Semantics/call10.f90
index a9b760cc692e6..cc4e7a707400a 100644
--- a/flang/test/Semantics/call10.f90
+++ b/flang/test/Semantics/call10.f90
@@ -26,8 +26,6 @@ pure subroutine s05a
       import polyAlloc
       real, save :: v1
       real :: v2 = 0.
-      real :: v3
-      data v3/0./
       real :: v4
       common /blk/ v4
       save /blk/

>From e494b0b6ceb8e78cf863519f21eb6c76fb8f8229 Mon Sep 17 00:00:00 2001
From: Shandong Lao <shandong.lao at hpe.com>
Date: Mon, 14 Sep 2026 12:52:52 -0500
Subject: [PATCH 3/4] [FLANG][Semantics] #219525: Fix formatting by adding
 newline at end of file

interface-body-data.f90
---
 flang/test/Semantics/interface-body-data.f90 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/test/Semantics/interface-body-data.f90 b/flang/test/Semantics/interface-body-data.f90
index bdd522ed0eee8..f492ab40e0995 100644
--- a/flang/test/Semantics/interface-body-data.f90
+++ b/flang/test/Semantics/interface-body-data.f90
@@ -8,4 +8,4 @@ subroutine s
   end subroutine
 end interface
 
-end
\ No newline at end of file
+end

>From 4c17ef92a4464fbee3b4a109ed7d2b5dd25eb566 Mon Sep 17 00:00:00 2001
From: Shandong Lao <shandong.lao at hpe.com>
Date: Tue, 15 Sep 2026 05:02:19 -0500
Subject: [PATCH 4/4] [FLANG][Semantics] #219525: Add more test cases into test
 interface-body-data.f90

---
 flang/test/Semantics/interface-body-data.f90 | 59 +++++++++++++++++---
 1 file changed, 51 insertions(+), 8 deletions(-)

diff --git a/flang/test/Semantics/interface-body-data.f90 b/flang/test/Semantics/interface-body-data.f90
index f492ab40e0995..0114f6b1ee069 100644
--- a/flang/test/Semantics/interface-body-data.f90
+++ b/flang/test/Semantics/interface-body-data.f90
@@ -1,11 +1,54 @@
 ! RUN: %python %S/test_errors.py %s %flang_fc1
 
-interface
-  subroutine s
-    integer :: x
-    !ERROR: A DATA statement may not appear in an interface body
-    data x /1/
-  end subroutine
-end interface
+module m
+  interface
+    subroutine ms(x)
+      integer :: x
+      integer :: y
+      !ERROR: A DATA statement may not appear in an interface body
+      data y /1/
+    end subroutine
+  end interface
+end module
 
-end
+module mop
+  type t
+    integer :: i
+  end type
+  interface operator(+)
+    function add(a, b) result(r)
+      import t
+      type(t), intent(in) :: a, b
+      type(t) :: r
+      integer :: w
+      !ERROR: A DATA statement may not appear in an interface body
+      data w /3/
+    end function
+  end interface
+end module
+
+program p
+  interface
+    subroutine s
+      integer :: x
+      !ERROR: A DATA statement may not appear in an interface body
+      data x /1/
+    end subroutine
+    pure subroutine sp
+      integer :: z
+      !ERROR: A DATA statement may not appear in an interface body
+      data z /2/
+    end subroutine
+    subroutine cb
+      real :: v
+      common /blk/ v
+      !ERROR: A DATA statement may not appear in an interface body
+      data v /0./
+    end subroutine
+    subroutine ok
+      ! Legal type declaration initialization is not a DATA statement.
+      ! it is simply a specification with no effect (F2023 15.4.3.2 p6).
+      integer :: v = 1
+    end subroutine
+  end interface
+end program



More information about the flang-commits mailing list