From e2b52290d8847a63a995154c3f9cb1d3d8c5cb76 Mon Sep 17 00:00:00 2001 From: Eugene Flesselle Date: Sun, 4 Feb 2024 15:37:56 +0100 Subject: [PATCH] Fix untupling of functions in for comprehensions --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 7 ++++--- tests/pos/i19576.scala | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 tests/pos/i19576.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 7727c125d1e4..5648f651d8e9 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -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 diff --git a/tests/pos/i19576.scala b/tests/pos/i19576.scala new file mode 100644 index 000000000000..030107b3c63c --- /dev/null +++ b/tests/pos/i19576.scala @@ -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