Skip to content

Commit

Permalink
Fix untupling of functions in for comprehensions
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneFlesselle committed Feb 5, 2024
1 parent 5c628d9 commit e2b5229
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1621,15 +1621,16 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
case untpd.Annotated(scrut1, _) => isParamRef(scrut1)
case untpd.Ident(id) => id == params.head.name
fnBody match
case untpd.Match(scrut, untpd.CaseDef(untpd.Tuple(elems), untpd.EmptyTree, rhs) :: Nil)
case untpd.Match(scrut, cases @ untpd.CaseDef(untpd.Tuple(elems), untpd.EmptyTree, rhs) :: Nil)
if scrut.span.isSynthetic && isParamRef(scrut) && elems.hasSameLengthAs(protoFormals) =>
// If `pt` is N-ary function type, convert synthetic lambda
// x$1 => x$1 match case (a1, ..., aN) => e
// to
// (a1, ..., aN) => e
val params1 = desugar.patternsToParams(elems)
if params1.hasSameLengthAs(elems) then
desugared = cpy.Function(tree)(params1, rhs)
desugared = if params1.hasSameLengthAs(elems)
then cpy.Function(tree)(params1, rhs)
else desugar.makeCaseLambda(cases, desugar.MatchCheck.IrrefutablePatDef, protoFormals.length)
case _ =>

if desugared.isEmpty then
Expand Down
6 changes: 6 additions & 0 deletions tests/pos/i19576.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

object Test:
val z = Seq(0 -> 1, 2 -> 3).lazyZip(Seq("A", "B"))
for case ((beg, end), c) <- z yield c // Ok: a withFilter is inserted before map
for (range, c) <- z yield c // Ok: exact shape
for ((beg, end), c) <- z yield c // Error before changes: Wrong number of parameters, expected 2

0 comments on commit e2b5229

Please sign in to comment.