Skip to content

Commit 179b9cc

Browse files
committed
Fix subtyping of null and refined types.
1 parent 7cdf3d0 commit 179b9cc

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,13 @@ class TypeComparer(initctx: Context) extends DotClass {
777777
case TypeBounds(lo1, hi1) =>
778778
isSubType(hi1, tp2)
779779
case _ =>
780+
def isNullable(tp: Type): Boolean = tp.dealias match {
781+
case tp: TypeRef => tp.symbol.isNullableClass
782+
case RefinedType(parent, _) => isNullable(parent)
783+
case _ => false
784+
}
780785
(tp1.symbol eq NothingClass) && tp2.isInstanceOf[ValueType] ||
781-
(tp1.symbol eq NullClass) && tp2.dealias.typeSymbol.isNullableClass
786+
(tp1.symbol eq NullClass) && isNullable(tp2)
782787
}
783788
case tp1: SingletonType =>
784789
isNewSubType(tp1.underlying.widenExpr, tp2) || {

tests/pos/i262-null-subtyping.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
object O {
2+
// This compiles
3+
val a: { type T } = null;
4+
val b: Any { type T } = null;
5+
6+
// This doesn't:
7+
// found : Null
8+
// required: AnyRef{T}
9+
val c: AnyRef { type T } = null;
10+
11+
class A
12+
class B
13+
14+
val d: A & B = null
15+
val e: A | B = null
16+
17+
val f: (A & B) { def toString: String } = null
18+
val g: (A | B) { def toString: String } = null
19+
}

0 commit comments

Comments
 (0)