-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #5140: Ycheck failure in covariant java varargs #5403
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is what scalac does then I suppose we should reproduce it
Could we instead change elimRepeated to replace |
Switched to @smarter's suggestion that we change the type to |
@smarter Here's the problem I run into with the The code generated by
Which doesn't work because there's no sound way to convert an If there's no sound way, then I suggest we revert back to the |
The problem is that passing object Test {
val x: Array[_ <: Object] = Array()
val y = Predef.wrapRefArray(x)
val z: Seq[Object] = y
} The infered type argument is |
PTAL. Changed
Which can't be written with surface syntax (unbound wildcard), but apparently typechecks. |
Allow passing `Array[Dog]` to a Java varargs that expects an `Array[Animal]`. This types because Java vargs are represented as `RepeatedParam[+T]`, which is covariant. However, after `elimRepeated`, the `RP`s go away and the true type (Array) is revealed, which was causing a Ycheck error because arrays are invariant before erasure. Fix the Ycheck error by changing how `elimRepeated` transforms `RP[T]`s that appear in Java code. The previous transform was `RP[T]` => `Array[T]`. The new transform is `RP[T]` => `Array[_ <: T]`. This is unsound but consistent with the typer behaviour and what scalac does.
Addressed style comments and collapsed review commits. |
Thanks @abeln! |
This is not necessary anymore after scala#5403
This is not necessary anymore after scala#5403
Allow passing
Array[Dog]
to a Java varargs that expects anArray[Animal]
. This types because Java vargs are represented asRepeatedParam[+T]
, which is covariant.However, after
elimRepeated
, theRP
s go away and the true type(Array) is revealed, which was causing a Ycheck error because arrays are
invariant before erasure.
Fix the Ycheck error by casting
Array[Dog]
toArray[Animal]
. This isunsound but consistent with the typer behaviour and what scalac does.