Skip to content

[clang][bytecode] Handle non-primitive array index expressions #128479

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
Feb 24, 2025
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
9 changes: 6 additions & 3 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1689,14 +1689,17 @@ bool Compiler<Emitter>::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
if (!Success)
return false;

PrimType IndexT = classifyPrim(Index->getType());
std::optional<PrimType> IndexT = classify(Index->getType());
// In error-recovery cases, the index expression has a dependent type.
if (!IndexT)
return this->emitError(E);
// If the index is first, we need to change that.
if (LHS == Index) {
if (!this->emitFlip(PT_Ptr, IndexT, E))
if (!this->emitFlip(PT_Ptr, *IndexT, E))
return false;
}

return this->emitArrayElemPtrPop(IndexT, E);
return this->emitArrayElemPtrPop(*IndexT, E);
}

template <class Emitter>
Expand Down
8 changes: 8 additions & 0 deletions clang/test/AST/ByteCode/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,11 @@ namespace ZeroSizeTypes {
// both-warning {{subtraction of pointers to type 'int[0]' of zero size has undefined behavior}}
}
}

namespace InvalidIndex {
constexpr int foo(int i) { // both-error {{no return statement in constexpr function}}
int a[] = {1,2,3};
return a[_z]; // both-error {{use of undeclared identifier}}
}
static_assert(foo(0) == 1, "");
}