@@ -428,7 +428,19 @@ StmtResult Sema::BuildNonEnumeratingCXXExpansionStmtPattern(
428428 if (DD->isInvalidDecl ())
429429 return StmtError ();
430430
431- ExprResult Select = BuildCXXDestructuringExpansionSelectExpr (DD, Index);
431+ // Synthesise an InitListExpr to store DREs to the BindingDecls; this
432+ // essentially lets us desugar the expansion of a destructuring expansion
433+ // statement to that of an enumerating expansion statement.
434+ SmallVector<Expr *> Bindings;
435+ for (BindingDecl *BD : DD->bindings ()) {
436+ auto *HVD = BD->getHoldingVar ();
437+ Bindings.push_back (HVD ? HVD->getInit () : BD->getBinding ());
438+ }
439+
440+ ExprResult Select = BuildCXXExpansionSelectExpr (
441+ new (Context) InitListExpr (Context, ColonLoc, Bindings, ColonLoc),
442+ Index);
443+
432444 if (Select.isInvalid ()) {
433445 ActOnInitializerError (ExpansionVar);
434446 return StmtError ();
@@ -480,6 +492,8 @@ StmtResult Sema::FinishCXXExpansionStmt(Stmt *Exp, Stmt *Body) {
480492 Shared.push_back (Expansion->getEndVarStmt ());
481493 } else if (Expansion->isDestructuring ()) {
482494 Shared.push_back (Expansion->getDecompositionDeclStmt ());
495+ MarkAnyDeclReferenced (Exp->getBeginLoc (), Expansion->getDecompositionDecl (),
496+ true );
483497 }
484498
485499 // Return an empty statement if the range is empty.
@@ -540,23 +554,6 @@ ExprResult Sema::BuildCXXExpansionSelectExpr(InitListExpr *Range, Expr *Idx) {
540554 return Range->getInit (I);
541555}
542556
543- ExprResult Sema::BuildCXXDestructuringExpansionSelectExpr (DecompositionDecl *DD,
544- Expr *Idx) {
545- if (Idx->isValueDependent ())
546- return new (Context) CXXDestructuringExpansionSelectExpr (Context, DD, Idx);
547-
548- Expr::EvalResult ER;
549- if (!Idx->EvaluateAsInt (ER, Context))
550- llvm_unreachable (" Failed to evaluate expansion index" );
551-
552- uint64_t I = ER.Val .getInt ().getZExtValue ();
553- MarkAnyDeclReferenced (Idx->getBeginLoc (), DD, true );
554- if (auto *BD = DD->bindings ()[I]; auto *HVD = BD->getHoldingVar ())
555- return HVD->getInit ();
556- else
557- return BD->getBinding ();
558- }
559-
560557std::optional<uint64_t >
561558Sema::ComputeExpansionSize (CXXExpansionStmtPattern *Expansion) {
562559 assert (!HasDependentSize (Expansion));
0 commit comments