-
Notifications
You must be signed in to change notification settings - Fork 15.6k
[flang] Check for errors when analyzing array constructors #173092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
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 llvm#127425
|
@llvm/pr-subscribers-flang-semantics Author: Leandro Lupori (luporl) ChangesErrors in array constructor values result in the array having Fixes #127425 Full diff: https://github.com/llvm/llvm-project/pull/173092.diff 2 Files Affected:
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
|
ergawy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank @luporl! Just one stop by comment if that's ok.
| ! RUN: %python %S/test_errors.py %s %flang_fc1 | ||
|
|
||
| program main | ||
| pointer (j1,ja) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add implicit none and add explicit type declarations for better test readability?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review.
I've added explicit type declarations.
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