Skip to content

Commit 14096e3

Browse files
committed
Refactoring of typedTyped
Goal: Make implementation easier to understand. Prepare the ground for special-casing of typetagged patterns.
1 parent c6064ed commit 14096e3

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

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

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -400,30 +400,38 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
400400
}
401401

402402
def typedTyped(tree: untpd.Typed, pt: Type)(implicit ctx: Context): Tree = track("typedTyped") {
403-
def regularTyped(isWildcard: Boolean) = {
404-
val tpt1 =
405-
if (untpd.isWildcardStarArg(tree))
406-
TypeTree(defn.SeqType.appliedTo(pt :: Nil))
407-
else
408-
checkSimpleKinded(typedType(tree.tpt))
409-
val expr1 =
410-
if (isWildcard) tree.expr withType tpt1.tpe
411-
else typed(tree.expr, tpt1.tpe.widenSkolem)
412-
assignType(cpy.Typed(tree)(expr1, tpt1), tpt1)
413-
}
414-
tree.expr match {
403+
/* Handles three cases:
404+
* @param ifPat how to handle a pattern (_: T)
405+
* @param ifExpr how to handle an expression (e: T)
406+
* @param wildName what name `w` to use in the rewriting of
407+
* (x: T) to (x @ (w: T)). This is either `_` or `_*`.
408+
*/
409+
def cases(ifPat: => Tree, ifExpr: => Tree, wildName: TermName) = tree.expr match {
415410
case id: untpd.Ident if (ctx.mode is Mode.Pattern) && isVarPattern(id) =>
416-
if (id.name == nme.WILDCARD || id.name == nme.WILDCARD_STAR) regularTyped(isWildcard = true)
411+
if (id.name == nme.WILDCARD || id.name == nme.WILDCARD_STAR) ifPat
417412
else {
418413
import untpd._
419-
val name = if (untpd.isWildcardStarArg(tree)) nme.WILDCARD_STAR else nme.WILDCARD
420-
typed(Bind(id.name, Typed(Ident(name), tree.tpt)).withPos(id.pos), pt)
414+
typed(Bind(id.name, Typed(Ident(wildName), tree.tpt)).withPos(id.pos), pt)
421415
}
422-
case _ =>
423-
if (untpd.isWildcardStarArg(tree))
424-
seqToRepeated(typedExpr(tree.expr, defn.SeqType))
425-
else
426-
regularTyped(isWildcard = false)
416+
case _ => ifExpr
417+
}
418+
def ascription(tpt: Tree, isWildcard: Boolean) = {
419+
val expr1 =
420+
if (isWildcard) tree.expr.withType(tpt.tpe)
421+
else typed(tree.expr, tpt.tpe.widenSkolem)
422+
assignType(cpy.Typed(tree)(expr1, tpt), tpt)
423+
}
424+
if (untpd.isWildcardStarArg(tree))
425+
cases(
426+
ifPat = ascription(TypeTree(defn.SeqType.appliedTo(pt :: Nil)), isWildcard = true),
427+
ifExpr = seqToRepeated(typedExpr(tree.expr, defn.SeqType)),
428+
wildName = nme.WILDCARD_STAR)
429+
else {
430+
def tpt1 = checkSimpleKinded(typedType(tree.tpt))
431+
cases(
432+
ifPat = ascription(tpt1, isWildcard = true),
433+
ifExpr = ascription(tpt1, isWildcard = false),
434+
wildName = nme.WILDCARD)
427435
}
428436
}
429437

0 commit comments

Comments
 (0)