@@ -1483,14 +1483,14 @@ object desugar {
14831483 |please bind to an identifier and use an alias given. """ , bind)
14841484 false
14851485
1486- def isTuplePattern ( arity : Int ) : Boolean = pat match {
1487- case Tuple (pats) if pats.size == arity =>
1488- pats.forall(isVarPattern)
1489- case _ => false
1486+ // The arity of the tuple pattern if it only contains simple variables or wildcards.
1487+ val varTuplePatternArity = pat match {
1488+ case Tuple (pats) if pats.forall(isVarPattern) => pats.length
1489+ case _ => - 1
14901490 }
14911491
14921492 val isMatchingTuple : Tree => Boolean = {
1493- case Tuple (es) => isTuplePattern( es.length) && ! hasNamedArg(es)
1493+ case Tuple (es) => varTuplePatternArity == es.length && ! hasNamedArg(es)
14941494 case _ => false
14951495 }
14961496
@@ -1519,10 +1519,28 @@ object desugar {
15191519
15201520 val ids = for ((named, _) <- vars) yield Ident (named.name)
15211521 val matchExpr =
1522- if ( tupleOptimizable) rhs
1522+ if tupleOptimizable then rhs
15231523 else
1524- val caseDef = CaseDef (pat, EmptyTree , makeTuple(ids).withAttachment(ForArtifact , ()))
1524+ val caseDef =
1525+ if varTuplePatternArity >= 0 && ids.length > 1 then
1526+ // If the pattern contains only simple variables or wildcards,
1527+ // we don't need to create a new tuple.
1528+ // If there is only one variable (ids.length == 1),
1529+ // `makeTuple` will optimize it to `Ident(named)`,
1530+ // so we don't need to handle that case here.
1531+ val tmpTuple = UniqueName .fresh()
1532+ // Replace all variables with wildcards in the pattern
1533+ val pat1 = pat match
1534+ case Tuple (pats) =>
1535+ Tuple (pats.map(pat => Ident (nme.WILDCARD ).withSpan(pat.span)))
1536+ CaseDef (
1537+ Bind (tmpTuple, pat1),
1538+ EmptyTree ,
1539+ Ident (tmpTuple).withAttachment(ForArtifact , ())
1540+ )
1541+ else CaseDef (pat, EmptyTree , makeTuple(ids).withAttachment(ForArtifact , ()))
15251542 Match (makeSelector(rhs, MatchCheck .IrrefutablePatDef ), caseDef :: Nil )
1543+
15261544 vars match {
15271545 case Nil if ! mods.is(Lazy ) =>
15281546 matchExpr
0 commit comments