@@ -399,8 +399,10 @@ trait TypeAssigner {
399
399
def assignType (tree : untpd.Inlined , bindings : List [Tree ], expansion : Tree )(implicit ctx : Context ) =
400
400
tree.withType(avoidingType(expansion, bindings))
401
401
402
- def assignType (tree : untpd.If , thenp : Tree , elsep : Tree )(implicit ctx : Context ) =
402
+ def assignType (tree : untpd.If , thenp : Tree , elsep : Tree )(implicit ctx : Context ) = {
403
+ checkSameUniverse(thenp, elsep, " be combined in branches of if/else" , tree.pos)
403
404
tree.withType(thenp.tpe | elsep.tpe)
405
+ }
404
406
405
407
def assignType (tree : untpd.Closure , meth : Tree , target : Tree )(implicit ctx : Context ) =
406
408
tree.withType(
@@ -410,8 +412,15 @@ trait TypeAssigner {
410
412
def assignType (tree : untpd.CaseDef , body : Tree )(implicit ctx : Context ) =
411
413
tree.withType(body.tpe)
412
414
413
- def assignType (tree : untpd.Match , cases : List [CaseDef ])(implicit ctx : Context ) =
415
+ def assignType (tree : untpd.Match , cases : List [CaseDef ])(implicit ctx : Context ) = {
416
+ if (tree.selector.typeOpt.isPhantom)
417
+ ctx.error(" Cannot pattern match on phantoms" , tree.selector.pos)
418
+ if (cases.nonEmpty) {
419
+ val head = cases.head
420
+ cases.tail.foreach(c => checkSameUniverse(head, c, " be combined in branches of a match" , c.pos))
421
+ }
414
422
tree.withType(ctx.typeComparer.lub(cases.tpes))
423
+ }
415
424
416
425
def assignType (tree : untpd.Return )(implicit ctx : Context ) =
417
426
tree.withType(defn.NothingType )
@@ -431,11 +440,15 @@ trait TypeAssigner {
431
440
def assignType (tree : untpd.SingletonTypeTree , ref : Tree )(implicit ctx : Context ) =
432
441
tree.withType(ref.tpe)
433
442
434
- def assignType (tree : untpd.AndTypeTree , left : Tree , right : Tree )(implicit ctx : Context ) =
443
+ def assignType (tree : untpd.AndTypeTree , left : Tree , right : Tree )(implicit ctx : Context ) = {
444
+ checkSameUniverse(left, right, " be combined in `&`" , tree.pos)
435
445
tree.withType(left.tpe & right.tpe)
446
+ }
436
447
437
- def assignType (tree : untpd.OrTypeTree , left : Tree , right : Tree )(implicit ctx : Context ) =
448
+ def assignType (tree : untpd.OrTypeTree , left : Tree , right : Tree )(implicit ctx : Context ) = {
449
+ checkSameUniverse(left, right, " be combined in `|`" , tree.pos)
438
450
tree.withType(left.tpe | right.tpe)
451
+ }
439
452
440
453
/** Assign type of RefinedType.
441
454
* Refinements are typed as if they were members of refinement class `refineCls`.
@@ -465,8 +478,10 @@ trait TypeAssigner {
465
478
def assignType (tree : untpd.ByNameTypeTree , result : Tree )(implicit ctx : Context ) =
466
479
tree.withType(ExprType (result.tpe))
467
480
468
- def assignType (tree : untpd.TypeBoundsTree , lo : Tree , hi : Tree )(implicit ctx : Context ) =
481
+ def assignType (tree : untpd.TypeBoundsTree , lo : Tree , hi : Tree )(implicit ctx : Context ) = {
482
+ checkSameUniverse(lo, hi, " be combined in type bounds." , tree.pos)
469
483
tree.withType(if (lo eq hi) TypeAlias (lo.tpe) else TypeBounds (lo.tpe, hi.tpe))
484
+ }
470
485
471
486
def assignType (tree : untpd.Bind , sym : Symbol )(implicit ctx : Context ) =
472
487
tree.withType(NamedType .withFixedSym(NoPrefix , sym))
@@ -511,6 +526,12 @@ trait TypeAssigner {
511
526
512
527
def assignType (tree : untpd.PackageDef , pid : Tree )(implicit ctx : Context ) =
513
528
tree.withType(pid.symbol.valRef)
529
+
530
+ private def checkSameUniverse (tree1 : Tree , tree2 : Tree , relationship : => String , pos : Position )(implicit ctx : Context ) = {
531
+ if (tree1.tpe.topType != tree2.tpe.topType)
532
+ ctx.error(ex " ${tree1.tpe} and ${tree2.tpe} are in different universes. They cannot $relationship" , pos)
533
+ }
534
+
514
535
}
515
536
516
537
object TypeAssigner extends TypeAssigner
0 commit comments