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

Overridden trait method cannot be called from java #12753

Closed
felixbr opened this issue Jun 8, 2021 · 2 comments · Fixed by #12860
Closed

Overridden trait method cannot be called from java #12753

felixbr opened this issue Jun 8, 2021 · 2 comments · Fixed by #12860
Assignees
Milestone

Comments

@felixbr
Copy link

felixbr commented Jun 8, 2021

Compiler version

3.0.0 (it compiles fine with 2.13.6 and earlier)

Minimized code

The following is minimized code from my Scala3 migration attempt of twitter/util.

trait TimeLike[This <: TimeLike[This]]

trait TimeLikeOps[This <: TimeLike[This]] {
  def fromMilliseconds(milliseconds: Long): This

  def fromSeconds(seconds: Long): This = fromMilliseconds(seconds * 1000L)
}

sealed class Duration(val milliseconds: Long) extends TimeLike[Duration]
object Duration extends TimeLikeOps[Duration] {
  def fromMilliseconds(milliseconds: Long): Duration = new Duration(milliseconds)
  
  // note that this override was already a workaround for calling Scala2 from Java. For pure Scala it's not needed.
  override def fromSeconds(seconds: Long): Duration = super.fromSeconds(seconds)
}

Then trying to call the overridden method from Java causes a compile-error:

Duration.fromSeconds(1);

If you want to run the code, here's a runnable branch/commit: felixbr/scala3-example-project@e23ffcf

Output

reference to fromSeconds is ambiguous 
both method fromSeconds(long) in twitter.Duration and method fromSeconds(long) in twitter.Duration match
Duration.fromSeconds

Expectation

It should compile as it did in 2.13.6 and earlier.

Cheers
~ Felix

@lrytz
Copy link
Member

lrytz commented Jun 9, 2021

Scala 3 generates two static forwarders.

  public static fromSeconds(J)LDuration;

  public static fromSeconds(J)LTimeLike;

Scala 2 only the first, it was fixed in scala/scala#6531 / scala/bug#10812.

The override is necessary because of scala/bug#11305, I assume.

https://github.com/lampepfl/dotty/blob/fdbb94f29b05fb712c04b981f462ae65b447fc39/compiler/src/dotty/tools/backend/jvm/BCodeHelpers.scala#L925-L926

@lrytz
Copy link
Member

lrytz commented Jun 9, 2021

I'll take a look (can't assign myself, don't have permission in this repo)

smarter added a commit to dotty-staging/dotty that referenced this issue Jun 17, 2021
Static forwarders for bridges lead to ambiguous errors in some Java
compilers, and the trait setters aren't meant to be called by users.
Since we can't remove them without breaking binary-compatibility, we
mark them ACC_SYNTHETIC so that Java compilers will ignore them. See
also the discussion in scala#12767 which implements an alternate fix.

Fixes scala#12753.

Co-Authored-By: Lukas Rytz <[email protected]>
@Kordyjan Kordyjan added this to the 3.0.2 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
4 participants