Skip to content

Commit f6f3b28

Browse files
committed
Initialize static fields correctly
Currently the only way to put initializer inside the static constructor is to use @static. However, CheckStatic restricts that @static can only used in companion object, which is a user-facing check, as MoveStatics can handle @static member in the class. We need a refactoring to make @static a general mechanism that includes a phase to generate the static constructor. This needs to be addressed in another PR.
1 parent 96ae5ae commit f6f3b28

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class Compiler {
5858
new CheckReentrant, // Internal use only: Check that compiled program has no data races involving global vars
5959
new ElimPackagePrefixes, // Eliminate references to package prefixes in Select nodes
6060
new CookComments, // Cook the comments: expand variables, doc, etc.
61-
new CompleteJavaEnums) :: // Fill in constructors for Java enums
62-
List(new CheckStatic, // Check restrictions that apply to @static members
61+
new CheckStatic) :: // Check restrictions that apply to @static members
62+
List(new CompleteJavaEnums, // Fill in constructors for Java enums
6363
new ElimRepeated, // Rewrite vararg parameters and arguments
6464
new ExpandSAMs, // Expand single abstract method closures to anonymous classes
6565
new ProtectedAccessors, // Add accessors for protected members

compiler/src/dotty/tools/dotc/transform/CompleteJavaEnums.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,11 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
117117
val moduleCls = clazz.companionClass
118118
val moduleRef = ref(clazz.companionModule)
119119

120-
val enums = moduleCls.info.decls.filter(member => member.isTerm && member.isAllOf(EnumValue))
120+
val enums = moduleCls.info.decls.filter(member => member.isAllOf(EnumValue))
121121
for { enumValue <- enums }
122122
yield {
123-
val fieldSym = ctx.newSymbol(clazz, enumValue.name.asTermName, EnumValue, enumValue.info)
123+
val fieldSym = ctx.newSymbol(clazz, enumValue.name.asTermName, EnumValue | JavaStatic, enumValue.info)
124+
fieldSym.addAnnotation(Annotations.Annotation(defn.ScalaStaticAnnot))
124125
ValDef(fieldSym, moduleRef.select(enumValue))
125126
}
126127
}

0 commit comments

Comments
 (0)