@@ -418,8 +418,9 @@ trait TypeAssigner {
418
418
tree.withType(avoidingType(expansion, bindings))
419
419
420
420
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)
423
424
}
424
425
425
426
def assignType (tree : untpd.Closure , meth : Tree , target : Tree )(implicit ctx : Context ) =
@@ -433,11 +434,11 @@ trait TypeAssigner {
433
434
def assignType (tree : untpd.Match , cases : List [CaseDef ])(implicit ctx : Context ) = {
434
435
if (tree.selector.typeOpt.isPhantom)
435
436
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)
439
439
}
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)
441
442
}
442
443
443
444
def assignType (tree : untpd.Return )(implicit ctx : Context ) =
@@ -545,9 +546,10 @@ trait TypeAssigner {
545
546
def assignType (tree : untpd.PackageDef , pid : Tree )(implicit ctx : Context ) =
546
547
tree.withType(pid.symbol.valRef)
547
548
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
551
553
}
552
554
553
555
}
0 commit comments