Skip to content

Array erasure: Better Java and Scala 2 compat #11846

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
Mar 22, 2021

Conversation

smarter
Copy link
Member

@smarter smarter commented Mar 22, 2021

We sometimes need to erase Array[T] to Object instead of
Array[erasure(T)] in method signatures, this happens when both
primitive and reference arrays, or two different sort of primitive
arrays could be passed as arguments because the lub of those types on
the JVM is Object. But before this commit, we additionally erased
Arrays whose element type is upper-bounded by a universal trait to
Object (like Array[_ <: Serializable]), this isn't necessary since
primitives do not extend those traits (except for compiler fictions like
Singleton) and derived value classes in arrays are always boxed.

Since having matching Scala 2 and 3 erasure is a lost cause (cf #11603),
this commit align ourselves with Java which improves Java
interop (because we can emit more accurate Java generic signatures) and
should let us simplify erasure more in the future.

It also turns out than even before this commit, we did not match Scala 2
erasure perfectly since we erase Array[_ <: Int] to Array[Int]
whereas Scala 2 erases to Object (this is important since we want
IArray[Int] to be erased to Array[Int]), so we need to special case
Scala 2 array erasure anyway to handle this.

This commit renames isUnboundedGeneric to isGenericArrayElement
since the former was somewhat misleading, T <: String | Any is
bounded, but Array[T] cannot be represented with a specific JVM array
type.

We sometimes need to erase `Array[T]` to `Object` instead of
`Array[erasure(T)]` in method signatures, this happens when both
primitive and reference arrays, or two different sort of primitive
arrays could be passed as arguments because the lub of those types on
the JVM is `Object`. But before this commit, we additionally erased
Arrays whose element type is upper-bounded by a universal trait to
Object (like `Array[_ <: Serializable]`), this isn't necessary since
primitives do not extend those traits (except for compiler fictions like
`Singleton`) and derived value classes in arrays are always boxed.

Since having matching Scala 2 and 3 erasure is a lost cause (cf scala#11603),
this commit align ourselves with Java which improves Java
interop (because we can emit more accurate Java generic signatures) and
should let us simplify erasure more in the future.

It also turns out than even before this commit, we did not match Scala 2
erasure perfectly since we erase `Array[_ <: Int]` to `Array[Int]`
whereas Scala 2 erases to `Object` (this is important since we want
`IArray[Int]` to be erased to `Array[Int]`), so we need to special case
Scala 2 array erasure anyway to handle this.

This commit renames `isUnboundedGeneric` to `isGenericArrayElement`
since the former was somewhat misleading, `T <: String | Any` is
bounded, but `Array[T]` cannot be represented with a specific JVM array
type.
@smarter smarter added this to the 3.0.0-RC2 milestone Mar 22, 2021
@smarter smarter requested a review from odersky March 22, 2021 15:52
Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

Nice!

@odersky odersky merged commit 4035f51 into scala:master Mar 22, 2021
@odersky odersky deleted the univ-array-erasure branch March 22, 2021 20:24
@smarter
Copy link
Member Author

smarter commented Mar 29, 2021

@bishabosha Like with #11808, this PR will require some adaptation in Scala 2 to deal with the changed erasure rules, the relevant code in Scala 2 is probably https://github.com/scala/scala/blob/107c72718682024c0494fa81c58ad2301c18aae9/src/reflect/scala/reflect/internal/transform/Erasure.scala#L37

@bishabosha
Copy link
Member

thank you for the tip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants