@@ -417,8 +417,10 @@ trait TypeAssigner {
417
417
def assignType (tree : untpd.Inlined , bindings : List [Tree ], expansion : Tree )(implicit ctx : Context ) =
418
418
tree.withType(avoidingType(expansion, bindings))
419
419
420
- def assignType (tree : untpd.If , thenp : Tree , elsep : Tree )(implicit ctx : Context ) =
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)
421
422
tree.withType(thenp.tpe | elsep.tpe)
423
+ }
422
424
423
425
def assignType (tree : untpd.Closure , meth : Tree , target : Tree )(implicit ctx : Context ) =
424
426
tree.withType(
@@ -428,8 +430,15 @@ trait TypeAssigner {
428
430
def assignType (tree : untpd.CaseDef , body : Tree )(implicit ctx : Context ) =
429
431
tree.withType(body.tpe)
430
432
431
- def assignType (tree : untpd.Match , cases : List [CaseDef ])(implicit ctx : Context ) =
433
+ def assignType (tree : untpd.Match , cases : List [CaseDef ])(implicit ctx : Context ) = {
434
+ if (tree.selector.typeOpt.isPhantom)
435
+ 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))
439
+ }
432
440
tree.withType(ctx.typeComparer.lub(cases.tpes))
441
+ }
433
442
434
443
def assignType (tree : untpd.Return )(implicit ctx : Context ) =
435
444
tree.withType(defn.NothingType )
@@ -449,11 +458,15 @@ trait TypeAssigner {
449
458
def assignType (tree : untpd.SingletonTypeTree , ref : Tree )(implicit ctx : Context ) =
450
459
tree.withType(ref.tpe)
451
460
452
- def assignType (tree : untpd.AndTypeTree , left : Tree , right : Tree )(implicit ctx : Context ) =
461
+ def assignType (tree : untpd.AndTypeTree , left : Tree , right : Tree )(implicit ctx : Context ) = {
462
+ checkSameUniverse(left, right, " be combined in `&`" , tree.pos)
453
463
tree.withType(left.tpe & right.tpe)
464
+ }
454
465
455
- def assignType (tree : untpd.OrTypeTree , left : Tree , right : Tree )(implicit ctx : Context ) =
466
+ def assignType (tree : untpd.OrTypeTree , left : Tree , right : Tree )(implicit ctx : Context ) = {
467
+ checkSameUniverse(left, right, " be combined in `|`" , tree.pos)
456
468
tree.withType(left.tpe | right.tpe)
469
+ }
457
470
458
471
/** Assign type of RefinedType.
459
472
* Refinements are typed as if they were members of refinement class `refineCls`.
@@ -483,8 +496,10 @@ trait TypeAssigner {
483
496
def assignType (tree : untpd.ByNameTypeTree , result : Tree )(implicit ctx : Context ) =
484
497
tree.withType(ExprType (result.tpe))
485
498
486
- def assignType (tree : untpd.TypeBoundsTree , lo : Tree , hi : Tree )(implicit ctx : Context ) =
499
+ def assignType (tree : untpd.TypeBoundsTree , lo : Tree , hi : Tree )(implicit ctx : Context ) = {
500
+ checkSameUniverse(lo, hi, " be combined in type bounds." , tree.pos)
487
501
tree.withType(if (lo eq hi) TypeAlias (lo.tpe) else TypeBounds (lo.tpe, hi.tpe))
502
+ }
488
503
489
504
def assignType (tree : untpd.Bind , sym : Symbol )(implicit ctx : Context ) =
490
505
tree.withType(NamedType .withFixedSym(NoPrefix , sym))
@@ -529,6 +544,12 @@ trait TypeAssigner {
529
544
530
545
def assignType (tree : untpd.PackageDef , pid : Tree )(implicit ctx : Context ) =
531
546
tree.withType(pid.symbol.valRef)
547
+
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)
551
+ }
552
+
532
553
}
533
554
534
555
object TypeAssigner extends TypeAssigner
0 commit comments