Skip to content

Commit ddbce6d

Browse files
committed
Fix after rebase, if/match missmatch on phantoms.
1 parent 6856c99 commit ddbce6d

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala

+11-9
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,9 @@ trait TypeAssigner {
418418
tree.withType(avoidingType(expansion, bindings))
419419

420420
def assignType(tree: untpd.If, thenp: Tree, elsep: Tree)(implicit ctx: Context) = {
421-
checkSameUniverse(thenp, elsep, "be combined in branches of if/else", tree.pos)
422-
tree.withType(thenp.tpe | elsep.tpe)
421+
val sameUniverse = checkSameUniverse(thenp, elsep, "be combined in branches of if/else", tree.pos)
422+
val tpe = if (sameUniverse) thenp.tpe | elsep.tpe else thenp.tpe
423+
tree.withType(tpe)
423424
}
424425

425426
def assignType(tree: untpd.Closure, meth: Tree, target: Tree)(implicit ctx: Context) =
@@ -433,11 +434,11 @@ trait TypeAssigner {
433434
def assignType(tree: untpd.Match, cases: List[CaseDef])(implicit ctx: Context) = {
434435
if (tree.selector.typeOpt.isPhantom)
435436
ctx.error("Cannot pattern match on phantoms", tree.selector.pos)
436-
if (cases.nonEmpty) {
437-
val head = cases.head
438-
cases.tail.foreach(c => checkSameUniverse(head, c, "be combined in branches of a match", c.pos))
437+
val sameUniverse = cases.isEmpty || cases.tail.forall { c =>
438+
checkSameUniverse(cases.head, c, "be combined in branches of a match", c.pos)
439439
}
440-
tree.withType(ctx.typeComparer.lub(cases.tpes))
440+
val tpe = if (sameUniverse) ctx.typeComparer.lub(cases.tpes) else cases.head.tpe
441+
tree.withType(tpe)
441442
}
442443

443444
def assignType(tree: untpd.Return)(implicit ctx: Context) =
@@ -545,9 +546,10 @@ trait TypeAssigner {
545546
def assignType(tree: untpd.PackageDef, pid: Tree)(implicit ctx: Context) =
546547
tree.withType(pid.symbol.valRef)
547548

548-
private def checkSameUniverse(tree1: Tree, tree2: Tree, relationship: => String, pos: Position)(implicit ctx: Context) = {
549-
if (tree1.tpe.topType != tree2.tpe.topType)
550-
ctx.error(ex"${tree1.tpe} and ${tree2.tpe} are in different universes. They cannot $relationship", pos)
549+
private def checkSameUniverse(tree1: Tree, tree2: Tree, relationship: => String, pos: Position)(implicit ctx: Context): Boolean = {
550+
val sameUniverse = tree1.tpe.topType == tree2.tpe.topType
551+
if (!sameUniverse) ctx.error(ex"${tree1.tpe} and ${tree2.tpe} are in different universes. They cannot $relationship", pos)
552+
sameUniverse
551553
}
552554

553555
}

0 commit comments

Comments
 (0)