Skip to content

Commit 19a39e9

Browse files
authored
[clang][bytecode] Handle non-primitive array index expressions (#128479)
By rejecting them instead of asserting in `classifyPrim()`.
1 parent 7cda365 commit 19a39e9

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,14 +1689,17 @@ bool Compiler<Emitter>::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
16891689
if (!Success)
16901690
return false;
16911691

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

1699-
return this->emitArrayElemPtrPop(IndexT, E);
1702+
return this->emitArrayElemPtrPop(*IndexT, E);
17001703
}
17011704

17021705
template <class Emitter>

clang/test/AST/ByteCode/arrays.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,3 +654,11 @@ namespace ZeroSizeTypes {
654654
// both-warning {{subtraction of pointers to type 'int[0]' of zero size has undefined behavior}}
655655
}
656656
}
657+
658+
namespace InvalidIndex {
659+
constexpr int foo(int i) { // both-error {{no return statement in constexpr function}}
660+
int a[] = {1,2,3};
661+
return a[_z]; // both-error {{use of undeclared identifier}}
662+
}
663+
static_assert(foo(0) == 1, "");
664+
}

0 commit comments

Comments
 (0)