-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[Clang] prevent assertion failure in value-dependent initializer expressions #112612
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
Conversation
@llvm/pr-subscribers-clang Author: Oleksandr T. (a-tarasyuk) ChangesFixes #112140 Full diff: https://github.com/llvm/llvm-project/pull/112612.diff 3 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 817e3abef8d566..1cbf95f0b034d4 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -517,6 +517,7 @@ Bug Fixes to C++ Support
certain situations. (#GH47400), (#GH90896)
- Fix erroneous templated array size calculation leading to crashes in generated code. (#GH41441)
- During the lookup for a base class name, non-type names are ignored. (#GH16855)
+- Fixed an assertion failure when evaluating constant expressions in value-dependent initializers (#GH112140)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 52a7f5778ce6d2..62d88f0f82b760 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -11536,6 +11536,9 @@ bool ArrayExprEvaluator::VisitCXXParenListOrInitListExpr(
LValue Subobject = This;
Subobject.addArray(Info, ExprToVisit, CAT);
auto Eval = [&](const Expr *Init, unsigned ArrayIndex) {
+ if (Init->isValueDependent())
+ return EvaluateDependentExpr(Init, Info);
+
if (!EvaluateInPlace(Result.getArrayInitializedElt(ArrayIndex), Info,
Subobject, Init) ||
!HandleLValueArrayAdjustment(Info, Init, Subobject,
diff --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp
index e2ea984b37cd04..e01aaff1459e88 100644
--- a/clang/test/SemaCXX/constant-expression-cxx11.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp
@@ -2564,3 +2564,13 @@ GH50055::E2 GlobalInitNotCE2 = GH50055::testDefaultArgForParam(); // ok, not a c
constexpr GH50055::E2 GlobalInitCE = (GH50055::E2)-1;
// expected-error@-1 {{constexpr variable 'GlobalInitCE' must be initialized by a constant expression}}
// expected-note@-2 {{integer value -1 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}
+
+namespace GH {
+struct S {
+ constexpr S(const int &a = ) { } // expected-error {{expected expression}}
+};
+
+void foo() {
+ constexpr S s[2] = { }; // expected-error {{constexpr variable 's' must be initialized by a constant expression}}
+}
+}
|
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.
LGTM modulo comment.
Thanks!
There is a description so I'll merge. Shafik can review post commit if needs be. |
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.
LGTM, thank you for adding more details to the summary
…essions (llvm#112612) Fixes llvm#112140 --- ``` CXXConstructExpr 0x14209e580 'const S':'const struct S' contains-errors 'void (const int &)' list `-CXXDefaultArgExpr 0x14209e500 'const int' contains-errors `-RecoveryExpr 0x14209daf0 'const int' contains-errors ``` This change resolves an issue with evaluating `ArrayFiller` initializers in _dependent_ contexts, especially when they involve a `RecoveryExpr`. In certain cases, `ArrayFiller` initializers containing a `RecoveryExpr` from earlier errors are incorrectly passed to `EvaluateInPlace`, causing evaluation failures when they are value-dependent. When this is the case, the initializer is processed through `EvaluateDependentExpr`, which prevents unnecessary evaluation attempts and ensures proper handling of value-dependent initializers in `ArrayFillers`.
Fixes #112140
This change resolves an issue with evaluating
ArrayFiller
initializers in dependent contexts, especially when they involve aRecoveryExpr
. In certain cases,ArrayFiller
initializers containing aRecoveryExpr
from earlier errors are incorrectly passed toEvaluateInPlace
, causing evaluation failures when they are value-dependent.When this is the case, the initializer is processed through
EvaluateDependentExpr
, which prevents unnecessary evaluation attempts and ensures proper handling of value-dependent initializers inArrayFillers
.