Skip to content

Commit 0a386a8

Browse files
committed
Only replace intersections of constants with Nothing
1 parent 9cb3542 commit 0a386a8

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -780,11 +780,17 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
780780
val t2 = mergeIfSub(tp2, tp1)
781781
if (t2.exists) t2
782782
else tp1 match {
783-
case tp1: SingletonType =>
783+
case tp1: ConstantType =>
784784
tp2 match {
785-
case tp2: SingletonType =>
786-
// Make use of the fact that the intersection of two singleton
787-
// types which are not subtypes of each other is empty.
785+
case tp2: ConstantType =>
786+
// Make use of the fact that the intersection of two constant types
787+
// types which are not subtypes of each other is known to be empty.
788+
// Note: The same does not apply to singleton types in general.
789+
// E.g. we could have a pattern match against `x.type & y.type`
790+
// which might succeed if `x` and `y` happen to be the same ref
791+
// at run time. It would not work to replace that with `Nothing`.
792+
// However, maybe we can still apply the replacement to
793+
// types which are not explicitly written.
788794
defn.NothingType
789795
case _ => andType(tp1, tp2)
790796
}

0 commit comments

Comments
 (0)