Skip to content

[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

Merged
merged 8 commits into from
Oct 24, 2024
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions clang/test/SemaCXX/constant-expression-cxx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
}
}
Loading