Skip to content

Commit f58acd1

Browse files
committed
Merge pull request #887 from dotty-staging/fix-#873
Allow pattern matching anonymous functions of arity > 1
2 parents 21f6711 + b80b179 commit f58acd1

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -556,14 +556,20 @@ object desugar {
556556
DefDef(nme.ANON_FUN, Nil, params :: Nil, tpt, body).withMods(synthetic),
557557
Closure(Nil, Ident(nme.ANON_FUN), EmptyTree))
558558

559-
/** Expand partial function
559+
/** If `nparams` == 1, expand partial function
560+
*
560561
* { cases }
561562
* ==>
562-
* x$0 => x$0 match { cases }
563+
* x$1 => x$1 match { cases }
564+
*
565+
* If `nparams` != 1, expand instead to
566+
*
567+
* (x$1, ..., x$n) => (x$0, ..., x${n-1}) match { cases }
563568
*/
564-
def makeCaseLambda(cases: List[CaseDef])(implicit ctx: Context) = {
565-
val param = makeSyntheticParameter()
566-
Function(param :: Nil, Match(Ident(param.name), cases))
569+
def makeCaseLambda(cases: List[CaseDef], nparams: Int = 1)(implicit ctx: Context) = {
570+
val params = (1 to nparams).toList.map(makeSyntheticParameter(_))
571+
val selector = makeTuple(params.map(p => Ident(p.name)))
572+
Function(params, Match(selector, cases))
567573
}
568574

569575
/** Add annotation with class `cls` to tree:

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -502,22 +502,24 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
502502
assignType(cpy.If(tree)(cond1, thenp2, elsep2), thenp2, elsep2)
503503
}
504504

505+
private def decomposeProtoFunction(pt: Type, defaultArity: Int)(implicit ctx: Context): (List[Type], Type) = pt match {
506+
case _ if defn.isFunctionType(pt) =>
507+
(pt.dealias.argInfos.init, pt.dealias.argInfos.last)
508+
case SAMType(meth) =>
509+
val mt @ MethodType(_, paramTypes) = meth.info
510+
(paramTypes, mt.resultType)
511+
case _ =>
512+
(List.range(0, defaultArity) map alwaysWildcardType, WildcardType)
513+
}
514+
505515
def typedFunction(tree: untpd.Function, pt: Type)(implicit ctx: Context) = track("typedFunction") {
506516
val untpd.Function(args, body) = tree
507517
if (ctx.mode is Mode.Type)
508518
typed(cpy.AppliedTypeTree(tree)(
509519
untpd.TypeTree(defn.FunctionClass(args.length).typeRef), args :+ body), pt)
510520
else {
511521
val params = args.asInstanceOf[List[untpd.ValDef]]
512-
val (protoFormals, protoResult): (List[Type], Type) = pt match {
513-
case _ if defn.isFunctionType(pt) =>
514-
(pt.dealias.argInfos.init, pt.dealias.argInfos.last)
515-
case SAMType(meth) =>
516-
val mt @ MethodType(_, paramTypes) = meth.info
517-
(paramTypes, mt.resultType)
518-
case _ =>
519-
(params map alwaysWildcardType, WildcardType)
520-
}
522+
val (protoFormals, protoResult) = decomposeProtoFunction(pt, params.length)
521523

522524
def refersTo(arg: untpd.Tree, param: untpd.ValDef): Boolean = arg match {
523525
case Ident(name) => name == param.name
@@ -629,7 +631,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
629631
def typedMatch(tree: untpd.Match, pt: Type)(implicit ctx: Context) = track("typedMatch") {
630632
tree.selector match {
631633
case EmptyTree =>
632-
typed(desugar.makeCaseLambda(tree.cases) withPos tree.pos, pt)
634+
val (protoFormals, _) = decomposeProtoFunction(pt, 1)
635+
typed(desugar.makeCaseLambda(tree.cases, protoFormals.length) withPos tree.pos, pt)
633636
case _ =>
634637
val sel1 = typedExpr(tree.selector)
635638
val selType = widenForMatchSelector(

tests/pos/i873.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object Test {
2+
def call(k: (Int, Int) => Unit): Unit = ???
3+
def test = call({ case (x, y) => ()})
4+
5+
trait X extends Function1[Int, String]
6+
implicit def f2x(f: Function1[Int, String]): X = ???
7+
({case _ if "".isEmpty => ""} : X) // allowed, implicit view used to adapt
8+
9+
// ({case _ if "".isEmpty => 0} : X) // expected String, found Int
10+
}

0 commit comments

Comments
 (0)