Skip to content

Commit dc31c9d

Browse files
committed
add comment about special case
1 parent 66eefc0 commit dc31c9d

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,18 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
105105
val enums = moduleCls.info.decls.filter(member => member.isAllOf(EnumValue))
106106
for { enumValue <- enums }
107107
yield {
108+
def forwarderSym(flags: FlagSet, info: Type): Symbol { type ThisName = TermName } =
109+
val sym = newSymbol(clazz, enumValue.name.asTermName, flags, info)
110+
sym.addAnnotation(Annotations.Annotation(defn.ScalaStaticAnnot))
111+
sym
112+
val body = moduleRef.select(enumValue)
108113
if ctx.settings.scalajs.value then
109-
val methodSym = newSymbol(clazz, enumValue.name.asTermName, EnumValue | Method | JavaStatic, MethodType(Nil, enumValue.info))
110-
methodSym.addAnnotation(Annotations.Annotation(defn.ScalaStaticAnnot))
111-
DefDef(methodSym, moduleRef.select(enumValue))
114+
// Scala.js has no support for <clinit> so we must avoid assigning static fields in the enum class.
115+
// However, since the public contract for reading static fields in the IR ABI is to call "static getters",
116+
// we achieve the right contract with static forwarders instead.
117+
DefDef(forwarderSym(EnumValue | Method | JavaStatic, MethodType(Nil, enumValue.info)), body)
112118
else
113-
val fieldSym = newSymbol(clazz, enumValue.name.asTermName, EnumValue | JavaStatic, enumValue.info)
114-
fieldSym.addAnnotation(Annotations.Annotation(defn.ScalaStaticAnnot))
115-
ValDef(fieldSym, moduleRef.select(enumValue))
119+
ValDef(forwarderSym(EnumValue | JavaStatic, enumValue.info), body)
116120
}
117121
}
118122

0 commit comments

Comments
 (0)