Skip to content

Commit c40a997

Browse files
committed
release/20.x: [clang] Don't evaluate the initializer of constexpr-unknown parameters. (llvm#142498)
Backport 9788521
1 parent 7cf1453 commit c40a997

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3525,7 +3525,12 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
35253525
// should begin within the evaluation of E
35263526
// Used to be C++20 [expr.const]p5.12.2:
35273527
// ... its lifetime began within the evaluation of E;
3528-
if (isa<ParmVarDecl>(VD) && !AllowConstexprUnknown) {
3528+
if (isa<ParmVarDecl>(VD)) {
3529+
if (AllowConstexprUnknown) {
3530+
Result = &Info.CurrentCall->createConstexprUnknownAPValues(VD, Base);
3531+
return true;
3532+
}
3533+
35293534
// Assume parameters of a potential constant expression are usable in
35303535
// constant expressions.
35313536
if (!Info.checkingPotentialConstantExpression() ||

clang/test/SemaCXX/constant-expression-p2280r4.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,15 @@ int f() {
200200
return !get_value<Dummy>(); // contextually convert the function call result to bool
201201
}
202202
}
203+
204+
namespace param_reference {
205+
constexpr int arbitrary = -12345;
206+
constexpr void f(const int &x = arbitrary) { // expected-note {{declared here}}
207+
constexpr const int &v1 = x; // expected-error {{must be initialized by a constant expression}} \
208+
// expected-note {{reference to 'x' is not a constant expression}}
209+
constexpr const int &v2 = (x, arbitrary); // expected-warning {{left operand of comma operator has no effect}}
210+
constexpr int v3 = x; // expected-error {{must be initialized by a constant expression}}
211+
static_assert(x==arbitrary); // expected-error {{static assertion expression is not an integral constant expression}}
212+
static_assert(&x - &x == 0);
213+
}
214+
}

0 commit comments

Comments
 (0)