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. --- src/dotty/tools/dotc/ast/Desugar.scala | 16 +++++++++++----- src/dotty/tools/dotc/typer/Typer.scala | 6 +++++- 2 files changed, 16 insertions(+), 6 deletions(-) (limited to 'src/dotty') diff --git a/src/dotty/tools/dotc/ast/Desugar.scala b/src/dotty/tools/dotc/ast/Desugar.scala index d1f101283..fe6e48aa8 100644 --- a/src/dotty/tools/dotc/ast/Desugar.scala +++ b/src/dotty/tools/dotc/ast/Desugar.scala @@ -556,14 +556,20 @@ object desugar { DefDef(nme.ANON_FUN, Nil, params :: Nil, tpt, body).withMods(synthetic), Closure(Nil, Ident(nme.ANON_FUN), EmptyTree)) - /** Expand partial function + /** If `nparams` == 1, expand partial function + * * { cases } * ==> - * x$0 => x$0 match { cases } + * x$1 => x$1 match { cases } + * + * If `nparams` != 1, expand instead to + * + * (x$0, ..., x${n-1}) => (x$0, ..., x${n-1}) match { cases } */ - def makeCaseLambda(cases: List[CaseDef])(implicit ctx: Context) = { - val param = makeSyntheticParameter() - Function(param :: Nil, Match(Ident(param.name), cases)) + def makeCaseLambda(cases: List[CaseDef], nparams: Int = 1)(implicit ctx: Context) = { + val params = (1 to nparams).toList.map(makeSyntheticParameter(_)) + val selector = makeTuple(params.map(p => Ident(p.name))) + Function(params, Match(selector, cases)) } /** Add annotation with class `cls` to tree: diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index 52ea32bbc..4c07e07de 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -629,7 +629,11 @@ 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 => - typed(desugar.makeCaseLambda(tree.cases) withPos tree.pos, pt) + val arity = pt match { + case defn.FunctionType(args, _) => args.length + case _ => 1 + } + typed(desugar.makeCaseLambda(tree.cases, arity) withPos tree.pos, pt) case _ => val sel1 = typedExpr(tree.selector) val selType = widenForMatchSelector( -- cgit v1.2.3