Skip to content

[clang][bytecode] Don't ignore discarded ArraySubScriptExprs #137526

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
Apr 27, 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: 5 additions & 4 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1695,9 +1695,6 @@ bool Compiler<Emitter>::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;
Expand Down Expand Up @@ -1728,7 +1725,11 @@ bool Compiler<Emitter>::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 <class Emitter>
Expand Down
14 changes: 11 additions & 3 deletions clang/test/AST/ByteCode/arrays.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
}