diff --git a/src/dotty/tools/dotc/core/TypeComparer.scala b/src/dotty/tools/dotc/core/TypeComparer.scala index 2140f9bfe3a7..eddf0b3e927f 100644 --- a/src/dotty/tools/dotc/core/TypeComparer.scala +++ b/src/dotty/tools/dotc/core/TypeComparer.scala @@ -777,8 +777,13 @@ class TypeComparer(initctx: Context) extends DotClass { case TypeBounds(lo1, hi1) => isSubType(hi1, tp2) case _ => + def isNullable(tp: Type): Boolean = tp.dealias match { + case tp: TypeRef => tp.symbol.isNullableClass + case RefinedType(parent, _) => isNullable(parent) + case _ => false + } (tp1.symbol eq NothingClass) && tp2.isInstanceOf[ValueType] || - (tp1.symbol eq NullClass) && tp2.dealias.typeSymbol.isNullableClass + (tp1.symbol eq NullClass) && isNullable(tp2) } case tp1: SingletonType => isNewSubType(tp1.underlying.widenExpr, tp2) || { diff --git a/tests/pos/i262-null-subtyping.scala b/tests/pos/i262-null-subtyping.scala new file mode 100644 index 000000000000..284be49e8520 --- /dev/null +++ b/tests/pos/i262-null-subtyping.scala @@ -0,0 +1,19 @@ +object O { + // This compiles + val a: { type T } = null; + val b: Any { type T } = null; + + // This doesn't: + // found : Null + // required: AnyRef{T} + val c: AnyRef { type T } = null; + + class A + class B + + val d: A & B = null + val e: A | B = null + + val f: (A & B) { def toString: String } = null + val g: (A | B) { def toString: String } = null +}