Skip to content

Commit b7ba84d

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

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
@@ -399,30 +399,38 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
399399
}
400400

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

0 commit comments

Comments
 (0)