diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp index 77fe0412d0dc5..8fbcc9ae3eec9 100644 --- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp +++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp @@ -467,8 +467,13 @@ bool ByteCodeExprGen::VisitArraySubscriptExpr( template bool ByteCodeExprGen::VisitInitListExpr(const InitListExpr *E) { for (const Expr *Init : E->inits()) { - if (!this->visit(Init)) - return false; + if (DiscardResult) { + if (!this->discard(Init)) + return false; + } else { + if (!this->visit(Init)) + return false; + } } return true; } @@ -944,12 +949,16 @@ bool ByteCodeExprGen::VisitCompoundLiteralExpr( // Otherwise, use a local variable. if (T) { // For primitive types, we just visit the initializer. - return this->visit(Init); + return DiscardResult ? this->discard(Init) : this->visit(Init); } else { if (std::optional LocalIndex = allocateLocal(Init)) { if (!this->emitGetPtrLocal(*LocalIndex, E)) return false; - return this->visitInitializer(Init); + if (!this->visitInitializer(Init)) + return false; + if (DiscardResult) + return this->emitPopPtr(E); + return true; } } diff --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp index 7cb01a80c8685..d4ade51b1d71c 100644 --- a/clang/test/AST/Interp/literals.cpp +++ b/clang/test/AST/Interp/literals.cpp @@ -897,9 +897,26 @@ namespace DiscardExprs { 1 ? 0 : 1; __is_trivial(int); + (int){1}; + (int[]){1,2,3}; + return 0; } + constexpr int oh_my(int x) { + (int){ x++ }; + return x; + } + static_assert(oh_my(0) == 1, ""); + + constexpr int oh_my2(int x) { + int y{x++}; + return x; + } + + static_assert(oh_my2(0) == 1, ""); + + /// Ignored comma expressions still have their /// expressions evaluated. constexpr int Comma(int start) {