Skip to content

Commit 4bd2307

Browse files
authored
[clang][ExprConst] Don't diagnose a non-existent init as not constant (#124575)
This test: ```c++ extern Swim& trident; // expected-note {{declared here}} 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}} ``` diagnosed the initializer of `trident` as not constant, but `trident` doesn't even have an initializer. Remove that diagnostic in this case.
1 parent 79499f0 commit 4bd2307

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3600,8 +3600,12 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
36003600
VD->mightBeUsableInConstantExpressions(Info.Ctx)) ||
36013601
((Info.getLangOpts().CPlusPlus || Info.getLangOpts().OpenCL) &&
36023602
!Info.getLangOpts().CPlusPlus11 && !VD->hasICEInitializer(Info.Ctx))) {
3603-
Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, 1) << VD;
3604-
NoteLValueLocation(Info, Base);
3603+
if (Init) {
3604+
Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, 1) << VD;
3605+
NoteLValueLocation(Info, Base);
3606+
} else {
3607+
Info.CCEDiag(E);
3608+
}
36053609
}
36063610

36073611
// Never use the initializer of a weak variable, not even for constant

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,7 @@ namespace InstantiateCaseStmt {
14621462

14631463
namespace ConvertedConstantExpr {
14641464
extern int &m;
1465-
extern int &n; // expected-note 2{{declared here}}
1465+
extern int &n; // pre-cxx23-note 2{{declared here}}
14661466

14671467
constexpr int k = 4;
14681468
int &m = const_cast<int&>(k);
@@ -1471,9 +1471,9 @@ namespace ConvertedConstantExpr {
14711471
// useless note and instead just point to the non-constant subexpression.
14721472
enum class E {
14731473
em = m,
1474-
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}}
1475-
eo = (m + // expected-error {{not a constant expression}}
1476-
n // cxx23-note {{initializer of 'n' is not a constant expression}} cxx11_20-note {{initializer of 'n' is unknown}}
1474+
en = n, // expected-error {{enumerator value is not a constant expression}} cxx11_20-note {{initializer of 'n' is unknown}}
1475+
eo = (m + // pre-cxx23-error {{not a constant expression}}
1476+
n // cxx11_20-note {{initializer of 'n' is unknown}} cxx23-error {{not a constant expression}}
14771477
),
14781478
eq = reinterpret_cast<long>((int*)0) // expected-error {{not a constant expression}} expected-note {{reinterpret_cast}}
14791479
};

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ void splash(Swim& swam) {
4747
}
4848

4949
extern Swim dc;
50-
extern Swim& trident; // expected-note {{declared here}}
50+
extern Swim& trident;
5151

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

5655
namespace explicitThis {
5756
struct C {

0 commit comments

Comments
 (0)