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

Dotty does not erase intersection types like scalac #4619

Closed
smarter opened this issue Jun 4, 2018 · 3 comments
Closed

Dotty does not erase intersection types like scalac #4619

smarter opened this issue Jun 4, 2018 · 3 comments

Comments

@smarter
Copy link
Member

smarter commented Jun 4, 2018

Given:

trait X
trait Y

trait Test {
  def foo[A <: X with Y, B <: Y with A](a: A, b: B): Unit
}

Scala 2 erases it as:

def foo(a: X, b: X): Unit

But Dotty erases it as:

 def foo(a: X, b: Y): Unit

This is problematic since it can affect which overloads are valid or not.
The Scala 2 implementation is at https://github.com/scala/scala/blob/a9275b50e425d29c0b7fd45c10317953d5bf7804/src/reflect/scala/reflect/internal/transform/Erasure.scala#L333-L366
The Dotty implementation is at https://github.com/lampepfl/dotty/blob/868d1995b0f10bacf13d365e12eb2b31061a1928/compiler/src/dotty/tools/dotc/core/TypeErasure.scala#L290-L315

@Blaisorblade
Copy link
Contributor

Does this also affect erasure in calls to Scala2-defined methods? That would make the issue even more important.

@smarter
Copy link
Member Author

smarter commented Jun 5, 2018

Yes, I think so.

@Jasper-M
Copy link
Member

Jasper-M commented Jun 6, 2018

Related ticket: #2920

@odersky odersky added this to the 3.0.0-RC1 milestone Nov 9, 2020
@odersky odersky assigned sjrd and smarter and unassigned sjrd Nov 23, 2020
@anatoliykmetyuk anatoliykmetyuk modified the milestones: 3.0.0-M3, 3.0.0-RC1 Dec 10, 2020
@anatoliykmetyuk anatoliykmetyuk modified the milestones: 3.0.0-RC1, 3.0.0-RC2 Feb 11, 2021
smarter added a commit to dotty-staging/dotty that referenced this issue Mar 4, 2021
Because our algorithm for erasing intersection types does not exactly
match the one used by Scala 2, we could end up emitting calls to Scala 2
methods with the wrong bytecode signature, leading to NoSuchMethodError
at runtime. We could try to exactly match what Scala 2 does, but it
turns out that the Scala 2 logic heavily relies on implementation
details which makes it extremely complex to reliably replicate.
Therefore, this commit instead special-cases the erasure of Scala 2
intersections (just like we already special-case the erasure of Java
intersections) and limits which Scala 2 intersection types we support to
a subset that we can erase without too much complications (but even that
still requires ~200 lines of code!). This means that we're now free to
change the way we erase intersections in any way we want without
introducing more compatibility problems (until 3.0.0 that is), I'll
explore this in a follow-up PR.

Fixes scala#4619. Fixes scala#9175.
smarter added a commit to dotty-staging/dotty that referenced this issue Mar 4, 2021
Because our algorithm for erasing intersection types does not exactly
match the one used by Scala 2, we could end up emitting calls to Scala 2
methods with the wrong bytecode signature, leading to NoSuchMethodError
at runtime. We could try to exactly match what Scala 2 does, but it
turns out that the Scala 2 logic heavily relies on implementation
details which makes it extremely complex to reliably replicate.
Therefore, this commit instead special-cases the erasure of Scala 2
intersections (just like we already special-case the erasure of Java
intersections) and limits which Scala 2 intersection types we support to
a subset that we can erase without too much complications (but even that
still requires ~200 lines of code!). This means that we're now free to
change the way we erase intersections in any way we want without
introducing more compatibility problems (until 3.0.0 that is), I'll
explore this in a follow-up PR.

Fixes scala#4619. Fixes scala#9175.
@smarter smarter closed this as completed in e4af70b Mar 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants