Skip to content

Commit d70f54f

Browse files
authored
[clang][bytecode] Fix reporting failed local constexpr initializers (#123588)
We need to emit the 'initializer of X is not a constant expression' note for local constexpr variables as well.
1 parent 0ec153b commit d70f54f

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,11 @@ bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
416416
AccessKinds AK) {
417417
if (!Ptr.isOnePastEnd())
418418
return true;
419-
const SourceInfo &Loc = S.Current->getSource(OpPC);
420-
S.FFDiag(Loc, diag::note_constexpr_access_past_end)
421-
<< AK << S.Current->getRange(OpPC);
419+
if (S.getLangOpts().CPlusPlus) {
420+
const SourceInfo &Loc = S.Current->getSource(OpPC);
421+
S.FFDiag(Loc, diag::note_constexpr_access_past_end)
422+
<< AK << S.Current->getRange(OpPC);
423+
}
422424
return false;
423425
}
424426

@@ -538,7 +540,7 @@ bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
538540
return true;
539541

540542
if (const auto *VD = Ptr.getDeclDesc()->asVarDecl();
541-
VD && VD->hasGlobalStorage()) {
543+
VD && (VD->isConstexpr() || VD->hasGlobalStorage())) {
542544
const SourceInfo &Loc = S.Current->getSource(OpPC);
543545
if (VD->getAnyInitializer()) {
544546
S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;

clang/test/AST/ByteCode/c23.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ static_assert(arg1[1] == 254);
4949
static_assert(arg1[2] == 186);
5050
static_assert(arg1[3] == 190);
5151
#endif
52+
53+
void ghissue109095() {
54+
constexpr char c[] = { 'a' };
55+
constexpr int i = c[1]; // both-error {{constexpr variable 'i' must be initialized by a constant expression}}\
56+
// both-note {{declared here}}
57+
_Static_assert(i == c[0]); // both-error {{static assertion expression is not an integral constant expression}}\
58+
// both-note {{initializer of 'i' is not a constant expression}}
59+
}

clang/test/AST/ByteCode/literals.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,3 +1315,12 @@ namespace {
13151315
}
13161316
}
13171317
#endif
1318+
1319+
void localConstexpr() {
1320+
constexpr int a = 1/0; // both-error {{must be initialized by a constant expression}} \
1321+
// both-note {{division by zero}} \
1322+
// both-warning {{division by zero is undefined}} \
1323+
// both-note {{declared here}}
1324+
static_assert(a == 0, ""); // both-error {{not an integral constant expression}} \
1325+
// both-note {{initializer of 'a' is not a constant expression}}
1326+
}

0 commit comments

Comments
 (0)