Skip to content

Commit 8e2f92a

Browse files
authored
Fix untupling of functions in for comprehensions (#19620)
2 parents 98efdab + e2b5229 commit 8e2f92a

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,15 +1625,16 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
16251625
case untpd.Annotated(scrut1, _) => isParamRef(scrut1)
16261626
case untpd.Ident(id) => id == params.head.name
16271627
fnBody match
1628-
case untpd.Match(scrut, untpd.CaseDef(untpd.Tuple(elems), untpd.EmptyTree, rhs) :: Nil)
1628+
case untpd.Match(scrut, cases @ untpd.CaseDef(untpd.Tuple(elems), untpd.EmptyTree, rhs) :: Nil)
16291629
if scrut.span.isSynthetic && isParamRef(scrut) && elems.hasSameLengthAs(protoFormals) =>
16301630
// If `pt` is N-ary function type, convert synthetic lambda
16311631
// x$1 => x$1 match case (a1, ..., aN) => e
16321632
// to
16331633
// (a1, ..., aN) => e
16341634
val params1 = desugar.patternsToParams(elems)
1635-
if params1.hasSameLengthAs(elems) then
1636-
desugared = cpy.Function(tree)(params1, rhs)
1635+
desugared = if params1.hasSameLengthAs(elems)
1636+
then cpy.Function(tree)(params1, rhs)
1637+
else desugar.makeCaseLambda(cases, desugar.MatchCheck.IrrefutablePatDef, protoFormals.length)
16371638
case _ =>
16381639

16391640
if desugared.isEmpty then

tests/pos/i19576.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
object Test:
3+
val z = Seq(0 -> 1, 2 -> 3).lazyZip(Seq("A", "B"))
4+
for case ((beg, end), c) <- z yield c // Ok: a withFilter is inserted before map
5+
for (range, c) <- z yield c // Ok: exact shape
6+
for ((beg, end), c) <- z yield c // Error before changes: Wrong number of parameters, expected 2

0 commit comments

Comments
 (0)