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

No static forwarder methods in companion of trait #13572

Closed
jlprat opened this issue Sep 21, 2021 · 0 comments · Fixed by #13573
Closed

No static forwarder methods in companion of trait #13572

jlprat opened this issue Sep 21, 2021 · 0 comments · Fixed by #13573

Comments

@jlprat
Copy link

jlprat commented Sep 21, 2021

First of all sorry if this was already discussed or if it's intentional, but I encountered a discrepancy in bytecode generation between Scala 2.13 and 3.x.
In Scala 2.13 when an object and a trait shared the same name, all methods belonging to the object were copied over to the class representing the trait as static ones.
This bit is important for the Java interoperability aspect, as the Scala 2.13 bytecode made it easy to use Scala classes from Java, while in Scala 3, we would need to fall back to referencing members of the object using the synthetic name (class name plus $ sign).

Note that when the combination is an object and a class the bytecode generated is compatible with the one generated for Scala 2.13, for more details see the reproducer here.

Compiler version

Scala 3.0.2 and 3.1.0-RC2

Minimized code

You can find a reproducer here

package example

object ObjectTraitPair {
  val Constant: String = "Some Text"
}

// In Scala 2.13 this class bytecode will carry over
// any val and def defined in the object with the same name
// but not in Scala 3.0
trait ObjectTraitPair {
  val method: String = "bye"
}

Output

If we inspect the bytecode of the trait (ObjectTraitPair.class) we see:
javap ObjectTraitPair.class

public interface ObjectTraitPair {
  public static void $init$(example.ObjectTraitPair);
  public abstract java.lang.String method();
  public abstract void example$ObjectTraitPair$_setter_$method_$eq(java.lang.String);
}

Expectation

In Scala 2.13 the same trait's bytecode would be

public interface ObjectTraitPair {
  public static java.lang.String Constant();
  public abstract void example$ObjectTraitPair$_setter_$method_$eq(java.lang.String);
  public abstract java.lang.String method();
  public static void $init$(example.ObjectTraitPair);
}

Please note the missing public static java.lang.String Constant(); on the Scala 3 bytecode.

Workaround

When this needs to be used from Java, the workaround can be this:

// This won't compile if the `ObjectTraitPair` is compiled in Scala 3.
// Instead of referencing it like this:
ObjectTraitPair.Constant();

// It can be referenced like this:
ObjectTraitPair$.MODULE$.Constant();
@smarter smarter changed the title Bytecode Difference when Object + Trait with the same name between Scala 2.13 and 3.x No static forwarder methods in companion of trait Sep 21, 2021
@smarter smarter self-assigned this Sep 21, 2021
smarter added a commit to dotty-staging/dotty that referenced this issue Sep 21, 2021
smarter added a commit to dotty-staging/dotty that referenced this issue Sep 21, 2021
jlprat added a commit to jlprat/kafka that referenced this issue Sep 21, 2021
jlprat added a commit to jlprat/kafka that referenced this issue Sep 21, 2021
anatoliykmetyuk pushed a commit to dotty-staging/dotty that referenced this issue Sep 27, 2021
smarter added a commit to dotty-staging/dotty that referenced this issue Oct 5, 2021
Same change implemented five years ago in Scala 2:
scala/scala#5131

Fixes scala#13572.

This PR was backported to 3.1.0-RC3 in scala#13616, this lets us bump the
previousDottyVersion to make the mima checks pass even though this PR
introduces new public member (and while we're at it, we can reset the
mima filter list).
olsdavis pushed a commit to olsdavis/dotty that referenced this issue Apr 4, 2022
Same change implemented five years ago in Scala 2:
scala/scala#5131

Fixes scala#13572.

This PR was backported to 3.1.0-RC3 in scala#13616, this lets us bump the
previousDottyVersion to make the mima checks pass even though this PR
introduces new public member (and while we're at it, we can reset the
mima filter list).
@Kordyjan Kordyjan added this to the 3.1.1 milestone 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.

4 participants