[flang-commits] [flang] Fix #222168: reject DATA-style initializer on EXTERNAL/INTRINSIC (PR #222256)
Adarsh Mishra via flang-commits
flang-commits at lists.llvm.org
Tue Sep 22 06:58:15 PDT 2026
https://github.com/Adarsh-Me updated https://github.com/llvm/llvm-project/pull/222256
>From 02d68ab3599cea2113c7937026d2cf866504d14c Mon Sep 17 00:00:00 2001
From: Adarsh-Me <122873385+Adarsh-Me at users.noreply.github.com>
Date: Wed, 9 Sep 2026 06:42:11 +0000
Subject: [PATCH 1/2] Fix #222168: reject DATA-style initializer on
EXTERNAL/INTRINSIC
---
flang/lib/Semantics/check-data.cpp | 8 +++++++-
flang/test/Semantics/data28.f90 | 13 +++++++++++++
2 files changed, 20 insertions(+), 1 deletion(-)
create mode 100644 flang/test/Semantics/data28.f90
diff --git a/flang/lib/Semantics/check-data.cpp b/flang/lib/Semantics/check-data.cpp
index 921128ced5d61..7fad6e9b40cf3 100644
--- a/flang/lib/Semantics/check-data.cpp
+++ b/flang/lib/Semantics/check-data.cpp
@@ -266,7 +266,13 @@ void DataChecker::Leave(const parser::EntityDecl &decl) {
std::get_if<std::list<common::Indirection<parser::DataStmtValue>>>(
&init->u)};
if (name && list && !exprAnalyzer_.context().HasError(*name)) {
- AccumulateDataInitializations(inits_, exprAnalyzer_, *name, *list);
+ if (IsProcedure(*name) && !IsProcedurePointer(*name)) {
+ exprAnalyzer_.context().Say(std::get<parser::Name>(decl.t).source,
+ "Procedure '%s' may not have a DATA-style initializer"_err_en_US,
+ name->name());
+ } else {
+ AccumulateDataInitializations(inits_, exprAnalyzer_, *name, *list);
+ }
}
}
}
diff --git a/flang/test/Semantics/data28.f90 b/flang/test/Semantics/data28.f90
new file mode 100644
index 0000000000000..9167da4ba9357
--- /dev/null
+++ b/flang/test/Semantics/data28.f90
@@ -0,0 +1,13 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+! A procedure (EXTERNAL/INTRINSIC) may not have a DATA-style initializer.
+! The initializer used to be silently accepted and dropped (#222168).
+subroutine s1
+ external foo
+ !ERROR: Procedure 'foo' may not have a DATA-style initializer
+ integer foo /1/
+end subroutine
+subroutine s2
+ intrinsic sin
+ !ERROR: Procedure 'sin' may not have a DATA-style initializer
+ integer sin /1/
+end subroutine
>From 5c3d2c83c08795e288252122b9a7d45a206a9fb0 Mon Sep 17 00:00:00 2001
From: Adarsh-Me <122873385+Adarsh-Me at users.noreply.github.com>
Date: Tue, 22 Sep 2026 18:17:07 +0530
Subject: [PATCH 2/2] [flang][semantics] Address review on the DATA-style
initializer check
Expand flang/test/Semantics/data28.f90 to the six cases requested in
review: an intrinsic that is not an unrestricted specific function (sum),
a dummy procedure, a plain local object that only shares a name with an
intrinsic, and procedure pointer initialization. Cite F2023 C880/R842
where the guard rejects a procedure name, as suggested.
Assisted-by: Qoder (AI assistant)
---
flang/lib/Semantics/check-data.cpp | 3 +++
flang/test/Semantics/data28.f90 | 25 ++++++++++++++++++++++++-
2 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/flang/lib/Semantics/check-data.cpp b/flang/lib/Semantics/check-data.cpp
index 7fad6e9b40cf3..75b2e91f6290f 100644
--- a/flang/lib/Semantics/check-data.cpp
+++ b/flang/lib/Semantics/check-data.cpp
@@ -266,6 +266,9 @@ void DataChecker::Leave(const parser::EntityDecl &decl) {
std::get_if<std::list<common::Indirection<parser::DataStmtValue>>>(
&init->u)};
if (name && list && !exprAnalyzer_.context().HasError(*name)) {
+ // A procedure name is not a data-stmt-object (F2023 C880, R842), but
+ // procedure pointer initialization is supported as an extension, so
+ // exclude procedure pointers here.
if (IsProcedure(*name) && !IsProcedurePointer(*name)) {
exprAnalyzer_.context().Say(std::get<parser::Name>(decl.t).source,
"Procedure '%s' may not have a DATA-style initializer"_err_en_US,
diff --git a/flang/test/Semantics/data28.f90 b/flang/test/Semantics/data28.f90
index 9167da4ba9357..572f3dab123aa 100644
--- a/flang/test/Semantics/data28.f90
+++ b/flang/test/Semantics/data28.f90
@@ -1,6 +1,8 @@
! RUN: %python %S/test_errors.py %s %flang_fc1
! A procedure (EXTERNAL/INTRINSIC) may not have a DATA-style initializer.
-! The initializer used to be silently accepted and dropped (#222168).
+! The initializer used to be silently accepted and dropped (#222168), or,
+! for an intrinsic that is not an unrestricted specific function, crashed
+! the compiler (CHECK(designator.has_value()) in data-to-inits.cpp).
subroutine s1
external foo
!ERROR: Procedure 'foo' may not have a DATA-style initializer
@@ -11,3 +13,24 @@ subroutine s2
!ERROR: Procedure 'sin' may not have a DATA-style initializer
integer sin /1/
end subroutine
+subroutine s3 ! used to crash: SUM is not an unrestricted specific intrinsic
+ intrinsic sum
+ !ERROR: Procedure 'sum' may not have a DATA-style initializer
+ integer sum /1/
+end subroutine
+subroutine s4(f) ! dummy procedure
+ external f
+ !ERROR: Procedure 'f' may not have a DATA-style initializer
+ integer f /1/
+end subroutine
+subroutine s5 ! no INTRINSIC/EXTERNAL: a plain local object named like an
+ integer sum /1/ ! intrinsic must stay accepted (guard keys on symbol class)
+ print *, sum
+end subroutine
+subroutine s6 ! procedure POINTER initialization must stay accepted (8.6.7)
+ interface
+ integer function tgt()
+ end function
+ end interface
+ procedure(tgt), pointer :: p => null()
+end subroutine
More information about the flang-commits
mailing list