[flang-commits] [flang] [flang] Check for errors when analyzing array constructors (PR #173092)
Leandro Lupori via flang-commits
flang-commits at lists.llvm.org
Tue Dec 23 08:43:16 PST 2025
https://github.com/luporl updated https://github.com/llvm/llvm-project/pull/173092
>From d57f922c21f5034bbe3d3bc4624b8d86abd8356c Mon Sep 17 00:00:00 2001
From: Leandro Lupori <leandro.lupori at linaro.org>
Date: Fri, 19 Dec 2025 16:27:02 -0300
Subject: [PATCH 1/2] [flang] Check for errors when analyzing array
constructors
Errors in array constructor values result in the array having
less elements than it should, which can cause other errors that
will confuse the user. Avoid this by not returning an expression
on errors.
Fixes #127425
---
flang/lib/Semantics/expression.cpp | 4 ++++
flang/test/Semantics/bug127425.f90 | 8 ++++++++
2 files changed, 12 insertions(+)
create mode 100644 flang/test/Semantics/bug127425.f90
diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index 6f5d0bf9eb242..0b67d1ae013a2 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -2076,11 +2076,15 @@ MaybeExpr ArrayConstructorContext::ToExpr() {
MaybeExpr ExpressionAnalyzer::Analyze(const parser::ArrayConstructor &array) {
const parser::AcSpec &acSpec{array.v};
+ bool hadAnyFatalError{context_.AnyFatalError()};
ArrayConstructorContext acContext{
*this, AnalyzeTypeSpec(acSpec.type, GetFoldingContext())};
for (const parser::AcValue &value : acSpec.values) {
acContext.Add(value);
}
+ if (!hadAnyFatalError && context_.AnyFatalError()) {
+ return std::nullopt;
+ }
return acContext.ToExpr();
}
diff --git a/flang/test/Semantics/bug127425.f90 b/flang/test/Semantics/bug127425.f90
new file mode 100644
index 0000000000000..b01fabba70135
--- /dev/null
+++ b/flang/test/Semantics/bug127425.f90
@@ -0,0 +1,8 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+
+program main
+ pointer (j1,ja)
+ pointer (j2,jb)
+ !ERROR: Values in array constructor must have the same declared type when no explicit type appears
+ if (any((/j1,j2,j3,j4,j5/)/=(/1,2,3,4,5/))) print *,'fail'
+end program main
>From 9b30c2c385b918a604ed9c0f24936ce111754fba Mon Sep 17 00:00:00 2001
From: Leandro Lupori <leandro.lupori at linaro.org>
Date: Tue, 23 Dec 2025 13:42:22 -0300
Subject: [PATCH 2/2] Add explicit type declarations
---
flang/test/Semantics/bug127425.f90 | 2 ++
1 file changed, 2 insertions(+)
diff --git a/flang/test/Semantics/bug127425.f90 b/flang/test/Semantics/bug127425.f90
index b01fabba70135..3cd3abd55a075 100644
--- a/flang/test/Semantics/bug127425.f90
+++ b/flang/test/Semantics/bug127425.f90
@@ -1,6 +1,8 @@
! RUN: %python %S/test_errors.py %s %flang_fc1
program main
+ implicit none
+ integer :: ja, jb, j3, j4, j5
pointer (j1,ja)
pointer (j2,jb)
!ERROR: Values in array constructor must have the same declared type when no explicit type appears
More information about the flang-commits
mailing list