[flang-commits] [flang] [flang] Fix DATA-like default component initialization (PR #82784)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Fri Feb 23 08:15:53 PST 2024


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/82784

Ensure that the values in a DATA-like default component initialization pass through expression analysis.

Fixes https://github.com/llvm/llvm-project/issues/81097.

>From a53f2ed6536402c773193b39b92daa997fc8f285 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Fri, 23 Feb 2024 08:13:28 -0800
Subject: [PATCH] [flang] Fix DATA-like default component initialization

Ensure that the values in a DATA-like default component initialization
pass through expression analysis.

Fixes https://github.com/llvm/llvm-project/issues/81097.
---
 flang/include/flang/Semantics/expression.h | 16 +++++++++++++---
 flang/test/Semantics/data21.f90            |  7 +++++++
 2 files changed, 20 insertions(+), 3 deletions(-)
 create mode 100644 flang/test/Semantics/data21.f90

diff --git a/flang/include/flang/Semantics/expression.h b/flang/include/flang/Semantics/expression.h
index a330e241c2cdaa..a224b08da21da7 100644
--- a/flang/include/flang/Semantics/expression.h
+++ b/flang/include/flang/Semantics/expression.h
@@ -506,9 +506,18 @@ class ExprChecker {
   }
 
   bool Pre(const parser::ComponentDefStmt &) {
-    // Already analyzed in name resolution and PDT instantiation;
-    // do not attempt to re-analyze now without type parameters.
-    return false;
+    inComponentDefStmt_ = true;
+    return true;
+  }
+  void Post(const parser::ComponentDefStmt &) { inComponentDefStmt_ = false; }
+  bool Pre(const parser::Initialization &x) {
+    // Default component initialization expressions (but not DATA-like ones
+    // as in DEC STRUCTUREs) were already analyzed in name resolution
+    // and PDT instantiation; do not attempt to re-analyze them without
+    // type parameters.
+    return !inComponentDefStmt_ ||
+        std::holds_alternative<
+            std::list<common::Indirection<parser::DataStmtValue>>>(x.u);
   }
 
   template <typename A> bool Pre(const parser::Scalar<A> &x) {
@@ -538,6 +547,7 @@ class ExprChecker {
   SemanticsContext &context_;
   evaluate::ExpressionAnalyzer exprAnalyzer_{context_};
   int whereDepth_{0}; // nesting of WHERE statements & constructs
+  bool inComponentDefStmt_{false};
 };
 } // namespace Fortran::semantics
 #endif // FORTRAN_SEMANTICS_EXPRESSION_H_
diff --git a/flang/test/Semantics/data21.f90 b/flang/test/Semantics/data21.f90
new file mode 100644
index 00000000000000..639f78440840a8
--- /dev/null
+++ b/flang/test/Semantics/data21.f90
@@ -0,0 +1,7 @@
+! RUN: %flang_fc1 -fdebug-dump-symbols %s 2>&1 | FileCheck %s
+! Ensure that DATA-like default component initializers work.
+! CHECK: j (InDataStmt) size=4 offset=0: ObjectEntity type: INTEGER(4) init:123_4
+type t
+  integer j/123/
+end type
+end



More information about the flang-commits mailing list