From 14096e3601e42fd33fb2446b908a5cfce3cf1fa9 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 19 Feb 2016 15:47:44 +0100 Subject: Refactoring of typedTyped Goal: Make implementation easier to understand. Prepare the ground for special-casing of typetagged patterns. --- src/dotty/tools/dotc/typer/Typer.scala | 48 ++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 20 deletions(-) (limited to 'src/dotty/tools/dotc/typer/Typer.scala') diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index 1e8e3a524..fabee83b7 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -400,30 +400,38 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit } def typedTyped(tree: untpd.Typed, pt: Type)(implicit ctx: Context): Tree = track("typedTyped") { - def regularTyped(isWildcard: Boolean) = { - val tpt1 = - if (untpd.isWildcardStarArg(tree)) - TypeTree(defn.SeqType.appliedTo(pt :: Nil)) - else - checkSimpleKinded(typedType(tree.tpt)) - val expr1 = - if (isWildcard) tree.expr withType tpt1.tpe - else typed(tree.expr, tpt1.tpe.widenSkolem) - assignType(cpy.Typed(tree)(expr1, tpt1), tpt1) - } - tree.expr match { + /* Handles three cases: + * @param ifPat how to handle a pattern (_: T) + * @param ifExpr how to handle an expression (e: T) + * @param wildName what name `w` to use in the rewriting of + * (x: T) to (x @ (w: T)). This is either `_` or `_*`. + */ + def cases(ifPat: => Tree, ifExpr: => Tree, wildName: TermName) = tree.expr match { case id: untpd.Ident if (ctx.mode is Mode.Pattern) && isVarPattern(id) => - if (id.name == nme.WILDCARD || id.name == nme.WILDCARD_STAR) regularTyped(isWildcard = true) + if (id.name == nme.WILDCARD || id.name == nme.WILDCARD_STAR) ifPat else { import untpd._ - val name = if (untpd.isWildcardStarArg(tree)) nme.WILDCARD_STAR else nme.WILDCARD - typed(Bind(id.name, Typed(Ident(name), tree.tpt)).withPos(id.pos), pt) + typed(Bind(id.name, Typed(Ident(wildName), tree.tpt)).withPos(id.pos), pt) } - case _ => - if (untpd.isWildcardStarArg(tree)) - seqToRepeated(typedExpr(tree.expr, defn.SeqType)) - else - regularTyped(isWildcard = false) + case _ => ifExpr + } + def ascription(tpt: Tree, isWildcard: Boolean) = { + val expr1 = + if (isWildcard) tree.expr.withType(tpt.tpe) + else typed(tree.expr, tpt.tpe.widenSkolem) + assignType(cpy.Typed(tree)(expr1, tpt), tpt) + } + if (untpd.isWildcardStarArg(tree)) + cases( + ifPat = ascription(TypeTree(defn.SeqType.appliedTo(pt :: Nil)), isWildcard = true), + ifExpr = seqToRepeated(typedExpr(tree.expr, defn.SeqType)), + wildName = nme.WILDCARD_STAR) + else { + def tpt1 = checkSimpleKinded(typedType(tree.tpt)) + cases( + ifPat = ascription(tpt1, isWildcard = true), + ifExpr = ascription(tpt1, isWildcard = false), + wildName = nme.WILDCARD) } } -- cgit v1.2.3