Skip to content

Commit 66eefc0

Browse files
committed
err if assign static field in other scalajs unit
1 parent 07de522 commit 66eefc0

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -802,20 +802,25 @@ class JSCodeGen()(using genCtx: Context) {
802802
if (isJSClass && f.isJSExposed)
803803
js.JSFieldDef(flags, genExpr(f.jsName)(f.sourcePos), irTpe) :: Nil
804804
else
805-
val fieldDef = js.FieldDef(flags, encodeFieldSym(f), originalNameOfField(f), irTpe)
806-
val rest =
805+
val fieldIdent = encodeFieldSym(f)
806+
val originalName = originalNameOfField(f)
807+
val fieldDef = js.FieldDef(flags, fieldIdent, originalName, irTpe)
808+
val optionalStaticFieldGetter =
807809
if isStaticField then
810+
// Here we are generating a public static getter for the static field,
811+
// this is its API for other units. This is necessary for singleton
812+
// enum values, which are backed by static fields.
808813
val className = encodeClassName(classSym)
809814
val body = js.Block(
810-
js.LoadModule(className),
811-
js.SelectStatic(className, encodeFieldSym(f))(toIRType(f.info)))
815+
js.LoadModule(className),
816+
js.SelectStatic(className, fieldIdent)(irTpe))
812817
js.MethodDef(js.MemberFlags.empty.withNamespace(js.MemberNamespace.PublicStatic),
813-
encodeStaticMemberSym(f), originalNameOfField(f), Nil, toIRType(f.info),
818+
encodeStaticMemberSym(f), originalName, Nil, irTpe,
814819
Some(body))(
815-
OptimizerHints.empty, None) :: Nil
820+
OptimizerHints.empty, None) :: Nil
816821
else
817822
Nil
818-
fieldDef :: rest
823+
fieldDef :: optionalStaticFieldGetter
819824
}).toList
820825
}
821826

@@ -1451,6 +1456,8 @@ class JSCodeGen()(using genCtx: Context) {
14511456

14521457
case Assign(lhs0, rhs) =>
14531458
val sym = lhs0.symbol
1459+
if (sym.is(JavaStaticTerm) && sym.source != ctx.compilationUnit.source)
1460+
throw new FatalError(s"Assignment to static member ${sym.fullName} not supported")
14541461
def genRhs = genExpr(rhs)
14551462
val lhs = lhs0 match {
14561463
case lhs: Ident => desugarIdent(lhs).getOrElse(lhs)
@@ -3916,12 +3923,14 @@ class JSCodeGen()(using genCtx: Context) {
39163923
(f, true)
39173924
} else*/ {
39183925
val f =
3926+
val className = encodeClassName(sym.owner)
3927+
val fieldIdent = encodeFieldSym(sym)
3928+
val irType = toIRType(sym.info)
3929+
39193930
if sym.is(JavaStatic) then
3920-
js.SelectStatic(encodeClassName(sym.owner),
3921-
encodeFieldSym(sym))(toIRType(sym.info))
3931+
js.SelectStatic(className, fieldIdent)(irType)
39223932
else
3923-
js.Select(qual, encodeClassName(sym.owner),
3924-
encodeFieldSym(sym))(toIRType(sym.info))
3933+
js.Select(qual, className, fieldIdent)(irType)
39253934

39263935
(f, false)
39273936
}

0 commit comments

Comments
 (0)