-
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 1 commit
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
Now moveStatics can correctly create static constructors for objects. Those static constructors would later be merged with synthetic module initialisers by GenBCode. This is a bit of magic, it would be good to move all this into this phase.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
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 | ||
|
@@ -20,7 +21,7 @@ class MoveStatics extends MiniPhaseTransform with SymTransformer { thisTransform | |
|
||
|
||
def transformSym(sym: SymDenotation)(implicit ctx: Context): SymDenotation = { | ||
if (sym.hasAnnotation(defn.ScalaStaticAnnot) && sym.owner.is(Flags.Module)) { | ||
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 | ||
|
@@ -34,32 +35,41 @@ class MoveStatics extends MiniPhaseTransform with SymTransformer { thisTransform | |
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 move(companion: TypeDef, module: TypeDef): Thicket = { | ||
if (companion.symbol.is(Flags.Module)) move(module, companion) | ||
def move(module: TypeDef, companion: TypeDef): List[Tree] = { | ||
if (!module.symbol.is(Flags.Module)) move(companion, module) | ||
else { | ||
val allMembers = companion.rhs.asInstanceOf[Template].body ++ module.rhs.asInstanceOf[Template].body | ||
val (newCompanionBody, newModuleBody) = allMembers.partition(x => {assert(x.symbol.exists); x.symbol.owner == companion.symbol}) | ||
def rebuild(orig: TypeDef, newBody: List[Tree]) = { | ||
val oldTemplate = orig.rhs.asInstanceOf[Template] | ||
val statics = newBody.filter(x => x.isInstanceOf[ValDef] && x.symbol.hasAnnotation(defn.ScalaStaticAnnot)).asInstanceOf[List[ValDef]] | ||
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}) | ||
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 (statics.nonEmpty) { | ||
val staticCostructor = ctx.newSymbol(orig.symbol, Names.STATIC_CONSTRUCTOR, Flags.Synthetic | Flags.JavaStatic | Flags.Method, MethodType(Nil, defn.UnitType)) | ||
val staticAssigns = statics.map(x => Assign(ref(x.symbol), x.rhs.changeOwner(x.symbol, staticCostructor))) | ||
if (staticFields.nonEmpty) { | ||
val staticCostructor = ctx.newSymbol(orig.symbol, Names.STATIC_CONSTRUCTOR, Flags.Synthetic | Flags.Method, 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)) | ||
} | ||
Thicket(rebuild(companion, newCompanionBody), rebuild(module, newModuleBody)) | ||
Trees.flatten(rebuild(companion, newCompanionBody) :: rebuild(module, newModuleBody) :: Nil) | ||
} | ||
} | ||
val newPairs = | ||
for ((name, classes) <- pairs) | ||
yield | ||
if (classes.tail.isEmpty) classes.head | ||
if (classes.tail.isEmpty) | ||
if (classes.head.symbol.is(Flags.Module)) move(classes.head, null) | ||
else List(classes.head) | ||
else move(classes.head, classes.tail.head) | ||
Trees.flatten(newPairs.toList ++ others) | ||
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.
✨ :)