Skip to content

Commit cbb1e07

Browse files
authored
Merge pull request #12697 from dotty-staging/fix-parents-of-null
Add Matchable to the parents of Null in explicit nulls
2 parents 2ce3e97 + e248658 commit cbb1e07

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,9 @@ class Definitions {
455455
ScalaPackageClass, tpnme.Nothing, AbstractFinal, List(AnyType))
456456
def NothingType: TypeRef = NothingClass.typeRef
457457
@tu lazy val NullClass: ClassSymbol = {
458-
val parent = if ctx.explicitNulls then AnyType else ObjectType
459-
enterCompleteClassSymbol(ScalaPackageClass, tpnme.Null, AbstractFinal, parent :: Nil)
458+
// When explicit-nulls is enabled, Null becomes a direct subtype of Any and Matchable
459+
val parents = if ctx.explicitNulls then AnyType :: MatchableType :: Nil else ObjectType :: Nil
460+
enterCompleteClassSymbol(ScalaPackageClass, tpnme.Null, AbstractFinal, parents)
460461
}
461462
def NullType: TypeRef = NullClass.typeRef
462463

tests/explicit-nulls/neg/basic.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class Basic {
99
val any1: Any = null
1010
val any2: Any = n
1111

12+
val matchable1: Matchable = null
13+
val matchable2: Matchable = n
14+
1215
val s1: String = null // error
1316
val s2: String = n // error
1417
val s3: String | Null = null
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def foo1[T <: Matchable](t: T) = t match { case t: Null => () }
2+
3+
def foo2[T <: Matchable](t: T) = t match { case null => () }

0 commit comments

Comments
 (0)