-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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] Treat constexpr-unknown value as invalid in EvaluateAsInitializer
#128409
Changes from all commits
83248eb
ac2bd21
1cea7d4
bc24bde
52c8159
1f4618c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3628,8 +3628,6 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E, | |||||||||
if (AllowConstexprUnknown) { | ||||||||||
if (!Result) | ||||||||||
Result = &Info.CurrentCall->createConstexprUnknownAPValues(VD, Base); | ||||||||||
else | ||||||||||
Result->setConstexprUnknown(); | ||||||||||
} | ||||||||||
return true; | ||||||||||
} | ||||||||||
|
@@ -17000,6 +16998,18 @@ 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) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, we should emit a note here: llvm-project/clang/lib/AST/ExprConstant.cpp Lines 3581 to 3584 in a955426
But I don't know how to add a "lazy" note (i.e., these notes will be shown if an error occurs). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what you mean by a "lazy" note; the whole point of constexpr-unknown is that it's not an error at the point it's constructed. If you need more information to generate a good diagnostic, you could pass that information to createConstexprUnknownAPValues, and store it in the APValue itself, I guess. Computing the whole diagnostic in advance seems like it would be too expensive, though. I don't think it's a big deal if we generate slightly different diagnostics depending on whether we're in C++23 mode, as long as we generate some explanation for what's happening. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That is what the current implementation does. |
||||||||||
<< NewVD; | ||||||||||
NoteLValueLocation(Info, Base); | ||||||||||
return false; | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
return CheckConstantExpression(Info, DeclLoc, DeclTy, Value, | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++23 %s -emit-llvm -o - | FileCheck %s | ||
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %s -emit-llvm -o - | FileCheck %s | ||
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++17 %s -emit-llvm -o - | FileCheck %s | ||
|
||
extern int& s; | ||
|
||
// CHECK: @_Z4testv() | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: [[I:%.*]] = alloca ptr, align {{.*}} | ||
// CHECK-NEXT: [[X:%.*]] = load ptr, ptr @s, align {{.*}} | ||
// CHECK-NEXT: store ptr [[X]], ptr [[I]], align {{.*}} | ||
int& test() { | ||
auto &i = s; | ||
return i; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleting this makes a lot of sense; I was really confused trying to parse what this line in the original patch was supposed to do.