Skip to content

Commit f5b378b

Browse files
authored
[clang] Use new interpreter in EvaluateAsConstantExpr if requested (#70763)
EvaluateAsConstantExpr() uses ::EvaluateInPlace() directly, which does not use the new interpreter if requested. Do it here, which is the same pattern we use in EvaluateAsInitializer.
1 parent e2f8ec7 commit f5b378b

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15672,12 +15672,14 @@ bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx,
1567215672
// this doesn't escape.
1567315673
MaterializeTemporaryExpr BaseMTE(T, const_cast<Expr*>(this), true);
1567415674
APValue::LValueBase Base(&BaseMTE);
15675-
1567615675
Info.setEvaluatingDecl(Base, Result.Val);
15677-
LValue LVal;
15678-
LVal.set(Base);
1567915676

15680-
{
15677+
if (Info.EnableNewConstInterp) {
15678+
if (!Info.Ctx.getInterpContext().evaluateAsRValue(Info, this, Result.Val))
15679+
return false;
15680+
} else {
15681+
LValue LVal;
15682+
LVal.set(Base);
1568115683
// C++23 [intro.execution]/p5
1568215684
// A full-expression is [...] a constant-expression
1568315685
// So we need to make sure temporary objects are destroyed after having
@@ -15686,10 +15688,10 @@ bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx,
1568615688
if (!::EvaluateInPlace(Result.Val, Info, LVal, this) ||
1568715689
Result.HasSideEffects || !Scope.destroy())
1568815690
return false;
15689-
}
1569015691

15691-
if (!Info.discardCleanups())
15692-
llvm_unreachable("Unhandled cleanup; missing full expression marker?");
15692+
if (!Info.discardCleanups())
15693+
llvm_unreachable("Unhandled cleanup; missing full expression marker?");
15694+
}
1569315695

1569415696
if (!CheckConstantExpression(Info, getExprLoc(), getStorageType(Ctx, this),
1569515697
Result.Val, Kind))

clang/test/AST/Interp/literals.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,13 @@ namespace SizeOf {
276276
#if __cplusplus >= 202002L
277277
/// FIXME: The following code should be accepted.
278278
consteval int foo(int n) { // ref-error {{consteval function never produces a constant expression}}
279-
return sizeof(int[n]); // ref-note 3{{not valid in a constant expression}} \
280-
// expected-note {{not valid in a constant expression}}
279+
return sizeof(int[n]); // ref-note 3{{not valid in a constant expression}}
281280
}
282281
constinit int var = foo(5); // ref-error {{not a constant expression}} \
283282
// ref-note 2{{in call to}} \
284283
// ref-error {{does not have a constant initializer}} \
285284
// ref-note {{required by 'constinit' specifier}} \
286285
// expected-error {{is not a constant expression}} \
287-
// expected-note {{in call to}} \
288286
// expected-error {{does not have a constant initializer}} \
289287
// expected-note {{required by 'constinit' specifier}} \
290288

0 commit comments

Comments
 (0)