Skip to content

Commit ff836e9

Browse files
committed
Under explicit nulls, remove the rule Null <:< x.type.
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`.
1 parent b6229e1 commit ff836e9

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
912912
// scala.AnyRef [Scala 3: which scala.Null conforms to], the type denotes the set of values consisting
913913
// of null and the value denoted by p (i.e., the value v for which v eq p). [Otherwise,] the type
914914
// denotes the set consisting of only the value denoted by p.
915-
isNullable(tp.underlying) && tp.isStable
915+
!ctx.explicitNulls && isNullable(tp.underlying) && tp.isStable
916916
case tp: RefinedOrRecType => isNullable(tp.parent)
917917
case tp: AppliedType => isNullable(tp.tycon)
918918
case AndType(tp1, tp2) => isNullable(tp1) && isNullable(tp2)

tests/explicit-nulls/neg/i17467.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
object Test:
2+
def test(): Unit =
3+
val a1: String = "foo"
4+
val a2: a1.type = null // error
5+
6+
val b1: String | Null = "foo"
7+
val b2: b1.type = null // error
8+
summon[Null <:< b1.type] // error
9+
10+
/* The following would be sound, but it would require a specific subtyping
11+
* rule (and implementation code) for debatable value. So it is an error.
12+
*/
13+
val c1: Null = null
14+
val c2: h1.type = null // error
15+
end test
16+
end Test

0 commit comments

Comments
 (0)