Skip to content

Commit c678e8f

Browse files
committed
[clang] Improve constexpr-unknown diagnostics.
APValue::ConstexprUnknown() constructs a broken LValue that doesn't have an lvalue path, which confuses later error handling. It turns out we don't actually use the result of createConstexprUnknownAPValues for anything, so just stop using it. Just construct the LValue directly when we need it. Make findCompleteObject emit errors more aggressively; allowing it to succeed for constexpr-unknown objects leads to weird states where it succeeds, but doesn't return a well-formed object. Delete the check for constexpr-unknown in dynamic_cast handling: it's not necessary, and breaks with the other changes in this patch. These changes allow us to produce proper diagnostics when something fails to be evaluated, instead of just printing a generic top-level error without any notes.
1 parent aa3c5d0 commit c678e8f

File tree

4 files changed

+79
-46
lines changed

4 files changed

+79
-46
lines changed

clang/include/clang/Basic/DiagnosticASTKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ def note_constexpr_access_static_temporary : Note<
240240
"outside the expression that created the temporary">;
241241
def note_constexpr_access_unreadable_object : Note<
242242
"%sub{access_kind}0 object '%1' whose value is not known">;
243+
def note_constexpr_access_unknown_variable : Note<
244+
"%sub{access_kind}0 variable %1 whose value is not known">;
243245
def note_constexpr_access_deleted_object : Note<
244246
"%sub{access_kind}0 heap allocated object that has been deleted">;
245247
def note_constexpr_modify_global : Note<

clang/lib/AST/ExprConstant.cpp

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ namespace {
202202
assert(!isBaseAnAllocSizeCall(Base) &&
203203
"Unsized arrays shouldn't appear here");
204204
unsigned MostDerivedLength = 0;
205-
Type = getType(Base);
205+
// The type of Base is a reference type if the base is a constexpr-unknown
206+
// variable. In that case, look through the reference type.
207+
Type = getType(Base).getNonReferenceType();
206208

207209
for (unsigned I = 0, N = Path.size(); I != N; ++I) {
208210
if (Type->isArrayType()) {
@@ -571,7 +573,6 @@ namespace {
571573
typedef std::map<MapKeyTy, APValue> MapTy;
572574
/// Temporaries - Temporary lvalues materialized within this stack frame.
573575
MapTy Temporaries;
574-
MapTy ConstexprUnknownAPValues;
575576

576577
/// CallRange - The source range of the call expression for this call.
577578
SourceRange CallRange;
@@ -646,9 +647,6 @@ namespace {
646647
APValue &createTemporary(const KeyT *Key, QualType T,
647648
ScopeKind Scope, LValue &LV);
648649

649-
APValue &createConstexprUnknownAPValues(const VarDecl *Key,
650-
APValue::LValueBase Base);
651-
652650
/// Allocate storage for a parameter of a function call made in this frame.
653651
APValue &createParam(CallRef Args, const ParmVarDecl *PVD, LValue &LV);
654652

@@ -1955,15 +1953,6 @@ APValue &CallStackFrame::createTemporary(const KeyT *Key, QualType T,
19551953
return createLocal(Base, Key, T, Scope);
19561954
}
19571955

1958-
APValue &
1959-
CallStackFrame::createConstexprUnknownAPValues(const VarDecl *Key,
1960-
APValue::LValueBase Base) {
1961-
APValue &Result = ConstexprUnknownAPValues[MapKeyTy(Key, Base.getVersion())];
1962-
Result = APValue(Base, CharUnits::Zero(), APValue::ConstexprUnknown{});
1963-
1964-
return Result;
1965-
}
1966-
19671956
/// Allocate storage for a parameter of a function call made in this frame.
19681957
APValue &CallStackFrame::createParam(CallRef Args, const ParmVarDecl *PVD,
19691958
LValue &LV) {
@@ -3493,7 +3482,7 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
34933482
APValue::LValueBase Base(VD, Frame ? Frame->Index : 0, Version);
34943483

34953484
auto CheckUninitReference = [&](bool IsLocalVariable) {
3496-
if (!Result->hasValue() && VD->getType()->isReferenceType()) {
3485+
if (!Result || (!Result->hasValue() && VD->getType()->isReferenceType())) {
34973486
// C++23 [expr.const]p8
34983487
// ... For such an object that is not usable in constant expressions, the
34993488
// dynamic type of the object is constexpr-unknown. For such a reference
@@ -3509,7 +3498,7 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
35093498
Info.FFDiag(E, diag::note_constexpr_use_uninit_reference);
35103499
return false;
35113500
}
3512-
Result = &Info.CurrentCall->createConstexprUnknownAPValues(VD, Base);
3501+
Result = nullptr;
35133502
}
35143503
return true;
35153504
};
@@ -3552,7 +3541,7 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
35523541
// ... its lifetime began within the evaluation of E;
35533542
if (isa<ParmVarDecl>(VD)) {
35543543
if (AllowConstexprUnknown) {
3555-
Result = &Info.CurrentCall->createConstexprUnknownAPValues(VD, Base);
3544+
Result = nullptr;
35563545
return true;
35573546
}
35583547

@@ -3659,12 +3648,8 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
36593648

36603649
Result = VD->getEvaluatedValue();
36613650

3662-
if (!Result) {
3663-
if (AllowConstexprUnknown)
3664-
Result = &Info.CurrentCall->createConstexprUnknownAPValues(VD, Base);
3665-
else
3666-
return false;
3667-
}
3651+
if (!Result && !AllowConstexprUnknown)
3652+
return false;
36683653

36693654
return CheckUninitReference(/*IsLocalVariable=*/false);
36703655
}
@@ -3947,11 +3932,6 @@ findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj,
39473932
const FieldDecl *LastField = nullptr;
39483933
const FieldDecl *VolatileField = nullptr;
39493934

3950-
// C++23 [expr.const]p8 If we have an unknown reference or pointers and it
3951-
// does not have a value then bail out.
3952-
if (O->allowConstexprUnknown() && !O->hasValue())
3953-
return false;
3954-
39553935
// Walk the designator's path to find the subobject.
39563936
for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) {
39573937
// Reading an indeterminate value is undefined, but assigning over one is OK.
@@ -4491,6 +4471,14 @@ static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E,
44914471

44924472
if (!evaluateVarDeclInit(Info, E, VD, Frame, LVal.getLValueVersion(), BaseVal))
44934473
return CompleteObject();
4474+
// If evaluateVarDeclInit sees a constexpr-unknown variable, it returns
4475+
// a null BaseVal. Any constexpr-unknown variable seen here is an error:
4476+
// we can't access a constexpr-unknown object.
4477+
if (!BaseVal) {
4478+
Info.FFDiag(E, diag::note_constexpr_access_unknown_variable, 1) << AK << VD;
4479+
Info.Note(VD->getLocation(), diag::note_declared_at);
4480+
return CompleteObject();
4481+
}
44944482
} else if (DynamicAllocLValue DA = LVal.Base.dyn_cast<DynamicAllocLValue>()) {
44954483
std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA);
44964484
if (!Alloc) {
@@ -6057,15 +6045,6 @@ struct CheckDynamicTypeHandler {
60576045
/// dynamic type.
60586046
static bool checkDynamicType(EvalInfo &Info, const Expr *E, const LValue &This,
60596047
AccessKinds AK, bool Polymorphic) {
6060-
// We are not allowed to invoke a virtual function whose dynamic type
6061-
// is constexpr-unknown, so stop early and let this fail later on if we
6062-
// attempt to do so.
6063-
// C++23 [expr.const]p5.6
6064-
// an invocation of a virtual function ([class.virtual]) for an object whose
6065-
// dynamic type is constexpr-unknown;
6066-
if (This.allowConstexprUnknown())
6067-
return true;
6068-
60696048
if (This.Designator.Invalid)
60706049
return false;
60716050

@@ -9063,6 +9042,12 @@ bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) {
90639042
if (!evaluateVarDeclInit(Info, E, VD, Frame, Version, V))
90649043
return false;
90659044

9045+
if (!V) {
9046+
Result.set(VD);
9047+
Result.AllowConstexprUnknown = true;
9048+
return true;
9049+
}
9050+
90669051
return Success(*V, E);
90679052
}
90689053

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,7 @@ namespace InstantiateCaseStmt {
14661466

14671467
namespace ConvertedConstantExpr {
14681468
extern int &m;
1469-
extern int &n; // pre-cxx23-note 2{{declared here}}
1469+
extern int &n; // expected-note 2{{declared here}}
14701470

14711471
constexpr int k = 4;
14721472
int &m = const_cast<int&>(k);
@@ -1475,9 +1475,9 @@ namespace ConvertedConstantExpr {
14751475
// useless note and instead just point to the non-constant subexpression.
14761476
enum class E {
14771477
em = m,
1478-
en = n, // expected-error {{enumerator value is not a constant expression}} cxx11_20-note {{initializer of 'n' is unknown}}
1478+
en = n, // expected-error {{enumerator value is not a constant expression}} cxx11_20-note {{initializer of 'n' is unknown}} cxx23-note {{read of non-constexpr variable 'n'}}
14791479
eo = (m + // expected-error {{not a constant expression}}
1480-
n // cxx11_20-note {{initializer of 'n' is unknown}}
1480+
n // cxx11_20-note {{initializer of 'n' is unknown}} cxx23-note {{read of non-constexpr variable 'n'}}
14811481
),
14821482
eq = reinterpret_cast<long>((int*)0) // expected-error {{not a constant expression}} expected-note {{reinterpret_cast}}
14831483
};

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

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,18 @@ constexpr int how_many(Swim& swam) {
3535
return (p + 1 - 1)->phelps();
3636
}
3737

38-
void splash(Swim& swam) {
38+
void splash(Swim& swam) { // nointerpreter-note {{declared here}}
3939
static_assert(swam.phelps() == 28); // ok
4040
static_assert((&swam)->phelps() == 28); // ok
4141
Swim* pswam = &swam; // expected-note {{declared here}}
4242
static_assert(pswam->phelps() == 28); // expected-error {{static assertion expression is not an integral constant expression}} \
4343
// expected-note {{read of non-constexpr variable 'pswam' is not allowed in a constant expression}}
4444
static_assert(how_many(swam) == 28); // ok
4545
static_assert(Swim().lochte() == 12); // ok
46-
static_assert(swam.lochte() == 12); // expected-error {{static assertion expression is not an integral constant expression}}
47-
static_assert(swam.coughlin == 12); // expected-error {{static assertion expression is not an integral constant expression}}
46+
static_assert(swam.lochte() == 12); // expected-error {{static assertion expression is not an integral constant expression}} \
47+
// nointerpreter-note {{virtual function called on object 'swam' whose dynamic type is not constant}}
48+
static_assert(swam.coughlin == 12); // expected-error {{static assertion expression is not an integral constant expression}} \
49+
// nointerpreter-note {{read of variable 'swam' whose value is not known}}
4850
}
4951

5052
extern Swim dc;
@@ -253,12 +255,56 @@ namespace uninit_reference_used {
253255

254256
namespace param_reference {
255257
constexpr int arbitrary = -12345;
256-
constexpr void f(const int &x = arbitrary) { // expected-note {{declared here}}
258+
constexpr void f(const int &x = arbitrary) { // nointerpreter-note 3 {{declared here}} interpreter-note {{declared here}}
257259
constexpr const int &v1 = x; // expected-error {{must be initialized by a constant expression}} \
258260
// expected-note {{reference to 'x' is not a constant expression}}
259261
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+
constexpr int v3 = x; // expected-error {{must be initialized by a constant expression}} \
263+
// nointerpreter-note {{read of variable 'x' whose value is not known}}
264+
static_assert(x==arbitrary); // expected-error {{static assertion expression is not an integral constant expression}} \
265+
// nointerpreter-note {{read of variable 'x' whose value is not known}}
262266
static_assert(&x - &x == 0);
263267
}
264268
}
269+
270+
namespace dropped_note {
271+
extern int &x; // expected-note {{declared here}}
272+
constexpr int f() { return x; } // nointerpreter-note {{read of non-constexpr variable 'x'}} \
273+
// interpreter-note {{initializer of 'x' is unknown}}
274+
constexpr int y = f(); // expected-error {{constexpr variable 'y' must be initialized by a constant expression}} expected-note {{in call to 'f()'}}
275+
}
276+
277+
namespace dynamic {
278+
struct A {virtual ~A();};
279+
struct B : A {};
280+
void f(A& a) {
281+
constexpr B* b = dynamic_cast<B*>(&a); // expected-error {{must be initialized by a constant expression}} \
282+
// nointerpreter-note {{dynamic_cast applied to object 'a' whose dynamic type is not constant}}
283+
constexpr void* b2 = dynamic_cast<void*>(&a); // expected-error {{must be initialized by a constant expression}} \
284+
// nointerpreter-note {{dynamic_cast applied to object 'a' whose dynamic type is not constant}}
285+
}
286+
}
287+
288+
namespace pointer_comparisons {
289+
extern int &extern_n; // interpreter-note 2 {{declared here}}
290+
extern int &extern_n2;
291+
constexpr int f1(bool b, int& n) {
292+
if (b) {
293+
return &extern_n == &n;
294+
}
295+
return f1(true, n);
296+
}
297+
// FIXME: interpreter incorrectly rejects; both sides are the same constexpr-unknown value.
298+
static_assert(f1(false, extern_n)); // interpreter-error {{static assertion expression is not an integral constant expression}} \
299+
// interpreter-note {{initializer of 'extern_n' is unknown}}
300+
// FIXME: We should diagnose this: we don't know if the references bind
301+
// to the same object.
302+
static_assert(&extern_n != &extern_n2); // interpreter-error {{static assertion expression is not an integral constant expression}} \
303+
// interpreter-note {{initializer of 'extern_n' is unknown}}
304+
void f2(const int &n) {
305+
// FIXME: We should not diagnose this: the two objects provably have
306+
// different addresses because the lifetime of "n" extends across
307+
// the initialization.
308+
constexpr int x = &x == &n; // nointerpreter-error {{must be initialized by a constant expression}}
309+
}
310+
}

0 commit comments

Comments
 (0)