@@ -1483,14 +1483,14 @@ object desugar {
1483
1483
|please bind to an identifier and use an alias given. """ , bind)
1484
1484
false
1485
1485
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
1490
1490
}
1491
1491
1492
1492
val isMatchingTuple : Tree => Boolean = {
1493
- case Tuple (es) => isTuplePattern( es.length) && ! hasNamedArg(es)
1493
+ case Tuple (es) => varTuplePatternArity == es.length && ! hasNamedArg(es)
1494
1494
case _ => false
1495
1495
}
1496
1496
@@ -1519,10 +1519,28 @@ object desugar {
1519
1519
1520
1520
val ids = for ((named, _) <- vars) yield Ident (named.name)
1521
1521
val matchExpr =
1522
- if ( tupleOptimizable) rhs
1522
+ if tupleOptimizable then rhs
1523
1523
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 , ()))
1525
1542
Match (makeSelector(rhs, MatchCheck .IrrefutablePatDef ), caseDef :: Nil )
1543
+
1526
1544
vars match {
1527
1545
case Nil if ! mods.is(Lazy ) =>
1528
1546
matchExpr
0 commit comments