-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Drop empty companion objects #1075
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
5 commits
Select commit
Hold shift + click to select a range
c16ead8
Abstract out lazy local names somewhat
odersky df7d891
New phase to drop empty companion objects
odersky 24ccb77
Move test to pending
odersky 1839369
Only remove synthetic companion objects
odersky d3c3317
Fix problems in DropEmptyCompanions
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
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,98 @@ | ||
package dotty.tools.dotc | ||
package transform | ||
|
||
import core._ | ||
import DenotTransformers.SymTransformer | ||
import Phases.Phase | ||
import Contexts.Context | ||
import Flags._ | ||
import Symbols._ | ||
import SymDenotations.SymDenotation | ||
import ast.Trees._ | ||
import collection.mutable | ||
import Decorators._ | ||
import NameOps._ | ||
import TreeTransforms.MiniPhaseTransform | ||
import dotty.tools.dotc.transform.TreeTransforms.TransformerInfo | ||
|
||
/** Remove companion objects that are empty | ||
* Lots of constraints here: | ||
* 1. It's impractical to place DropEmptyCompanions before lambda lift because dropped | ||
* modules can be anywhere and have hard to trace references. | ||
* 2. DropEmptyCompanions cannot be interleaved with LambdaLift or Flatten because | ||
* they put things in liftedDefs sets which cause them to surface later. So | ||
* removed modules resurface. | ||
* 3. DropEmptyCompanions has to be before RestoreScopes. | ||
* The solution to the constraints is to put DropEmptyCompanions between Flatten | ||
* and RestoreScopes and to only start working once we are back on PackageDef | ||
* level, so we know that all objects moved by LambdaLift and Flatten have arrived | ||
* at their destination. | ||
*/ | ||
class DropEmptyCompanions extends MiniPhaseTransform { thisTransform => | ||
import ast.tpd._ | ||
override def phaseName = "dropEmpty" | ||
override def runsAfter: Set[Class[_ <: Phase]] = Set(classOf[Flatten]) | ||
|
||
override def transformPackageDef(pdef: PackageDef)(implicit ctx: Context, info: TransformerInfo) = { | ||
|
||
/** Is `tree` an empty companion object? */ | ||
def isEmptyCompanion(tree: Tree) = tree match { | ||
case TypeDef(_, impl: Template) if tree.symbol.is(SyntheticModule) && | ||
tree.symbol.companionClass.exists && | ||
impl.body.forall(_.symbol.isPrimaryConstructor) => | ||
println(i"removing ${tree.symbol}") | ||
true | ||
case _ => | ||
false | ||
} | ||
|
||
val dropped = pdef.stats.filter(isEmptyCompanion).map(_.symbol).toSet | ||
|
||
/** Symbol is a $lzy field representing a module */ | ||
def isLazyModuleVar(sym: Symbol) = | ||
sym.name.isLazyLocal && | ||
sym.owner.info.decl(sym.name.asTermName.nonLazyName).symbol.is(Module) | ||
|
||
/** Symbol should be dropped together with a dropped companion object. | ||
* Such symbols are: | ||
* - lzy fields pointing to modules, | ||
* - vals and getters representing modules. | ||
*/ | ||
def symIsDropped(sym: Symbol): Boolean = | ||
(sym.is(Module) || isLazyModuleVar(sym)) && | ||
dropped.contains(sym.info.resultType.typeSymbol) | ||
|
||
/** Tree should be dropped because it (is associated with) an empty | ||
* companion object. Such trees are | ||
* - module classes of empty companion objects | ||
* - definitions of lazy module variables or assignments to them. | ||
* - vals and getters for empty companion objects | ||
*/ | ||
def toDrop(stat: Tree): Boolean = stat match { | ||
case stat: TypeDef => dropped.contains(stat.symbol) | ||
case stat: ValOrDefDef => symIsDropped(stat.symbol) | ||
case stat: Assign => symIsDropped(stat.lhs.symbol) | ||
case _ => false | ||
} | ||
|
||
def prune(tree: Tree): Tree = tree match { | ||
case tree @ TypeDef(name, impl @ Template(constr, _, _, _)) => | ||
cpy.TypeDef(tree)( | ||
rhs = cpy.Template(impl)( | ||
constr = cpy.DefDef(constr)(rhs = pruneLocals(constr.rhs)), | ||
body = pruneStats(impl.body))) | ||
case _ => | ||
tree | ||
} | ||
|
||
def pruneStats(stats: List[Tree]) = | ||
stats.filterConserve(!toDrop(_)).mapConserve(prune) | ||
|
||
def pruneLocals(expr: Tree) = expr match { | ||
case Block(stats, expr) => cpy.Block(expr)(pruneStats(stats), expr) | ||
case _ => expr | ||
} | ||
|
||
cpy.PackageDef(pdef)(pdef.pid, pruneStats(pdef.stats)) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -63,8 +63,8 @@ class LazyVals extends MiniPhaseTransform with IdentityDenotTransformer { | |
|
||
if (isField) { | ||
if (sym.isVolatile || | ||
(sym.is(Flags.Module) && !sym.is(Flags.Synthetic))) | ||
// module class is user-defined. | ||
(sym.is(Flags.Module) && !sym.is(Flags.Synthetic))) | ||
// module class is user-defined. | ||
// Should be threadsafe, to mimic safety guaranteed by global object | ||
transformMemberDefVolatile(tree) | ||
else if (sym.is(Flags.Module)) { // synthetic module | ||
|
@@ -101,7 +101,7 @@ class LazyVals extends MiniPhaseTransform with IdentityDenotTransformer { | |
*/ | ||
def transformSyntheticModule(tree: ValOrDefDef)(implicit ctx: Context) = { | ||
val sym = tree.symbol | ||
val holderSymbol = ctx.newSymbol(sym.owner, sym.asTerm.name ++ nme.LAZY_LOCAL, | ||
val holderSymbol = ctx.newSymbol(sym.owner, sym.asTerm.name.lazyLocalName, | ||
Flags.Synthetic, sym.info.widen.resultType).enteredAfter(this) | ||
val field = ValDef(holderSymbol, tree.rhs.changeOwnerAfter(sym, holderSymbol, this)) | ||
val getter = DefDef(sym.asTerm, ref(holderSymbol)) | ||
|
@@ -114,7 +114,7 @@ class LazyVals extends MiniPhaseTransform with IdentityDenotTransformer { | |
*/ | ||
def transformLocalDef(x: ValOrDefDef)(implicit ctx: Context) = { | ||
val valueInitter = x.rhs | ||
val holderName = ctx.freshName(x.name ++ StdNames.nme.LAZY_LOCAL).toTermName | ||
val holderName = ctx.freshName(x.name.asTermName.lazyLocalName).toTermName | ||
val initName = ctx.freshName(x.name ++ StdNames.nme.LAZY_LOCAL_INIT).toTermName | ||
val tpe = x.tpe.widen.resultType.widen | ||
|
||
|
@@ -206,7 +206,7 @@ class LazyVals extends MiniPhaseTransform with IdentityDenotTransformer { | |
val claz = x.symbol.owner.asClass | ||
val tpe = x.tpe.widen.resultType.widen | ||
assert(!(x.mods is Flags.Mutable)) | ||
val containerName = ctx.freshName(x.name ++ StdNames.nme.LAZY_LOCAL).toTermName | ||
val containerName = ctx.freshName(x.name.asTermName.lazyLocalName).toTermName | ||
val containerSymbol = ctx.newSymbol(claz, containerName, | ||
x.symbol.flags &~ containerFlagsMask | containerFlags | Flags.Private, | ||
tpe, coord = x.symbol.coord | ||
|
@@ -367,7 +367,7 @@ class LazyVals extends MiniPhaseTransform with IdentityDenotTransformer { | |
appendOffsetDefs += (companion.moduleClass -> new OffsetInfo(List(offsetTree), ord)) | ||
} | ||
|
||
val containerName = ctx.freshName(x.name ++ StdNames.nme.LAZY_LOCAL).toTermName | ||
val containerName = ctx.freshName(x.name.asTermName.lazyLocalName).toTermName | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @DarkDimius This is just DRY in order not to do the same low-level name plumbing over and over again. |
||
val containerSymbol = ctx.newSymbol(claz, containerName, (x.mods &~ containerFlagsMask | containerFlags).flags, tpe, coord = x.symbol.coord).enteredAfter(this) | ||
val containerTree = ValDef(containerSymbol, defaultValue(tpe)) | ||
|
||
|
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 |
---|---|---|
|
@@ -17,4 +17,9 @@ object Test { | |
def main(args : Array[String]) : Unit = { | ||
b.initialize; | ||
} | ||
class XYZ | ||
} | ||
|
||
|
||
|
||
|
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.
println left in code.
I'll make a pr to remove it.
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's already removed in another PR of mine. So it should go away by itself
soon.
On Thu, Feb 18, 2016 at 11:23 AM, Dmitry Petrashko <[email protected]
Martin Odersky
EPFL