Skip to content

Commit cdf3396

Browse files
tbaederrAnkur-0429
authored andcommitted
[clang][bytecode] Don't ignore discarded ArraySubScriptExprs (llvm#137526)
We need to evaluate them since the index might be out of bounds.
1 parent 1b7afb0 commit cdf3396

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,9 +1695,6 @@ bool Compiler<Emitter>::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
16951695
const Expr *Index = E->getIdx();
16961696
const Expr *Base = E->getBase();
16971697

1698-
if (DiscardResult)
1699-
return this->discard(LHS) && this->discard(RHS);
1700-
17011698
// C++17's rules require us to evaluate the LHS first, regardless of which
17021699
// side is the base.
17031700
bool Success = true;
@@ -1728,7 +1725,11 @@ bool Compiler<Emitter>::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
17281725
return false;
17291726
}
17301727

1731-
return this->emitArrayElemPtrPop(*IndexT, E);
1728+
if (!this->emitArrayElemPtrPop(*IndexT, E))
1729+
return false;
1730+
if (DiscardResult)
1731+
return this->emitPopPtr(E);
1732+
return true;
17321733
}
17331734

17341735
template <class Emitter>

clang/test/AST/ByteCode/arrays.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both %s
2-
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++20 -verify=expected,both %s
3-
// RUN: %clang_cc1 -verify=ref,both %s
1+
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both %s
2+
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both -std=c++20 %s
3+
// RUN: %clang_cc1 -verify=ref,both %s
44
// RUN: %clang_cc1 -verify=ref,both -std=c++20 %s
55

66
constexpr int m = 3;
@@ -771,3 +771,11 @@ namespace OnePastEndDiag {
771771
constexpr int k = a(foo + 2); // both-error {{must be initialized by a constant expression}} \
772772
// both-note {{in call to 'a(&foo[2])'}}
773773
}
774+
775+
namespace DiscardedSubScriptExpr {
776+
constexpr bool foo() { // both-error {{never produces a constant expression}}
777+
int a[2] = {};
778+
(void)a[3]; // both-note {{cannot refer to element 3 of array of 2 elements in a constant expression}}
779+
return true;
780+
}
781+
}

0 commit comments

Comments
 (0)