Skip to content

Commit de45fa1

Browse files
committed
MoveStatics: Fix classes without companion not getting static <clinit>
This broke lazy vals, as unsafe offsets were not initialised.
1 parent c428e74 commit de45fa1

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

src/dotty/tools/dotc/transform/MoveStatics.scala

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,32 @@ class MoveStatics extends MiniPhaseTransform with SymTransformer { thisTransform
3535
val (classes, others) = trees.partition(x => x.isInstanceOf[TypeDef] && x.symbol.isClass)
3636
val pairs = classes.groupBy(_.symbol.name.stripModuleClassSuffix).asInstanceOf[Map[Name, List[TypeDef]]]
3737

38+
def rebuild(orig: TypeDef, newBody: List[Tree]): Tree = {
39+
if (orig eq null) return EmptyTree
40+
41+
val staticFields = newBody.filter(x => x.isInstanceOf[ValDef] && x.symbol.hasAnnotation(defn.ScalaStaticAnnot)).asInstanceOf[List[ValDef]]
42+
val newBodyWithStaticConstr =
43+
if (staticFields.nonEmpty) {
44+
/* do NOT put Flags.JavaStatic here. It breaks .enclosingClass */
45+
val staticCostructor = ctx.newSymbol(orig.symbol, Names.STATIC_CONSTRUCTOR, Flags.Synthetic | Flags.Method, MethodType(Nil, defn.UnitType))
46+
staticCostructor.addAnnotation(Annotation(defn.ScalaStaticAnnot))
47+
staticCostructor.entered
48+
49+
val staticAssigns = staticFields.map(x => Assign(ref(x.symbol), x.rhs.changeOwner(x.symbol, staticCostructor)))
50+
tpd.DefDef(staticCostructor, Block(staticAssigns, tpd.unitLiteral)) :: newBody
51+
} else newBody
52+
53+
val oldTemplate = orig.rhs.asInstanceOf[Template]
54+
cpy.TypeDef(orig)(rhs = cpy.Template(orig.rhs)(oldTemplate.constr, oldTemplate.parents, oldTemplate.self, newBodyWithStaticConstr))
55+
}
56+
3857
def move(module: TypeDef, companion: TypeDef): List[Tree] = {
3958
if (!module.symbol.is(Flags.Module)) move(companion, module)
4059
else {
4160
val allMembers =
4261
(if(companion ne null) {companion.rhs.asInstanceOf[Template].body} else Nil) ++
4362
module.rhs.asInstanceOf[Template].body
4463
val (newModuleBody, newCompanionBody) = allMembers.partition(x => {assert(x.symbol.exists); x.symbol.owner == module.symbol})
45-
def rebuild(orig: TypeDef, newBody: List[Tree]): Tree = {
46-
if (orig eq null) return EmptyTree
47-
48-
val staticFields = newBody.filter(x => x.isInstanceOf[ValDef] && x.symbol.hasAnnotation(defn.ScalaStaticAnnot)).asInstanceOf[List[ValDef]]
49-
val newBodyWithStaticConstr =
50-
if (staticFields.nonEmpty) {
51-
/* do NOT put Flags.JavaStatic here. It breaks .enclosingClass */
52-
val staticCostructor = ctx.newSymbol(orig.symbol, Names.STATIC_CONSTRUCTOR, Flags.Synthetic | Flags.Method, MethodType(Nil, defn.UnitType))
53-
staticCostructor.addAnnotation(Annotation(defn.ScalaStaticAnnot))
54-
staticCostructor.entered
55-
56-
val staticAssigns = staticFields.map(x => Assign(ref(x.symbol), x.rhs.changeOwner(x.symbol, staticCostructor)))
57-
tpd.DefDef(staticCostructor, Block(staticAssigns, tpd.unitLiteral)) :: newBody
58-
} else newBody
59-
60-
val oldTemplate = orig.rhs.asInstanceOf[Template]
61-
cpy.TypeDef(orig)(rhs = cpy.Template(orig.rhs)(oldTemplate.constr, oldTemplate.parents, oldTemplate.self, newBodyWithStaticConstr))
62-
}
6364
Trees.flatten(rebuild(companion, newCompanionBody) :: rebuild(module, newModuleBody) :: Nil)
6465
}
6566
}
@@ -68,7 +69,7 @@ class MoveStatics extends MiniPhaseTransform with SymTransformer { thisTransform
6869
yield
6970
if (classes.tail.isEmpty)
7071
if (classes.head.symbol.is(Flags.Module)) move(classes.head, null)
71-
else List(classes.head)
72+
else List(rebuild(classes.head, classes.head.rhs.asInstanceOf[Template].body))
7273
else move(classes.head, classes.tail.head)
7374
Trees.flatten(newPairs.toList.flatten ++ others)
7475
} else trees

0 commit comments

Comments
 (0)