Skip to content

Commit e83bea4

Browse files
committed
[clang][Interp][NFC] Add some tests for invalid array access
1 parent 7349bc3 commit e83bea4

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

clang/test/AST/Interp/arrays.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,22 @@ struct fred {
9898
struct fred y [] = { [0] = { .s[0] = 'q' } };
9999
#endif
100100
#pragma clang diagnostic pop
101+
102+
namespace indices {
103+
constexpr int first[] = {1};
104+
constexpr int firstValue = first[2]; // ref-error {{must be initialized by a constant expression}} \
105+
// ref-note {{cannot refer to element 2 of array of 1}} \
106+
// expected-error {{must be initialized by a constant expression}} \
107+
// expected-note {{cannot refer to element 2 of array of 1}}
108+
109+
constexpr int second[10] = {17};
110+
constexpr int secondValue = second[10];// ref-error {{must be initialized by a constant expression}} \
111+
// ref-note {{read of dereferenced one-past-the-end pointer}} \
112+
// expected-error {{must be initialized by a constant expression}} \
113+
// expected-note {{read of dereferenced one-past-the-end pointer}}
114+
115+
constexpr int negative = second[-2]; // ref-error {{must be initialized by a constant expression}} \
116+
// ref-note {{cannot refer to element -2 of array of 10}} \
117+
// expected-error {{must be initialized by a constant expression}} \
118+
// expected-note {{cannot refer to element -2 of array of 10}}
119+
};

0 commit comments

Comments
 (0)