Skip to content

Fix problem with comparing Null and value classes #10620

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

Merged
merged 1 commit into from
Dec 3, 2020
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
8 changes: 6 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,13 @@ object Types {
// If the type is `T | Null` or `T | Nothing`, the class is != Nothing,
// and `T` derivesFrom the class, then the OrType derivesFrom the class.
// Otherwise, we need to check both sides derivesFrom the class.
if tp.tp1.isBottomType && cls != defn.NothingClass then
def isLowerBottomType(tp: Type) =
tp.isBottomType
&& (tp.hasClassSymbol(defn.NothingClass)
|| cls != defn.NothingClass && !cls.isValueClass)
if isLowerBottomType(tp.tp1) then
loop(tp.tp2)
else if tp.tp2.isBottomType && cls != defn.NothingClass then
else if isLowerBottomType(tp.tp2) then
loop(tp.tp1)
else
loop(tp.tp1) && loop(tp.tp2)
Expand Down
12 changes: 12 additions & 0 deletions tests/neg/null-anyval.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
object Test:
val x: Int = 0
val y: Int | Null = x // during erasure, x is boxed here, and Int | Null becomes Object
val z0: Int = identity(y) // error
val z1: Int = identity[Int | Null](y) // error
val z2: Int = y // error

class StrWrapper(x: String) extends AnyVal
val z3: StrWrapper = null // error
val z4: O.T = null // error
object O:
opaque type T = String