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

Java imports of Scala symbols with trailing $ not inferred #13662

Closed
stuhood opened this issue Nov 18, 2021 · 7 comments · Fixed by #13739
Closed

Java imports of Scala symbols with trailing $ not inferred #13662

stuhood opened this issue Nov 18, 2021 · 7 comments · Fixed by #13739

Comments

@stuhood
Copy link
Member

stuhood commented Nov 18, 2021

akka-http-core/src/main/java/akka/http/javadsl/model/headers/ByteRange.java:7: error: package akka.http.scaladsl.model.headers does not exist
import akka.http.scaladsl.model.headers.ByteRange$;
                                       ^
@stuhood stuhood changed the title Java imports of Scala symbols with $ not inferred Java imports of Scala symbols with trailing $ not inferred Nov 18, 2021
@tdyas
Copy link
Contributor

tdyas commented Nov 20, 2021

This occurs because of the Scala name mangling. The Java reference to akka.http.scaladsl.model.headers.ByteRange is for the Scala object at https://github.com/akka/akka-http/blob/c3f3c9d6cc09e81efc553b45519f91e026939861/akka-http-core/src/main/scala/akka/http/scaladsl/model/headers/ByteRange.scala#L32.

@tdyas
Copy link
Contributor

tdyas commented Nov 20, 2021

Unfortunately, the name mangling is not officially documented and is implementation-specific and so can (and has) changed between scalac versions.

https://github.com/travisbrown/scala-java-interop/blob/master/src/main/java/demo/UsingScala.java provides examples of the name mangling that we could refer to.

Is this worth even doing? If users are importing Scala code into Java files, maybe we should punt and they will have to add explicit dependencies?

@tdyas
Copy link
Contributor

tdyas commented Nov 20, 2021

scala.reflect.NameTransformer exposes some of the name mangling algorithm. Using this could work if we apply it to the "provided names" and expose those names as additional provided names.

tdyas pushed a commit that referenced this issue Nov 23, 2021
…code (#13696)

As described in #13662, Java code that tries to import Scala symbols is not having that dependency inferred by Pants. The cause is that the Scala backend does not expose the transformed names seen by Java code for Scala symbols.

For example, the instance for `object Foo` in package `org.pantsbuild.example` is actually `org.pantsbuild.example.Foo$.MODULE$`.

This PR is the first step in solving this by generating the transformed names as part of source analysis. Only the `object` case is handled for now. A subsequent PR will use this information for Java dependency inference.

[ci skip-rust]
@stuhood
Copy link
Member Author

stuhood commented Nov 29, 2021

Fixed by #13696. Thanks @tdyas!

@stuhood stuhood closed this as completed Nov 29, 2021
@tdyas
Copy link
Contributor

tdyas commented Nov 29, 2021

I still have to implement inference in the Python rules using this data.

@tdyas tdyas reopened this Nov 29, 2021
@tdyas
Copy link
Contributor

tdyas commented Nov 29, 2021

The reason is that only the Java rules should consume the mangled names. Scala inference should ignore this data. That seemed like a complicated change so I didn't include that change in #13696.

@stuhood
Copy link
Member Author

stuhood commented Nov 30, 2021

This represented roughly 5% of the issues in akka-http.

stuhood pushed a commit that referenced this issue Nov 30, 2021
Use analysis of Scala encoded symbols ("name-mangling") to allow inference of those symbols in Java sources. This is the last part of solving #13662.

Fixes #13662.

[ci skip-rust]
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 a pull request may close this issue.

2 participants