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][ExprConst] Don't diagnose a non-existent init as not constant #124575

Merged
merged 2 commits into from
Jan 28, 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
8 changes: 6 additions & 2 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3600,8 +3600,12 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
VD->mightBeUsableInConstantExpressions(Info.Ctx)) ||
((Info.getLangOpts().CPlusPlus || Info.getLangOpts().OpenCL) &&
!Info.getLangOpts().CPlusPlus11 && !VD->hasICEInitializer(Info.Ctx))) {
Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, 1) << VD;
NoteLValueLocation(Info, Base);
if (Init) {
Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, 1) << VD;
NoteLValueLocation(Info, Base);
} else {
Info.CCEDiag(E);
}
}

// Never use the initializer of a weak variable, not even for constant
Expand Down
8 changes: 4 additions & 4 deletions clang/test/SemaCXX/constant-expression-cxx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ namespace InstantiateCaseStmt {

namespace ConvertedConstantExpr {
extern int &m;
extern int &n; // expected-note 2{{declared here}}
extern int &n; // pre-cxx23-note 2{{declared here}}

constexpr int k = 4;
int &m = const_cast<int&>(k);
Expand All @@ -1471,9 +1471,9 @@ namespace ConvertedConstantExpr {
// useless note and instead just point to the non-constant subexpression.
enum class E {
em = m,
en = n, // cxx23-note {{initializer of 'n' is not a constant expression}} expected-error {{enumerator value is not a constant expression}} cxx11_20-note {{initializer of 'n' is unknown}}
eo = (m + // expected-error {{not a constant expression}}
n // cxx23-note {{initializer of 'n' is not a constant expression}} cxx11_20-note {{initializer of 'n' is unknown}}
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}}
),
eq = reinterpret_cast<long>((int*)0) // expected-error {{not a constant expression}} expected-note {{reinterpret_cast}}
};
Expand Down
3 changes: 1 addition & 2 deletions clang/test/SemaCXX/constant-expression-p2280r4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ void splash(Swim& swam) {
}

extern Swim dc;
extern Swim& trident; // expected-note {{declared here}}
extern Swim& trident;

constexpr auto& sandeno = typeid(dc); // ok: can only be typeid(Swim)
constexpr auto& gallagher = typeid(trident); // expected-error {{constexpr variable 'gallagher' must be initialized by a constant expression}}
// expected-note@-1 {{initializer of 'trident' is not a constant expression}}

namespace explicitThis {
struct C {
Expand Down
Loading