Skip to content

Commit

Permalink
Under explicit nulls, remove the rule Null <:< x.type.
Browse files Browse the repository at this point in the history
The specified rule that `Null <:< x.type` when the underlying type
`U` of `x` is nullable is dubious to begin with. Under explicit
nulls, it becomes decidedly out of place. We now disable that rule
under `-Yexplicit-nulls`.
  • Loading branch information
sjrd committed May 11, 2023
1 parent 88b4949 commit a7014d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
// scala.AnyRef [Scala 3: which scala.Null conforms to], the type denotes the set of values consisting
// of null and the value denoted by p (i.e., the value v for which v eq p). [Otherwise,] the type
// denotes the set consisting of only the value denoted by p.
isNullable(tp.underlying) && tp.isStable
!ctx.explicitNulls && isNullable(tp.underlying) && tp.isStable
case tp: RefinedOrRecType => isNullable(tp.parent)
case tp: AppliedType => isNullable(tp.tycon)
case AndType(tp1, tp2) => isNullable(tp1) && isNullable(tp2)
Expand Down
10 changes: 10 additions & 0 deletions tests/explicit-nulls/neg/i17467.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object Test:
def test(): Unit =
val a1: String = "foo"
val a2: a1.type = null // error

val b1: String | Null = "foo"
val b2: b1.type = null // error
summon[Null, b1.type] // error
end test
end Test

0 comments on commit a7014d1

Please sign in to comment.