diff --git a/compiler/src/dotty/tools/dotc/core/Definitions.scala b/compiler/src/dotty/tools/dotc/core/Definitions.scala index 0034e8b2d9ac..547ceb292055 100644 --- a/compiler/src/dotty/tools/dotc/core/Definitions.scala +++ b/compiler/src/dotty/tools/dotc/core/Definitions.scala @@ -455,8 +455,9 @@ class Definitions { ScalaPackageClass, tpnme.Nothing, AbstractFinal, List(AnyType)) def NothingType: TypeRef = NothingClass.typeRef @tu lazy val NullClass: ClassSymbol = { - val parent = if ctx.explicitNulls then AnyType else ObjectType - enterCompleteClassSymbol(ScalaPackageClass, tpnme.Null, AbstractFinal, parent :: Nil) + // When explicit-nulls is enabled, Null becomes a direct subtype of Any and Matchable + val parents = if ctx.explicitNulls then AnyType :: MatchableType :: Nil else ObjectType :: Nil + enterCompleteClassSymbol(ScalaPackageClass, tpnme.Null, AbstractFinal, parents) } def NullType: TypeRef = NullClass.typeRef diff --git a/tests/explicit-nulls/neg/basic.scala b/tests/explicit-nulls/neg/basic.scala index cafe4b156d85..66284de4a9f9 100644 --- a/tests/explicit-nulls/neg/basic.scala +++ b/tests/explicit-nulls/neg/basic.scala @@ -9,6 +9,9 @@ class Basic { val any1: Any = null val any2: Any = n + val matchable1: Matchable = null + val matchable2: Matchable = n + val s1: String = null // error val s2: String = n // error val s3: String | Null = null diff --git a/tests/explicit-nulls/pos/matchable.scala b/tests/explicit-nulls/pos/matchable.scala new file mode 100644 index 000000000000..928f8e33934f --- /dev/null +++ b/tests/explicit-nulls/pos/matchable.scala @@ -0,0 +1,3 @@ +def foo1[T <: Matchable](t: T) = t match { case t: Null => () } + +def foo2[T <: Matchable](t: T) = t match { case null => () } \ No newline at end of file