From f0e54c59520566b8d9d2b4dae8a4802de6b3a843 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 30 Oct 2015 09:53:58 +0100 Subject: Allow pattern matching anonymous functions of arity > 1 This is sepcified in Sec. 8.5 of the SLS. Fixes #873. Review by @smarter. --- tests/pos/i873.scala | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 tests/pos/i873.scala (limited to 'tests/pos') diff --git a/tests/pos/i873.scala b/tests/pos/i873.scala new file mode 100644 index 000000000..71c8a3959 --- /dev/null +++ b/tests/pos/i873.scala @@ -0,0 +1,4 @@ +object Test { + def call(k: (Int, Int) => Unit): Unit = ??? + def test = call({ case (x, y) => ()}) +} -- cgit v1.2.3 From b80b179d6fbb92c8f6ff3616cec1f3aab5106799 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 30 Oct 2015 19:11:19 +0100 Subject: Also handle SAM functions when adaptiing arity of case lambdas. --- src/dotty/tools/dotc/typer/Typer.scala | 27 +++++++++++++-------------- tests/pos/i873.scala | 6 ++++++ 2 files changed, 19 insertions(+), 14 deletions(-) (limited to 'tests/pos') diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index 4c07e07de..15ce59c8b 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -502,6 +502,16 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit assignType(cpy.If(tree)(cond1, thenp2, elsep2), thenp2, elsep2) } + private def decomposeProtoFunction(pt: Type, defaultArity: Int)(implicit ctx: Context): (List[Type], Type) = pt match { + case _ if defn.isFunctionType(pt) => + (pt.dealias.argInfos.init, pt.dealias.argInfos.last) + case SAMType(meth) => + val mt @ MethodType(_, paramTypes) = meth.info + (paramTypes, mt.resultType) + case _ => + (List.range(0, defaultArity) map alwaysWildcardType, WildcardType) + } + def typedFunction(tree: untpd.Function, pt: Type)(implicit ctx: Context) = track("typedFunction") { val untpd.Function(args, body) = tree if (ctx.mode is Mode.Type) @@ -509,15 +519,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit untpd.TypeTree(defn.FunctionClass(args.length).typeRef), args :+ body), pt) else { val params = args.asInstanceOf[List[untpd.ValDef]] - val (protoFormals, protoResult): (List[Type], Type) = pt match { - case _ if defn.isFunctionType(pt) => - (pt.dealias.argInfos.init, pt.dealias.argInfos.last) - case SAMType(meth) => - val mt @ MethodType(_, paramTypes) = meth.info - (paramTypes, mt.resultType) - case _ => - (params map alwaysWildcardType, WildcardType) - } + val (protoFormals, protoResult) = decomposeProtoFunction(pt, params.length) def refersTo(arg: untpd.Tree, param: untpd.ValDef): Boolean = arg match { case Ident(name) => name == param.name @@ -629,11 +631,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit def typedMatch(tree: untpd.Match, pt: Type)(implicit ctx: Context) = track("typedMatch") { tree.selector match { case EmptyTree => - val arity = pt match { - case defn.FunctionType(args, _) => args.length - case _ => 1 - } - typed(desugar.makeCaseLambda(tree.cases, arity) withPos tree.pos, pt) + val (protoFormals, _) = decomposeProtoFunction(pt, 1) + typed(desugar.makeCaseLambda(tree.cases, protoFormals.length) withPos tree.pos, pt) case _ => val sel1 = typedExpr(tree.selector) val selType = widenForMatchSelector( diff --git a/tests/pos/i873.scala b/tests/pos/i873.scala index 71c8a3959..94f8d2c67 100644 --- a/tests/pos/i873.scala +++ b/tests/pos/i873.scala @@ -1,4 +1,10 @@ object Test { def call(k: (Int, Int) => Unit): Unit = ??? def test = call({ case (x, y) => ()}) + + trait X extends Function1[Int, String] + implicit def f2x(f: Function1[Int, String]): X = ??? + ({case _ if "".isEmpty => ""} : X) // allowed, implicit view used to adapt + + // ({case _ if "".isEmpty => 0} : X) // expected String, found Int } -- cgit v1.2.3