-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New bridge implementation #2342
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
7 commits
Select commit
Hold shift + click to select a range
4cf4c4d
Erasure: Don't generate forwarders to abstract members.
DarkDimius f6c9a66
Add one more testcase.
DarkDimius e357230
New bridge implementation
odersky a48690b
Change println to debugLog statement
odersky b9f1817
Base decision whether to generate bridge on signatures
odersky 2f7ac63
Remove now unused old implementation
odersky e4d530b
Report more double def errors
odersky 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package dotty.tools | ||
package dotc | ||
package transform | ||
|
||
import core._ | ||
import Symbols._, Types._, Contexts._, Decorators._, SymDenotations._, Flags._, Scopes._ | ||
import DenotTransformers._ | ||
import ast.untpd | ||
import collection.{mutable, immutable} | ||
import TypeErasure._ | ||
import ValueClasses.isDerivedValueClass | ||
|
||
/** A helper class for generating bridge methods in class `root`. */ | ||
class Bridges(root: ClassSymbol)(implicit ctx: Context) { | ||
import ast.tpd._ | ||
|
||
assert(ctx.phase == ctx.erasurePhase.next) | ||
private val preErasureCtx = ctx.withPhase(ctx.erasurePhase) | ||
|
||
private class BridgesCursor(implicit ctx: Context) extends OverridingPairs.Cursor(root) { | ||
|
||
/** Only use the superclass of `root` as a parent class. This means | ||
* overriding pairs that have a common implementation in a trait parent | ||
* are also counted. This is necessary because we generate bridge methods | ||
* only in classes, never in traits. | ||
*/ | ||
override def parents = Array(root.superClass) | ||
override def exclude(sym: Symbol) = !sym.is(Method) || super.exclude(sym) | ||
} | ||
|
||
//val site = root.thisType | ||
|
||
private var toBeRemoved = immutable.Set[Symbol]() | ||
private val bridges = mutable.ListBuffer[Tree]() | ||
private val bridgesScope = newScope | ||
private val bridgeTarget = mutable.HashMap[Symbol, Symbol]() | ||
|
||
/** Add a bridge between `member` and `other`, where `member` overrides `other` | ||
* before erasure, if the following conditions are satisfied. | ||
* | ||
* - `member` and other have different signatures | ||
* - `member` is not inline | ||
* - there is not yet a bridge with the same name and signature in `root` | ||
* | ||
* The bridge has the erased info of `other` and forwards to `member`. | ||
*/ | ||
private def addBridgeIfNeeded(member: Symbol, other: Symbol) = { | ||
def bridgeExists = | ||
bridgesScope.lookupAll(member.name).exists(bridge => | ||
bridgeTarget(bridge) == member && bridge.signature == other.signature) | ||
if (!(member.is(Inline) || member.signature == other.signature || bridgeExists)) | ||
addBridge(member, other) | ||
} | ||
|
||
/** Generate bridge between `member` and `other` | ||
*/ | ||
private def addBridge(member: Symbol, other: Symbol) = { | ||
val bridgePos = if (member.owner == root && member.pos.exists) member.pos else root.pos | ||
val bridge = other.copy( | ||
owner = root, | ||
flags = (member.flags | Method | Bridge | Artifact) &~ | ||
(Accessor | ParamAccessor | CaseAccessor | Deferred | Lazy | Module), | ||
coord = bridgePos).enteredAfter(ctx.erasurePhase.asInstanceOf[DenotTransformer]).asTerm | ||
|
||
ctx.debuglog( | ||
i"""generating bridge from ${other.showLocated}: ${other.info} | ||
|to ${member.showLocated}: ${member.info} @ ${member.pos} | ||
|bridge: ${bridge.showLocated} with flags: ${bridge.flags}""") | ||
|
||
bridgeTarget(bridge) = member | ||
bridgesScope.enter(bridge) | ||
|
||
if (other.owner == root) { | ||
root.delete(other) | ||
toBeRemoved += other | ||
} | ||
|
||
bridges += | ||
DefDef(bridge, This(root).select(member).appliedToArgss(_)).withPos(bridge.pos) | ||
} | ||
|
||
/** Add all necessary bridges to template statements `stats`, and remove at the same | ||
* time deferred methods in `stats` that are replaced by a bridge with the same signature. | ||
*/ | ||
def add(stats: List[untpd.Tree]): List[untpd.Tree] = | ||
if (root.is(Trait)) stats | ||
else { | ||
val opc = new BridgesCursor()(preErasureCtx) | ||
while (opc.hasNext) { | ||
if (!opc.overriding.is(Deferred)) addBridgeIfNeeded(opc.overriding, opc.overridden) | ||
opc.next() | ||
} | ||
if (bridges.isEmpty) stats | ||
else stats.filterNot(stat => toBeRemoved contains stat.symbol) ::: bridges.toList | ||
} | ||
} |
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
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
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,10 @@ | ||
class Meter(val x: Double) extends AnyVal | ||
|
||
trait A { | ||
def apply(x: Double) = x.toString | ||
} | ||
trait B { | ||
def apply(x: Meter) = x.toString | ||
} | ||
|
||
object Test extends A with B // error: double def |
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
File renamed without changes.
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,34 @@ | ||
/* | ||
Minimized from collection strawman | ||
This issue has a lot to do with both mixin and bridge generation and subtleties in JVM spec | ||
if something breaks in this test, this is not a minor issue. Be careful. Here be dragons. | ||
*/ | ||
|
||
trait Define[A] { | ||
protected def coll: Define[A] | ||
def s = coll | ||
} | ||
|
||
trait Iterable[A] extends Define[A] { | ||
protected def coll: this.type = this | ||
} | ||
|
||
trait Seq[A] extends Iterable[A] | ||
|
||
trait Super1[A] { | ||
protected def coll: Iterable[A] | ||
} | ||
|
||
trait Super2[A] extends Super1[A] { | ||
override protected def coll: Seq[A] | ||
} | ||
|
||
class Foo[T] extends Seq[T] with Super2[T] { | ||
} | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
val foo = new Foo[Int] | ||
foo.s | ||
} | ||
} |
Oops, something went wrong.
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.
Why put this in
ElimErasedValueType
? Shouldn't it be a separate mini-phase?