Skip to content

Wrong "isInstanceOf[Array[?]]" #12071

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
flomebul opened this issue Apr 12, 2021 · 5 comments · Fixed by #12103
Closed

Wrong "isInstanceOf[Array[?]]" #12071

flomebul opened this issue Apr 12, 2021 · 5 comments · Fixed by #12103
Assignees
Milestone

Comments

@flomebul
Copy link
Contributor

Compiler version

scala-3.0.0-RC2

Minimized code

scala> 1.isInstanceOf[Array[?]]
1 |1.isInstanceOf[Array[?]]
  |^^^^^^^^^^^^^^^^^^^^^^^^
  |cannot test if value of type Int is a reference of class Object

scala> 1.asInstanceOf[Array[?]]
val res0: Array[?] = 1

scala> (1: Any).isInstanceOf[Array[?]]
val res1: Boolean = true

scala> (1: Any).asInstanceOf[Array[?]]
val res2: Array[?] = 1

Expectation

  • 1.asInstanceOf[Array[?]] should raise an exception
  • (1: Any).isInstanceOf[Array[?]] should raise an exception or return false
  • (1: Any).isInstanceOf[Array[?]] should raise an exception
@SethTisue
Copy link
Member

SethTisue commented Apr 12, 2021

scala> (1: Any).isInstanceOf[Array[?]]
val res1: Boolean = true

yes, that's a regression since Scala 2; it should return false. good catch!

the others are not bugs. isInstanceOf should never raise an exception, and asInstanceOf is a promise to the compiler that you know what you're doing. that asInstanceOf bypasses compile-time error checking (though it does insert a runtime cast to the erased type) is exactly why it exists

@SethTisue
Copy link
Member

SethTisue commented Apr 12, 2021

note that autoboxing isn't the culprit here, and Any isn't the culprit either, nor is Integer:

scala> Integer.valueOf(1).isInstanceOf[Array[?]]
val res4: Boolean = true

scala> (new AnyRef).isInstanceOf[Array[?]]
val res5: Boolean = true

@Jasper-M
Copy link
Contributor

The erasure of Array[?] is Object in Scala 2 and 3, so I guess Scala 2 does something special for arrays.

@sjrd
Copy link
Member

sjrd commented Apr 12, 2021

@smarter smarter self-assigned this Apr 15, 2021
@SethTisue
Copy link
Member

Guillaume's PR: #12103

@smarter smarter linked a pull request Apr 15, 2021 that will close this issue
@Kordyjan Kordyjan added this to the 3.0.1 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.

6 participants