Array erasure: Better Java and Scala 2 compat #11846
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We sometimes need to erase
Array[T]
toObject
instead ofArray[erasure(T)]
in method signatures, this happens when bothprimitive 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 erasedArrays whose element type is upper-bounded by a universal trait to
Object (like
Array[_ <: Serializable]
), this isn't necessary sinceprimitives 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]
toArray[Int]
whereas Scala 2 erases to
Object
(this is important since we wantIArray[Int]
to be erased toArray[Int]
), so we need to special caseScala 2 array erasure anyway to handle this.
This commit renames
isUnboundedGeneric
toisGenericArrayElement
since the former was somewhat misleading,
T <: String | Any
isbounded, but
Array[T]
cannot be represented with a specific JVM arraytype.