Skip to content

Fix #8599: Emit trait init methods only as static methods. #10509

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

Merged
merged 1 commit into from
Nov 27, 2020

Conversation

sjrd
Copy link
Member

@sjrd sjrd commented Nov 26, 2020

Concrete trait methods are encoded as two methods in the back-end:

  • a default method containing the real body, and
  • a static forwarder, to use for super calls (due to JVM reasons).

Previously, we did that also for the trait initializer methods $init$. However, that causes unrelated default methods to be inherited by Scala classes that extend several traits. In turn, that prevents Java classes from extending such classes.

Now, for the $init$ methods, instead of creating a static forwarder, we move the entire body to a static method. Therefore, we only create a static method, and no default method.

This corresponds to what scalac does as well (both what we do and how we do it), although the previous "discrepancy" was not causing any incompatibility between Scala 2 and 3 per se.

@sjrd sjrd requested a review from smarter November 26, 2020 12:10
@sjrd sjrd linked an issue Nov 26, 2020 that may be closed by this pull request
@sjrd sjrd added this to the 3.0.0-RC1 milestone Nov 26, 2020
@smarter smarter assigned sjrd and unassigned smarter Nov 26, 2020
Concrete trait methods are encoded as two methods in the back-end:

* a default method containing the real body, and
* a static forwarder, to use for super calls (due to JVM reasons).

Previously, we did that also for the trait initializer methods
`$init$`. However, that causes unrelated default methods to be
inherited by Scala classes that extend several traits. In turn,
that prevents Java classes from extending such classes.

Now, for the `$init$` methods, instead of creating a static
forwarder, we *move* the entire body to a static method. Therefore,
we only create a static method, and no default method.

This corresponds to what scalac does as well (both what we do and
how we do it), although the previous "discrepancy" was not causing
any incompatibility between Scala 2 and 3 per se.
@sjrd sjrd force-pushed the trait-init-only-static branch from 4db697b to dec6cc6 Compare November 27, 2020 09:38
@sjrd sjrd merged commit e73fb8a into scala:master Nov 27, 2020
@sjrd sjrd deleted the trait-init-only-static branch November 27, 2020 13:36
@Kordyjan Kordyjan modified the milestones: 3.0.0-M3, 3.0.0 Aug 2, 2023
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 this pull request may close these issues.

java interop: inherits unrelated defaults for $init$() from types
3 participants