Skip to content

Commit

Permalink
Merge pull request #12697 from dotty-staging/fix-parents-of-null
Browse files Browse the repository at this point in the history
Add Matchable to the parents of Null in explicit nulls
  • Loading branch information
noti0na1 authored Jun 4, 2021
2 parents 2ce3e97 + e248658 commit cbb1e07
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions tests/explicit-nulls/neg/basic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/explicit-nulls/pos/matchable.scala
Original file line number Diff line number Diff line change
@@ -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 => () }

0 comments on commit cbb1e07

Please sign in to comment.