Skip to content

Commit 0d1e75a

Browse files
authored
Merge pull request #10620 from dotty-staging/fix-null-anyval
Fix problem with comparing Null and value classes
2 parents d099250 + e8bcc6e commit 0d1e75a

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,13 @@ object Types {
264264
// If the type is `T | Null` or `T | Nothing`, the class is != Nothing,
265265
// and `T` derivesFrom the class, then the OrType derivesFrom the class.
266266
// Otherwise, we need to check both sides derivesFrom the class.
267-
if tp.tp1.isBottomType && cls != defn.NothingClass then
267+
def isLowerBottomType(tp: Type) =
268+
tp.isBottomType
269+
&& (tp.hasClassSymbol(defn.NothingClass)
270+
|| cls != defn.NothingClass && !cls.isValueClass)
271+
if isLowerBottomType(tp.tp1) then
268272
loop(tp.tp2)
269-
else if tp.tp2.isBottomType && cls != defn.NothingClass then
273+
else if isLowerBottomType(tp.tp2) then
270274
loop(tp.tp1)
271275
else
272276
loop(tp.tp1) && loop(tp.tp2)

tests/neg/null-anyval.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
object Test:
2+
val x: Int = 0
3+
val y: Int | Null = x // during erasure, x is boxed here, and Int | Null becomes Object
4+
val z0: Int = identity(y) // error
5+
val z1: Int = identity[Int | Null](y) // error
6+
val z2: Int = y // error
7+
8+
class StrWrapper(x: String) extends AnyVal
9+
val z3: StrWrapper = null // error
10+
val z4: O.T = null // error
11+
object O:
12+
opaque type T = String

0 commit comments

Comments
 (0)