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 handling of Java varargs #8718

Merged
merged 4 commits into from
May 25, 2020
Merged

Conversation

smarter
Copy link
Member

@smarter smarter commented Apr 13, 2020

No description provided.

The class constant corresponding to the primitive type `int` is
represented in bytecode by `Integer.TYPE`, this is correctly handled
when calling `clsOf`, but so far `ElimRepeated` was manually creating a
constant instead which does not work (I've now changed the backend to
emit a failure instead of doing the wrong thing when someone does that,
I also plan to rework the way we handle classOf to make this less
error-prone).

Note: after this commit, tests/run/t1360.scala started failing, this is
fixed in the next commit.
A Java generic array `T[]` is unpickled in ClassfileParser to `Array[T &
Object]`, because it will not accept primitive arrays. However, Java
varargs were special-cased to drop this intersection. Erasure contained
some code to compensate for this, but this is still unsound and lead to
tests/run/t1360.scala failing with a ClassCastException.

Thanks to scala#8669 we can drop this special-casing of varargs without
affecting typing too much: it's still possible to pass `Array(1, 2): _*`
where `(T & Object)*` is expected because adaptation will take care of
boxing.

One test case had to be adapted, I replaced:

    def apply[X](xs : X*): java.util.List[X]
      java.util.Arrays.asList(xs: _*)

with:

    def apply[X <: AnyRef](xs : X*): java.util.List[X] =
      java.util.Arrays.asList(xs: _*)

I don't think that's too constraining.

Note: after this commit, tests/run/i533 started failing, this is fixed
in the next commit.
Treat them like generic arrays coming from Java classfiles: `T[]` needs
to be typed as `Array[T & Object]` since it will not accept primitive
arrays.
@smarter smarter marked this pull request as ready for review May 18, 2020 14:09
@smarter smarter requested a review from odersky May 18, 2020 14:09
@odersky odersky merged commit 6345477 into scala:master May 25, 2020
@odersky odersky deleted the java-varargs branch May 25, 2020 12:50
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.

2 participants