diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index 58fe2c184cf3f..a9610835c81e6 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -1695,9 +1695,6 @@ bool Compiler::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { const Expr *Index = E->getIdx(); const Expr *Base = E->getBase(); - if (DiscardResult) - return this->discard(LHS) && this->discard(RHS); - // C++17's rules require us to evaluate the LHS first, regardless of which // side is the base. bool Success = true; @@ -1728,7 +1725,11 @@ bool Compiler::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { return false; } - return this->emitArrayElemPtrPop(*IndexT, E); + if (!this->emitArrayElemPtrPop(*IndexT, E)) + return false; + if (DiscardResult) + return this->emitPopPtr(E); + return true; } template diff --git a/clang/test/AST/ByteCode/arrays.cpp b/clang/test/AST/ByteCode/arrays.cpp index 934c3a3e7aff1..2dd51c2fa6711 100644 --- a/clang/test/AST/ByteCode/arrays.cpp +++ b/clang/test/AST/ByteCode/arrays.cpp @@ -1,6 +1,6 @@ -// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both %s -// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++20 -verify=expected,both %s -// RUN: %clang_cc1 -verify=ref,both %s +// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both %s +// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both -std=c++20 %s +// RUN: %clang_cc1 -verify=ref,both %s // RUN: %clang_cc1 -verify=ref,both -std=c++20 %s constexpr int m = 3; @@ -771,3 +771,11 @@ namespace OnePastEndDiag { constexpr int k = a(foo + 2); // both-error {{must be initialized by a constant expression}} \ // both-note {{in call to 'a(&foo[2])'}} } + +namespace DiscardedSubScriptExpr { + constexpr bool foo() { // both-error {{never produces a constant expression}} + int a[2] = {}; + (void)a[3]; // both-note {{cannot refer to element 3 of array of 2 elements in a constant expression}} + return true; + } +}