Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Matchable to the parents of Null in explicit nulls #12697

Merged
merged 1 commit into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 => () }