-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #1877: Add forwarders for primitive/generic mixins. #1883
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
compiler/src/dotty/tools/dotc/transform/PrimitiveForwarders.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package dotty.tools.dotc | ||
| package transform | ||
|
|
||
| import core._ | ||
| import TreeTransforms._ | ||
| import Contexts.Context | ||
| import Flags._ | ||
| import SymUtils._ | ||
| import Symbols._ | ||
| import SymDenotations._ | ||
| import Types._ | ||
| import Decorators._ | ||
| import DenotTransformers._ | ||
| import StdNames._ | ||
| import NameOps._ | ||
| import ast.Trees._ | ||
| import util.Positions._ | ||
| import Names._ | ||
| import collection.mutable | ||
| import ResolveSuper._ | ||
|
|
||
| /** This phase adds forwarder where mixedin generic and primitive typed methods have a missmatch. | ||
| * In particular for every method that is declared both as generic with a primitive type and with a primitive type | ||
| * `<mods> def f[Ts](ps1)...(psN): U` in trait M` and | ||
| * `<mods> def f[Ts](ps1)...(psN): V = ...` in implemented in N` | ||
| * where U is a primitive and V a polymorphic type (or vice versa) needs: | ||
| * | ||
| * <mods> def f[Ts](ps1)...(psN): U = super[N].f[Ts](ps1)...(psN) | ||
| * | ||
| * IMPORTANT: When\If Valhalla happens, we'll need to move mixin before erasure and than this code will need to be rewritten | ||
| * as it will instead change super-class. | ||
| */ | ||
| class PrimitiveForwarders extends MiniPhaseTransform with IdentityDenotTransformer { thisTransform => | ||
| import ast.tpd._ | ||
|
|
||
| override def phaseName: String = "primitiveForwarders" | ||
|
|
||
| override def runsAfter = Set(classOf[ResolveSuper]) | ||
|
|
||
| override def transformTemplate(impl: Template)(implicit ctx: Context, info: TransformerInfo) = { | ||
| val cls = impl.symbol.owner.asClass | ||
| val ops = new MixinOps(cls, thisTransform) | ||
| import ops._ | ||
|
|
||
| def methodPrimitiveForwarders: List[Tree] = | ||
| for (meth <- mixins.flatMap(_.info.decls.flatMap(needsPrimitiveForwarderTo)).distinct) | ||
| yield polyDefDef(implementation(meth.asTerm), forwarder(meth)) | ||
|
|
||
| cpy.Template(impl)(body = methodPrimitiveForwarders ::: impl.body) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| true | ||
| true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
|
|
||
| object Test { | ||
| def main(args: Array[String]): Unit = { | ||
| println((new Foo: Baz).value1) | ||
| println((new Foo: Baz).value2) | ||
| } | ||
| } | ||
|
|
||
| class Foo extends Bar[Boolean](true) with Baz | ||
|
|
||
| class Bar[T](x: T) { | ||
| def value1: T = x | ||
| def value2(): T = x | ||
| } | ||
|
|
||
| trait Baz { | ||
| def value1: Boolean | ||
| def value2(): Boolean | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| true | ||
| true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
|
|
||
| object Test { | ||
| def main(args: Array[String]): Unit = { | ||
| println((new Foo: Bar[Boolean]).value1) | ||
| println((new Foo: Bar[Boolean]).value2) | ||
| } | ||
| } | ||
|
|
||
| class Foo extends Baz with Bar[Boolean] | ||
|
|
||
| trait Bar[T] { | ||
| def value1: T | ||
| def value2(): T | ||
| } | ||
|
|
||
| class Baz { | ||
| def value1: Boolean = true | ||
| def value2(): Boolean = true | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| true | ||
| true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
|
|
||
| object Test { | ||
| def main(args: Array[String]): Unit = { | ||
| println((new Foo: Baz).value) | ||
| println((new Foo: Qux).value) | ||
| } | ||
| } | ||
|
|
||
| class Foo extends Bar[Boolean](true) with Baz with Qux | ||
|
|
||
| class Bar[T](x: T) { | ||
| def value: T = x | ||
| } | ||
|
|
||
| trait Baz { | ||
| def value: Boolean | ||
| } | ||
|
|
||
| trait Qux { | ||
| def value: Boolean | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| true | ||
| true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
|
|
||
| object Test { | ||
| def main(args: Array[String]): Unit = { | ||
| println((new Foo: Baz).value1.v) | ||
| println((new Foo: Baz).value2.v) | ||
| } | ||
| } | ||
|
|
||
| class Foo extends Bar[VBoolean](new VBoolean(true)) with Baz | ||
|
|
||
| class Bar[T](x: T) { | ||
| def value1: T = x | ||
| def value2(): T = x | ||
| } | ||
|
|
||
| trait Baz { | ||
| def value1: VBoolean | ||
| def value2(): VBoolean | ||
| } | ||
|
|
||
| class VBoolean(val v: Boolean) extends AnyVal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| true | ||
| true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
|
|
||
| object Test { | ||
| def main(args: Array[String]): Unit = { | ||
| println((new Foo: Bar[VBoolean]).value1.v) | ||
| println((new Foo: Bar[VBoolean]).value2.v) | ||
| } | ||
| } | ||
|
|
||
| class Foo extends Baz with Bar[VBoolean] | ||
|
|
||
| trait Bar[T] { | ||
| def value1: T | ||
| def value2(): T | ||
| } | ||
|
|
||
| class Baz { | ||
| def value1: VBoolean = new VBoolean(true) | ||
| def value2(): VBoolean = new VBoolean(true) | ||
| } | ||
|
|
||
| class VBoolean(val v: Boolean) extends AnyVal |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need a test with a value class, also it would be nice to check that the method called would be the unboxed one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in
mixin-primitive-on-generic-4.scalaandmixin-primitive-on-generic-5.scalaand I checked that the method called is the unboxed one.