Skip to content

Commit 9788521

Browse files
[clang] Don't evaluate the initializer of constexpr-unknown parameters. (#142498)
If we see a parameter of reference type that isn't part of the frame, don't try to evaluate its default argument. Just treat it as a constexpr-unknown value. Fixes #141114. Fixes #141858.
1 parent 67fa6ea commit 9788521

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
@@ -3550,7 +3550,12 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
35503550
// should begin within the evaluation of E
35513551
// Used to be C++20 [expr.const]p5.12.2:
35523552
// ... its lifetime began within the evaluation of E;
3553-
if (isa<ParmVarDecl>(VD) && !AllowConstexprUnknown) {
3553+
if (isa<ParmVarDecl>(VD)) {
3554+
if (AllowConstexprUnknown) {
3555+
Result = &Info.CurrentCall->createConstexprUnknownAPValues(VD, Base);
3556+
return true;
3557+
}
3558+
35543559
// Assume parameters of a potential constant expression are usable in
35553560
// constant expressions.
35563561
if (!Info.checkingPotentialConstantExpression() ||

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,15 @@ namespace uninit_reference_used {
250250
// expected-note {{in call to 'g5()'}}
251251

252252
}
253+
254+
namespace param_reference {
255+
constexpr int arbitrary = -12345;
256+
constexpr void f(const int &x = arbitrary) { // expected-note {{declared here}}
257+
constexpr const int &v1 = x; // expected-error {{must be initialized by a constant expression}} \
258+
// expected-note {{reference to 'x' is not a constant expression}}
259+
constexpr const int &v2 = (x, arbitrary); // expected-warning {{left operand of comma operator has no effect}}
260+
constexpr int v3 = x; // expected-error {{must be initialized by a constant expression}}
261+
static_assert(x==arbitrary); // expected-error {{static assertion expression is not an integral constant expression}}
262+
static_assert(&x - &x == 0);
263+
}
264+
}

0 commit comments

Comments
 (0)