Skip to content
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

Fix isInstanceOf[Array[?]] returning true on non-Array #12103

Merged
merged 1 commit into from
Apr 15, 2021

Conversation

smarter
Copy link
Member

@smarter smarter commented Apr 15, 2021

Before this commit, the MultiArrayOf(elem, ndims) extractor used to
strip wildcards from the element type, this was surprising since it does
not match what the ArrayOf(elem) extractor does, and lead to a bug in
TypeTestCasts.interceptTypeApply which contains the following code:

case defn.MultiArrayOf(elem, ndims) if isGenericArrayElement(elem, isScala2 = false) =>

isGenericArrayElement returns false for Any but true for
_ >: Nothing <: Any, so the stripped wildcard means that this case was
skipped, resulting in:

x.isInstanceOf[Array[?]]

being erased to:

x.isInstanceOf[Object]

instead of:

scala.runtime.ScalaRunTime.isArray(x, 1)

Fixed by tweaking MultiArrayOf to keep any final wildcard around like
ArrayOf does.

Before this commit, the `MultiArrayOf(elem, ndims)` extractor used to
strip wildcards from the element type, this was surprising since it does
not match what the `ArrayOf(elem)` extractor does, and lead to a bug in
`TypeTestCasts.interceptTypeApply` which contains the following code:

case defn.MultiArrayOf(elem, ndims) if isGenericArrayElement(elem, isScala2 = false) =>

`isGenericArrayElement` returns false for `Any` but true for
`_ >: Nothing <: Any`, so the stripped wildcard means that this case was
skipped, resulting in:
    x.isInstanceOf[Array[?]]
being erased to:
    x.isInstanceOf[Object]
instead of:
    scala.runtime.ScalaRunTime.isArray(x, 1)

Fixed by tweaking `MultiArrayOf` to keep any final wildcard around like
`ArrayOf` does.
@smarter smarter added this to the 3.0.0-RC3 milestone Apr 15, 2021
@smarter smarter requested a review from sjrd April 15, 2021 16:07
@smarter
Copy link
Member Author

smarter commented Apr 15, 2021

Milestoned to 3.0.0-RC3 since a bug in isInstanceOf seems pretty critical to me.

val str: Any = ""
assert(!str.isInstanceOf[Array[?]])
assert(!str.isInstanceOf[Array[Array[?]]])
assert(!str.isInstanceOf[Array[? <: Array[?]]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add positive tests? I.e., tests for which isInstanceOf returns true.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That file already checks case x: Array[_] in various situations

@smarter smarter linked an issue Apr 15, 2021 that may be closed by this pull request
@smarter smarter added the backport:accepted This PR needs to be backported, once it's been backported replace this tag by "backport:done" label Apr 15, 2021
@smarter smarter enabled auto-merge April 15, 2021 16:51
@smarter smarter merged commit 59ea58e into scala:master Apr 15, 2021
@smarter smarter deleted the fix-isInstanceOf-array branch April 15, 2021 17:03
@smarter smarter added backport:done This PR was successfully backported. and removed backport:accepted This PR needs to be backported, once it's been backported replace this tag by "backport:done" labels Apr 17, 2021
@Kordyjan Kordyjan modified the milestones: 3.0.0-RC3, 3.0.1 Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:done This PR was successfully backported.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong "isInstanceOf[Array[?]]"
3 participants