Skip to content

[clang][Interp] Don't diagnose indexing arrays with index 0 #102454

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

Merged
merged 1 commit into from
Aug 8, 2024
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
4 changes: 2 additions & 2 deletions clang/lib/AST/Interp/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2421,7 +2421,7 @@ inline bool ArrayElemPtr(InterpState &S, CodePtr OpPC) {
const T &Offset = S.Stk.pop<T>();
const Pointer &Ptr = S.Stk.peek<Pointer>();

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

if (!Ptr.isZero()) {
if (!Ptr.isZero() && !Offset.isZero()) {
if (!CheckArray(S, OpPC, Ptr))
return false;
}
Expand Down
12 changes: 7 additions & 5 deletions clang/test/AST/Interp/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1196,13 +1196,15 @@ namespace incdecbool {
}

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


namespace StmtExprs {
constexpr int foo() {
Expand Down
Loading