-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Multiple fixes to @static #1226
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
Changes from all commits
2e5ab54
7408f14
bbd48d3
806a029
db6a07d
4ce8ab0
27846bb
63eb88d
ce2b964
7cacd0f
1932b17
f650b1e
61fe99b
d9702d2
e1fcb4c
990e962
f2cfac5
fa6deee
5f73175
49ace48
2c6a9be
9899a06
c428e74
de45fa1
3c93c5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package dotty.tools.dotc.transform | ||
|
||
import dotty.tools.dotc.ast.{Trees, tpd} | ||
import dotty.tools.dotc.core.Annotations.Annotation | ||
import dotty.tools.dotc.core.Contexts.Context | ||
import dotty.tools.dotc.core.DenotTransformers.{InfoTransformer, SymTransformer} | ||
import dotty.tools.dotc.core.SymDenotations.SymDenotation | ||
import dotty.tools.dotc.core.Decorators._ | ||
import dotty.tools.dotc.core.NameOps._ | ||
import dotty.tools.dotc.core.{Flags, Names} | ||
import dotty.tools.dotc.core.Names.Name | ||
import dotty.tools.dotc.core.Symbols._ | ||
import dotty.tools.dotc.core.Types.MethodType | ||
import dotty.tools.dotc.transform.TreeTransforms.{MiniPhaseTransform, TransformerInfo} | ||
|
||
/** Move static methods from companion to the class itself */ | ||
class MoveStatics extends MiniPhaseTransform with SymTransformer { thisTransformer => | ||
|
||
import tpd._ | ||
override def phaseName = "moveStatic" | ||
|
||
|
||
def transformSym(sym: SymDenotation)(implicit ctx: Context): SymDenotation = { | ||
if (sym.hasAnnotation(defn.ScalaStaticAnnot) && sym.owner.is(Flags.Module) && sym.owner.companionClass.exists) { | ||
sym.owner.asClass.delete(sym.symbol) | ||
sym.owner.companionClass.asClass.enter(sym.symbol) | ||
val flags = if (sym.is(Flags.Method)) sym.flags else sym.flags | Flags.Mutable | ||
sym.copySymDenotation(owner = sym.owner.companionClass, initFlags = flags) | ||
} | ||
else sym | ||
} | ||
|
||
override def transformStats(trees: List[Tree])(implicit ctx: Context, info: TransformerInfo): List[Tree] = { | ||
if (ctx.owner.is(Flags.Package)) { | ||
val (classes, others) = trees.partition(x => x.isInstanceOf[TypeDef] && x.symbol.isClass) | ||
val pairs = classes.groupBy(_.symbol.name.stripModuleClassSuffix).asInstanceOf[Map[Name, List[TypeDef]]] | ||
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. why do you need the cast? 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. Static type is 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. oh i see now -- since you don't use |
||
|
||
def rebuild(orig: TypeDef, newBody: List[Tree]): Tree = { | ||
if (orig eq null) return EmptyTree | ||
|
||
val staticFields = newBody.filter(x => x.isInstanceOf[ValDef] && x.symbol.hasAnnotation(defn.ScalaStaticAnnot)).asInstanceOf[List[ValDef]] | ||
val newBodyWithStaticConstr = | ||
if (staticFields.nonEmpty) { | ||
/* do NOT put Flags.JavaStatic here. It breaks .enclosingClass */ | ||
val staticCostructor = ctx.newSymbol(orig.symbol, Names.STATIC_CONSTRUCTOR, Flags.Synthetic | Flags.Method | Flags.Private, MethodType(Nil, defn.UnitType)) | ||
staticCostructor.addAnnotation(Annotation(defn.ScalaStaticAnnot)) | ||
staticCostructor.entered | ||
|
||
val staticAssigns = staticFields.map(x => Assign(ref(x.symbol), x.rhs.changeOwner(x.symbol, staticCostructor))) | ||
tpd.DefDef(staticCostructor, Block(staticAssigns, tpd.unitLiteral)) :: newBody | ||
} else newBody | ||
|
||
val oldTemplate = orig.rhs.asInstanceOf[Template] | ||
cpy.TypeDef(orig)(rhs = cpy.Template(orig.rhs)(oldTemplate.constr, oldTemplate.parents, oldTemplate.self, newBodyWithStaticConstr)) | ||
} | ||
|
||
def move(module: TypeDef, companion: TypeDef): List[Tree] = { | ||
if (!module.symbol.is(Flags.Module)) move(companion, module) | ||
else { | ||
val allMembers = | ||
(if(companion ne null) {companion.rhs.asInstanceOf[Template].body} else Nil) ++ | ||
module.rhs.asInstanceOf[Template].body | ||
val (newModuleBody, newCompanionBody) = allMembers.partition(x => {assert(x.symbol.exists); x.symbol.owner == module.symbol}) | ||
Trees.flatten(rebuild(companion, newCompanionBody) :: rebuild(module, newModuleBody) :: Nil) | ||
} | ||
} | ||
val newPairs = | ||
for ((name, classes) <- pairs) | ||
yield | ||
if (classes.tail.isEmpty) | ||
if (classes.head.symbol.is(Flags.Module)) move(classes.head, null) | ||
else List(rebuild(classes.head, classes.head.rhs.asInstanceOf[Template].body)) | ||
else move(classes.head, classes.tail.head) | ||
Trees.flatten(newPairs.toList.flatten ++ others) | ||
} else trees | ||
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. maybe invert the condition and move this case up -- makes it easier to follow |
||
} | ||
} |
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.
do you need to remove / unlink the symbol from the module's scope?
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.
I'm kind of intentionally not doing so.
To make sure it can be found in both places.
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.
ok, i don't know if that's a good idea (literally - maybe it is..)
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.
Note here we're already near the backend, so I'm preparing the tree specifically for GenBCode.
Phases that go in this block are backend-specific and break some assumptions of compiler.
Eg
LabelDefs
reorders<label> def
s in a magical order that no one should touch.Uh oh!
There was an error while loading. Please reload this page.
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.
maybe it's related to exactly that discussion: the current PR creates a static field in both
T
andT$
the one in the module class is not initialized.
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.
✨ :)