Skip to content

Commit 479fd13

Browse files
committed
Set module class fields to static except bitmap fields
Still we have a problem with the following magic: https://github.com/scala/scala/pull/7270/files#r221195225 It is a magic for two reasons: 1. It generates `getstatic` for `Select(This(O$), x)` 2. It is not affected by compilation order of classes For the second, suppose the backend compiles `A$B` before `A`. Then the class `A$B` does not see that `x` is static, which should cause problem at runtime: ``` object A { private[this] val x: Int = 10 class B { val y = x } } ``` The following are the reasons why it works in scalac: 1. inner classes always come after the outer class 2. non `private[this]` fields are accessed via accessor methods, which are not static 3. inside the module class, access to the fields are compiled after setting the static flag 4. the code generation specialize for trees like `Select(This(O$), x)`, where `x` is a static member
1 parent 38a5aec commit 479fd13

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import dotty.tools.dotc.core.Annotations.Annotation
1616
import dotty.tools.dotc.core.Decorators._
1717
import dotty.tools.dotc.core.Flags._
1818
import dotty.tools.dotc.core.StdNames._
19+
import dotty.tools.dotc.core.NameKinds._
1920
import dotty.tools.dotc.core.Symbols._
2021
import dotty.tools.dotc.core.Types._
2122
import dotty.tools.dotc.core.Contexts._
@@ -129,14 +130,18 @@ trait BCodeSkelBuilder extends BCodeHelpers {
129130
// reverse all the effects of this transformation, which would be counter-productive.
130131

131132

132-
// TODO: enable once we change lazy val encoding (https://github.com/lampepfl/dotty/issues/7140)
133+
// TODO: remove `!f.name.is(LazyBitMapName)` once we change lazy val encoding
134+
// https://github.com/lampepfl/dotty/issues/7140
133135
//
134136
// Lazy val encoding assumes bitmap fields are non-static
135137
//
136138
// See `tests/run/given-var.scala`
137139
//
138-
// for (f <- claszSymbol.info.decls.filter(_.isField))
139-
// f.setFlag(JavaStatic)
140+
141+
claszSymbol.info.decls.foreach { f =>
142+
if f.isField && !f.name.is(LazyBitMapName) then
143+
f.setFlag(JavaStatic)
144+
}
140145

141146
val (clinits, body) = impl.body.partition(stat => stat.isInstanceOf[DefDef] && stat.symbol.isStaticConstructor)
142147

0 commit comments

Comments
 (0)