Skip to content

Commit f07fcd8

Browse files
committed
Auto-uncurry n-ary functions.
Implements SIP scala#897.
1 parent b80b179 commit f07fcd8

File tree

5 files changed

+87
-19
lines changed

5 files changed

+87
-19
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,25 @@ object desugar {
572572
Function(params, Match(selector, cases))
573573
}
574574

575+
/** Map n-ary function `(p1, ..., pn) => body` where n != 1 to unary function as follows:
576+
*
577+
* x$1 => {
578+
* val p1 = x$1._1
579+
* ...
580+
* val pn = x$1._n
581+
* body
582+
* }
583+
*/
584+
def makeUnaryCaseLambda(params: List[ValDef], body: Tree)(implicit ctx: Context): Tree = {
585+
val param = makeSyntheticParameter()
586+
def selector(n: Int) = Select(refOfDef(param), nme.selectorName(n))
587+
val vdefs =
588+
params.zipWithIndex.map{
589+
case(param, idx) => cpy.ValDef(param)(rhs = selector(idx))
590+
}
591+
Function(param :: Nil, Block(vdefs, body))
592+
}
593+
575594
/** Add annotation with class `cls` to tree:
576595
* tree @cls
577596
*/

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

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -581,26 +581,44 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
581581
if (protoFormals.length == params.length) protoFormals(i)
582582
else errorType(i"wrong number of parameters, expected: ${protoFormals.length}", tree.pos)
583583

584-
val inferredParams: List[untpd.ValDef] =
585-
for ((param, i) <- params.zipWithIndex) yield
586-
if (!param.tpt.isEmpty) param
587-
else cpy.ValDef(param)(
588-
tpt = untpd.TypeTree(
589-
inferredParamType(param, protoFormal(i)).underlyingIfRepeated(isJava = false)))
590-
591-
// Define result type of closure as the expected type, thereby pushing
592-
// down any implicit searches. We do this even if the expected type is not fully
593-
// defined, which is a bit of a hack. But it's needed to make the following work
594-
// (see typers.scala and printers/PlainPrinter.scala for examples).
595-
//
596-
// def double(x: Char): String = s"$x$x"
597-
// "abc" flatMap double
598-
//
599-
val resultTpt = protoResult match {
600-
case WildcardType(_) => untpd.TypeTree()
601-
case _ => untpd.TypeTree(protoResult)
584+
/** Is `formal` a product type which is elementwise compatible with `params`? */
585+
def ptIsCorrectProduct(formal: Type) = {
586+
val pclass = defn.ProductNClass(params.length)
587+
isFullyDefined(formal, ForceDegree.noBottom) &&
588+
formal.derivesFrom(pclass) &&
589+
formal.baseArgTypes(pclass).corresponds(params) {
590+
(argType, param) =>
591+
param.tpt.isEmpty || isCompatible(argType, typedAheadType(param.tpt).tpe)
592+
}
602593
}
603-
typed(desugar.makeClosure(inferredParams, fnBody, resultTpt), pt)
594+
595+
val desugared =
596+
if (protoFormals.length == 1 && params.length != 1 && ptIsCorrectProduct(protoFormals.head)) {
597+
desugar.makeUnaryCaseLambda(params, fnBody)
598+
}
599+
else {
600+
val inferredParams: List[untpd.ValDef] =
601+
for ((param, i) <- params.zipWithIndex) yield
602+
if (!param.tpt.isEmpty) param
603+
else cpy.ValDef(param)(
604+
tpt = untpd.TypeTree(
605+
inferredParamType(param, protoFormal(i)).underlyingIfRepeated(isJava = false)))
606+
607+
// Define result type of closure as the expected type, thereby pushing
608+
// down any implicit searches. We do this even if the expected type is not fully
609+
// defined, which is a bit of a hack. But it's needed to make the following work
610+
// (see typers.scala and printers/PlainPrinter.scala for examples).
611+
//
612+
// def double(x: Char): String = s"$x$x"
613+
// "abc" flatMap double
614+
//
615+
val resultTpt = protoResult match {
616+
case WildcardType(_) => untpd.TypeTree()
617+
case _ => untpd.TypeTree(protoResult)
618+
}
619+
desugar.makeClosure(inferredParams, fnBody, resultTpt)
620+
}
621+
typed(desugared, pt)
604622
}
605623
}
606624

test/dotc/tests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class tests extends CompilerTest {
102102

103103
@Test def neg_abstractOverride() = compileFile(negDir, "abstract-override", xerrors = 2)
104104
@Test def neg_blockescapes() = compileFile(negDir, "blockescapesNeg", xerrors = 1)
105+
@Test def neg_functionArity() = compileFile(negDir, "function-arity", xerrors = 5)
105106
@Test def neg_typedapply() = compileFile(negDir, "typedapply", xerrors = 4)
106107
@Test def neg_typedidents() = compileFile(negDir, "typedIdents", xerrors = 2)
107108
@Test def neg_assignments() = compileFile(negDir, "assignments", xerrors = 3)

tests/neg/function-arity.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
object Test {
2+
3+
// From #873:
4+
5+
trait X extends Function1[Int, String]
6+
implicit def f2x(f: Function1[Int, String]): X = ???
7+
({case _ if "".isEmpty => 0} : X) // error: expected String, found Int
8+
9+
// Tests where parameter list cannot be made into a pattern
10+
11+
def unary[T](x: T => Unit) = ???
12+
unary((x, y) => ()) // error
13+
14+
unary[(Int, Int)]((x, y) => ())
15+
16+
unary[(Int, Int)](() => ()) // error
17+
unary[(Int, Int)]((x, y, _) => ()) // error
18+
19+
unary[(Int, Int)]((x: String, y) => ()) // error
20+
21+
22+
}

tests/pos/i873.scala renamed to tests/pos/function-arity.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,12 @@ object Test {
77
({case _ if "".isEmpty => ""} : X) // allowed, implicit view used to adapt
88

99
// ({case _ if "".isEmpty => 0} : X) // expected String, found Int
10+
11+
def unary[T](a: T, b: T, f: ((T, T)) => T): T = f((a, b))
12+
unary(1, 2, (x, y) => x)
13+
unary(1, 2, (x: Int, y) => x)
14+
unary(1, 2, (x: Int, y: Float) => x)
15+
16+
val xs = List(1, 2, 3)
17+
xs.zipWithIndex.map(_ + _)
1018
}

0 commit comments

Comments
 (0)