Skip to content
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

[clang] Reject constexpr-unknown values as constant expressions more consistently #129952

Merged
merged 3 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2419,6 +2419,16 @@ static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc,
LVal.getLValueCallIndex() == 0) &&
"have call index for global lvalue");

if (LVal.allowConstexprUnknown()) {
if (BaseVD) {
Info.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << BaseVD;
NoteLValueLocation(Info, Base);
} else {
Info.FFDiag(Loc);
}
return false;
}

if (Base.is<DynamicAllocLValue>()) {
Info.FFDiag(Loc, diag::note_constexpr_dynamic_alloc)
<< IsReferenceType << !Designator.Entries.empty();
Expand Down Expand Up @@ -3597,7 +3607,8 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
// expressions here; doing so would regress diagnostics for things like
// reading from a volatile constexpr variable.
if ((Info.getLangOpts().CPlusPlus && !VD->hasConstantInitialization() &&
VD->mightBeUsableInConstantExpressions(Info.Ctx)) ||
VD->mightBeUsableInConstantExpressions(Info.Ctx) &&
!AllowConstexprUnknown) ||
((Info.getLangOpts().CPlusPlus || Info.getLangOpts().OpenCL) &&
!Info.getLangOpts().CPlusPlus11 && !VD->hasICEInitializer(Info.Ctx))) {
if (Init) {
Expand Down Expand Up @@ -16993,18 +17004,6 @@ bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx,

if (!Info.discardCleanups())
llvm_unreachable("Unhandled cleanup; missing full expression marker?");

if (Value.allowConstexprUnknown()) {
assert(Value.isLValue() && "Expected an lvalue");
auto Base = Value.getLValueBase();
const auto *NewVD = Base.dyn_cast<const ValueDecl *>();
if (!NewVD)
NewVD = VD;
Info.FFDiag(getExprLoc(), diag::note_constexpr_var_init_non_constant, 1)
<< NewVD;
NoteLValueLocation(Info, Base);
return false;
}
}

return CheckConstantExpression(Info, DeclLoc, DeclTy, Value,
Expand Down
15 changes: 14 additions & 1 deletion clang/test/CodeGenCXX/cxx23-p2280r4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

extern int& s;

// CHECK: @_Z4testv()
// CHECK-LABEL: @_Z4testv()
// CHECK-NEXT: entry:
// CHECK-NEXT: [[I:%.*]] = alloca ptr, align {{.*}}
// CHECK-NEXT: [[X:%.*]] = load ptr, ptr @s, align {{.*}}
Expand All @@ -13,3 +13,16 @@ int& test() {
auto &i = s;
return i;
}

// CHECK-LABEL: @_Z1fv(
// CHECK: [[X1:%.*]] = load ptr, ptr @x, align 8
// CHECK-NEXT: store ptr [[X1]]
// CHECK: [[X2:%.*]] = load ptr, ptr @x, align 8
// CHECK-NEXT: store ptr [[X2]]
// CHECK: [[X3:%.*]] = load ptr, ptr @x, align 8
// CHECK-NEXT: store ptr [[X3]]
int &ff();
int &x = ff();
struct A { int& x; };
struct B { A x[20]; };
B f() { return {x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x}; }
4 changes: 2 additions & 2 deletions clang/test/SemaCXX/constant-expression-cxx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1472,8 +1472,8 @@ namespace ConvertedConstantExpr {
enum class E {
em = m,
en = n, // expected-error {{enumerator value is not a constant expression}} cxx11_20-note {{initializer of 'n' is unknown}}
eo = (m + // pre-cxx23-error {{not a constant expression}}
n // cxx11_20-note {{initializer of 'n' is unknown}} cxx23-error {{not a constant expression}}
eo = (m + // expected-error {{not a constant expression}}
n // cxx11_20-note {{initializer of 'n' is unknown}}
),
eq = reinterpret_cast<long>((int*)0) // expected-error {{not a constant expression}} expected-note {{reinterpret_cast}}
};
Expand Down
29 changes: 26 additions & 3 deletions clang/test/SemaCXX/constant-expression-p2280r4.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %clang_cc1 -std=c++23 -verify %s
// RUN: %clang_cc1 -std=c++23 -verify=expected,nointerpreter %s
// RUN: %clang_cc1 -std=c++23 -verify %s -fexperimental-new-constant-interpreter

using size_t = decltype(sizeof(0));
Expand Down Expand Up @@ -39,8 +39,8 @@ void splash(Swim& swam) {
static_assert(swam.phelps() == 28); // ok
static_assert((&swam)->phelps() == 28); // ok
Swim* pswam = &swam; // expected-note {{declared here}}
static_assert(pswam->phelps() == 28); // expected-error {{static assertion expression is not an integral constant expression}}
// expected-note@-1 {{read of non-constexpr variable 'pswam' is not allowed in a constant expression}}
static_assert(pswam->phelps() == 28); // expected-error {{static assertion expression is not an integral constant expression}} \
// expected-note {{read of non-constexpr variable 'pswam' is not allowed in a constant expression}}
static_assert(how_many(swam) == 28); // ok
static_assert(Swim().lochte() == 12); // ok
static_assert(swam.lochte() == 12); // expected-error {{static assertion expression is not an integral constant expression}}
Expand Down Expand Up @@ -154,3 +154,26 @@ int g() {
static_assert(f(arr) == 5);
}
}

namespace GH128409 {
int &ff();
int &x = ff(); // nointerpreter-note {{declared here}}
constinit int &z = x; // expected-error {{variable does not have a constant initializer}} \
// expected-note {{required by 'constinit' specifier here}} \
// nointerpreter-note {{initializer of 'x' is not a constant expression}}
}

namespace GH129845 {
int &ff();
int &x = ff(); // nointerpreter-note {{declared here}}
struct A { int& x; };
constexpr A g = {x}; // expected-error {{constexpr variable 'g' must be initialized by a constant expression}} \
// nointerpreter-note {{initializer of 'x' is not a constant expression}}
const A* gg = &g;
}

namespace extern_reference_used_as_unknown {
extern int &x;
int y;
constinit int& g = (x,y); // expected-warning {{left operand of comma operator has no effect}}
}