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

Incorrect erasure for intersection type in Java method #12586

Closed
tpolecat opened this issue May 24, 2021 · 3 comments · Fixed by #12603
Closed

Incorrect erasure for intersection type in Java method #12586

tpolecat opened this issue May 24, 2021 · 3 comments · Fixed by #12603
Assignees
Milestone

Comments

@tpolecat
Copy link

tpolecat commented May 24, 2021

Compiler version

3.0.0

Minimized code

Add dependency "org.testcontainers" % "testcontainers" % "1.15.2" and then try the following:

scala> new org.testcontainers.images.builder.ImageFromDockerfile().withFileFromClasspath("foo", "bar")                                                                                                                   
java.lang.NoSuchMethodError: 'org.testcontainers.images.builder.traits.BuildContextBuilderTrait org.testcontainers.images.builder.ImageFromDockerfile.withFileFromClasspath(java.lang.String, java.lang.String)'
  ... 38 elided

The method in question is defined here.

Scala 2.13 generates the following instruction:

      16: invokeinterface #45,  3           // InterfaceMethod org/testcontainers/images/builder/traits/ClasspathTrait.withFileFromClasspath:(Ljava/lang/String;Ljava/lang/String;)Lorg/testcontainers/images/builder/traits/ClasspathTrait;

Scala 3.0 generates the following, which differs in the return type.

      18: invokeinterface #34,  3           // InterfaceMethod org/testcontainers/images/builder/traits/ClasspathTrait.withFileFromClasspath:(Ljava/lang/String;Ljava/lang/String;)Lorg/testcontainers/images/builder/traits/BuildContextBuilderTrait;
@smarter smarter changed the title java.lang.NoSuchMethodError when invoking default SELF Java method Incorrect erasure for intersection type in Java method May 24, 2021
@smarter
Copy link
Member

smarter commented May 24, 2021

Huh, looks like I misread https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.4, somehow I was convinced the first operand of a Java intersection had to be a class and could not be an interface, and therefore designed our intersection erasure algorithm around this (#11808). Turns out that's completely false and now that 3.0 is out it's too late to change how we do intersection erasure, so we'll have to add a special case for Java intersection erasure (but I guess that was inevitable since we can't at the same time have commutativity and match the Java erasure semantics, oh well).

@smarter smarter self-assigned this May 24, 2021
@smarter
Copy link
Member

smarter commented May 24, 2021

Oh man, looking at testcontainers/testcontainers-scala#177 it looks like the problem was known before 3.0.0 was out, so if only we had known about it then, we could have potentially redesigned our erasure algorithm in time, whereas now we're stuck with it until Scala 4. Sadness.

@tpolecat
Copy link
Author

Yep, sorry. Nobody looked at it closely until an hour ago. :-(

@smarter smarter added this to the 3.0.1-RC1 milestone May 25, 2021
smarter added a commit to dotty-staging/dotty that referenced this issue May 25, 2021
When I implemented our erasure algorithm in
scala#11808, I misread
https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.4 and
incorrectly thought that a Java intersection had to contain at least one
class, and since we always erase a Scala 3 intersection containing a
class to that class, I thought we could use the Scala 3 intersection
algorithm to erase Java intersections. But my assumption was incorrect:
a Java intersection can in fact contain only interfaces, so we need to
special-case them in erasure like before.

Fixes scala#12586.
@Kordyjan Kordyjan modified the milestones: 3.0.1-RC1, 3.0.1 Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants