Skip to content

Commit 1729676

Browse files
committed
Untuple using def not val.
As retronym noted on #897, `val` forces to early.
1 parent 06bfbd3 commit 1729676

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,18 +591,19 @@ object desugar {
591591
/** Map n-ary function `(p1, ..., pn) => body` where n != 1 to unary function as follows:
592592
*
593593
* x$1 => {
594-
* val p1 = x$1._1
594+
* def p1 = x$1._1
595595
* ...
596-
* val pn = x$1._n
596+
* def pn = x$1._n
597597
* body
598598
* }
599599
*/
600-
def makeUnaryCaseLambda(params: List[ValDef], body: Tree)(implicit ctx: Context): Tree = {
600+
def makeTupledFunction(params: List[ValDef], body: Tree)(implicit ctx: Context): Tree = {
601601
val param = makeSyntheticParameter()
602602
def selector(n: Int) = Select(refOfDef(param), nme.selectorName(n))
603603
val vdefs =
604604
params.zipWithIndex.map{
605-
case(param, idx) => cpy.ValDef(param)(rhs = selector(idx))
605+
case (param, idx) =>
606+
DefDef(param.name, Nil, Nil, TypeTree(), selector(idx)).withPos(param.pos)
606607
}
607608
Function(param :: Nil, Block(vdefs, body))
608609
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
624624

625625
val desugared =
626626
if (protoFormals.length == 1 && params.length != 1 && ptIsCorrectProduct(protoFormals.head)) {
627-
desugar.makeUnaryCaseLambda(params, fnBody)
627+
desugar.makeTupledFunction(params, fnBody)
628628
}
629629
else {
630630
val inferredParams: List[untpd.ValDef] =

tests/run/function-arity.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object Test {
2+
class T[A] { def foo(f: (=> A) => Int) = f(???) }
3+
4+
def main(args: Array[String]): Unit = {
5+
new T[(Int, Int)].foo((ii) => 0)
6+
new T[(Int, Int)].foo((x, y) => 0) // check that this does not run into ???
7+
}
8+
}

0 commit comments

Comments
 (0)