Skip to content

Disallow type Null in a type pattern or isInstanceOf test #4004

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

Closed
allanrenucci opened this issue Feb 15, 2018 · 9 comments · Fixed by #12905
Closed

Disallow type Null in a type pattern or isInstanceOf test #4004

allanrenucci opened this issue Feb 15, 2018 · 9 comments · Fixed by #12905

Comments

@allanrenucci
Copy link
Contributor

Should we disallow type Null in a type pattern or isInstanceOf test like scalac does to avoid surprising behaviors?

Dotty:

scala> null.isInstanceOf[Null]
val res0: Boolean = false

Scalac:

scala> null.isInstanceOf[Null]
<console>:12: error: type Null cannot be used in a type pattern or isInstanceOf test
       null.isInstanceOf[Null]
                        ^
@smarter
Copy link
Member

smarter commented Feb 15, 2018

I would say yes, same with Singleton.

@nicolasstucki
Copy link
Contributor

Maybe we should translate null.isInstanceOf[Null] into null eq null. Like we currently do with singleton types, we translate val o = ""; "".isInstanceOf[o.type] becomes val o = ""; "" eq o.

@smarter
Copy link
Member

smarter commented Mar 9, 2018

But what's the usecase? :)

@nicolasstucki
Copy link
Contributor

This can only be done if the callee is a subtype of AnyRef. Otherwise, it should be disallowed or 0.isInstanceOf[Null] might say true.

@nicolasstucki
Copy link
Contributor

The main point is that we should at least not do it for singletons. The one with Null might be useful in cases where an isInstanceOf check is inserted by the compiler for some reason (not sure if it ever happens).

@Blaisorblade
Copy link
Contributor

Blaisorblade commented Mar 12, 2018

The one with Null might be useful in cases where an isInstanceOf check is inserted by the compiler for some reason (not sure if it ever happens).

So somebody could just forbid isInstanceOf[Null] and see if something breaks. Volunteers? @liufengyun maybe in your PR when you have time?

Pattern matching with singleton types (which is equivalent to isInstanceOf, IIUC) is useful (at least a bit) with GADTs:

def f[T, U](t: Foo[T], u: Foo[U]) =
  t match {
    case _: u.type => // Here T and U are equal
  }

An actual use case is in ECOOP'07 Burak Emir's paper on case classes, and that code still works today in Scala (though IIRC not yet in Dotty).

@odersky
Copy link
Contributor

odersky commented Jun 8, 2021

I also think we should try to disallow isInstanceOf[Null]

@odersky
Copy link
Contributor

odersky commented Jun 8, 2021

One then also needs to check that

e match
  case x: Null => ...

is rejected.

@bishabosha
Copy link
Member

Scala 2.13.6 also rejects isInstanceOf[Nothing]

Kordyjan added a commit that referenced this issue Jun 22, 2021
Fixes #4004
`isInstanceOf[Nothing]` checks are always prohibited
`isInstanceOf[Null]` checks are probihibited unless they can be proven at compiletime, then thay are simplified to `true`
Kordyjan added a commit that referenced this issue Jun 22, 2021
Fixes #4004
`isInstanceOf[Nothing]` checks are always prohibited
`isInstanceOf[Null]` checks are probihibited unless they can be proven at compiletime, then thay are simplified to `true`
changvvb pushed a commit to changvvb/dotty that referenced this issue Jun 23, 2021
Fixes scala#4004
`isInstanceOf[Nothing]` checks are always prohibited
`isInstanceOf[Null]` checks are probihibited unless they can be proven at compiletime, then thay are simplified to `true`
@Kordyjan Kordyjan added this to the 3.0.2 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants