Skip to content

Commit ea1dee7

Browse files
authored
[clang][Interp] Don't diagnose indexing arrays with index 0 (#102454)
Even if the array is of unknown bounds, index 0 is fine.
1 parent b8c560f commit ea1dee7

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

clang/lib/AST/Interp/Interp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,7 +2421,7 @@ inline bool ArrayElemPtr(InterpState &S, CodePtr OpPC) {
24212421
const T &Offset = S.Stk.pop<T>();
24222422
const Pointer &Ptr = S.Stk.peek<Pointer>();
24232423

2424-
if (!Ptr.isZero()) {
2424+
if (!Ptr.isZero() && !Offset.isZero()) {
24252425
if (!CheckArray(S, OpPC, Ptr))
24262426
return false;
24272427
}
@@ -2437,7 +2437,7 @@ inline bool ArrayElemPtrPop(InterpState &S, CodePtr OpPC) {
24372437
const T &Offset = S.Stk.pop<T>();
24382438
const Pointer &Ptr = S.Stk.pop<Pointer>();
24392439

2440-
if (!Ptr.isZero()) {
2440+
if (!Ptr.isZero() && !Offset.isZero()) {
24412441
if (!CheckArray(S, OpPC, Ptr))
24422442
return false;
24432443
}

clang/test/AST/Interp/literals.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,13 +1196,15 @@ namespace incdecbool {
11961196
}
11971197

11981198
#if __cplusplus >= 201402L
1199-
/// NOTE: The diagnostics of the two interpreters are a little
1200-
/// different here, but they both make sense.
12011199
constexpr int externvar1() { // both-error {{never produces a constant expression}}
1202-
extern char arr[]; // ref-note {{declared here}}
1203-
return arr[0]; // ref-note {{read of non-constexpr variable 'arr'}} \
1204-
// expected-note {{indexing of array without known bound}}
1200+
extern char arr[]; // both-note {{declared here}}
1201+
return arr[0]; // both-note {{read of non-constexpr variable 'arr'}}
12051202
}
1203+
namespace externarr {
1204+
extern int arr[];
1205+
constexpr int *externarrindex = &arr[0]; /// No diagnostic.
1206+
}
1207+
12061208

12071209
namespace StmtExprs {
12081210
constexpr int foo() {

0 commit comments

Comments
 (0)