-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Support Scala 2.12 Mixins #2685
Merged
DarkDimius
merged 10 commits into
scala:master
from
dotty-staging:add-scala12-mixins-v2
Jun 14, 2017
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d64b271
Faster base class sets
odersky e5362ad
Mark traits coming from Scala 2.12+ with flag
odersky 2e4d159
Support Scala 2.12 mixins
odersky 8f80923
Rename miniphase
odersky b82f7eb
Add test case
odersky 4b6f6a5
Fix rename breakage
odersky ade4bbe
Change phase name
odersky b4b8d64
Fix test
odersky f521a5a
Address reviewers comments
odersky e0057da
Remove debug info
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 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 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 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 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 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 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 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 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 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 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
62 changes: 0 additions & 62 deletions
62
compiler/src/dotty/tools/dotc/transform/LinkScala2ImplClasses.scala
This file was deleted.
Oops, something went wrong.
100 changes: 100 additions & 0 deletions
100
compiler/src/dotty/tools/dotc/transform/LinkScala2Impls.scala
This file contains 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,100 @@ | ||
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 Phases._ | ||
import ast.untpd | ||
import ast.Trees._ | ||
import NameKinds.ImplMethName | ||
import collection.mutable | ||
|
||
/** Rewrite calls | ||
* | ||
* super[M].f(args) | ||
* | ||
* where M is a Scala 2.11 trait implemented by the current class to | ||
* | ||
* M$class.f(this, args) | ||
* | ||
* provided the implementation class M$class defines a corresponding function `f`. | ||
* If M is a Scala 2.12 or newer trait, rewrite to | ||
* | ||
* M.f(this, args) | ||
* | ||
* where f is a static member of M. | ||
*/ | ||
class LinkScala2Impls extends MiniPhase with IdentityDenotTransformer { thisTransform => | ||
import ast.tpd._ | ||
|
||
override def phaseName: String = "linkScala2Impls" | ||
override def changesMembers = true | ||
val treeTransform = new Transform | ||
|
||
override def runsAfterGroupsOf: Set[Class[_ <: Phase]] = Set(classOf[Mixin]) | ||
// Adds as a side effect static members to traits which can confuse Mixin, | ||
// that's why it is runsAfterGroupOf | ||
|
||
class Transform extends TreeTransform { | ||
def phase = thisTransform | ||
|
||
/** Copy definitions from implementation class to trait itself */ | ||
private def augmentScala_2_12_Trait(mixin: ClassSymbol)(implicit ctx: Context): Unit = { | ||
def newImpl(sym: TermSymbol): Symbol = sym.copy( | ||
owner = mixin, | ||
name = if (sym.isConstructor) sym.name else ImplMethName(sym.name) | ||
) | ||
for (sym <- mixin.implClass.info.decls) | ||
newImpl(sym.asTerm).enteredAfter(thisTransform) | ||
} | ||
|
||
override def prepareForTemplate(impl: Template)(implicit ctx: Context) = { | ||
val cls = impl.symbol.owner.asClass | ||
for (mixin <- cls.mixins) | ||
if (mixin.is(Scala_2_12_Trait, butNot = Scala_2_12_Augmented)) { | ||
augmentScala_2_12_Trait(mixin) | ||
mixin.setFlag(Scala_2_12_Augmented) | ||
} | ||
this | ||
} | ||
|
||
override def transformApply(app: Apply)(implicit ctx: Context, info: TransformerInfo) = { | ||
def currentClass = ctx.owner.enclosingClass.asClass | ||
app match { | ||
case Apply(sel @ Select(Super(_, _), _), args) | ||
if sel.symbol.owner.is(Scala2xTrait) && currentClass.mixins.contains(sel.symbol.owner) => | ||
val impl = implMethod(sel.symbol) | ||
if (impl.exists) Apply(ref(impl), This(currentClass) :: args).withPos(app.pos) | ||
else app // could have been an abstract method in a trait linked to from a super constructor | ||
case _ => | ||
app | ||
} | ||
} | ||
|
||
private def implMethod(meth: Symbol)(implicit ctx: Context): Symbol = { | ||
val (implInfo, implName) = | ||
if (meth.owner.is(Scala_2_12_Trait)) | ||
(meth.owner.info, ImplMethName(meth.name.asTermName)) | ||
else | ||
(meth.owner.implClass.info, meth.name) | ||
if (meth.isConstructor) | ||
implInfo.decl(nme.TRAIT_CONSTRUCTOR).symbol | ||
else | ||
implInfo.decl(implName) | ||
.suchThat(c => FullParameterization.memberSignature(c.info) == meth.signature) | ||
.symbol | ||
} | ||
} | ||
|
||
private val Scala2xTrait = allOf(Scala2x, Trait) | ||
} |
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.
it would be nice to leave a note that it is used as heuristic to find scala 2.12