|
1 |
| -package dotty.tools.dotc |
| 1 | +package dotty.tools |
| 2 | +package dotc |
2 | 3 | package transform
|
3 | 4 |
|
4 | 5 | import core.*
|
@@ -183,23 +184,29 @@ abstract class Dependencies(root: ast.tpd.Tree, @constructorOnly rootContext: Co
|
183 | 184 | def setLogicOwner(local: Symbol) =
|
184 | 185 | val encClass = local.owner.enclosingClass
|
185 | 186 | val preferEncClass =
|
186 |
| - ( |
187 | 187 | encClass.isStatic
|
188 | 188 | // non-static classes can capture owners, so should be avoided
|
189 | 189 | && (encClass.isProperlyContainedIn(local.topLevelClass)
|
190 | 190 | // can be false for symbols which are defined in some weird combination of supercalls.
|
191 | 191 | || encClass.is(ModuleClass, butNot = Package)
|
192 | 192 | // needed to not cause deadlocks in classloader. see t5375.scala
|
193 | 193 | )
|
194 |
| - ) |
195 |
| - || ( |
| 194 | + && (!sym.isAnonymousFunction || sym.owner.ownersIterator.exists(_.isConstructor)) |
| 195 | + // For anonymous functions in static objects, we prefer them to be static because |
| 196 | + // that means lambdas are memoized and can be serialized even if the enclosing object |
| 197 | + // is not serializable. See run/lambda-serialization-gc.scala and run/i19224.scala. |
| 198 | + // On the other hand, we don't want to lift anonymous functions from inside the |
| 199 | + // object constructor to be static since that can cause deadlocks by its interaction |
| 200 | + // with class initialization. See run/deadlock.scala, which works in Scala 3 |
| 201 | + // but deadlocks in Scala 2. |
| 202 | + || |
196 | 203 | /* Scala.js: Never move any member beyond the boundary of a DynamicImportThunk.
|
197 | 204 | * DynamicImportThunk subclasses are boundaries between the eventual ES modules
|
198 | 205 | * that can be dynamically loaded. Moving members across that boundary changes
|
199 | 206 | * the dynamic and static dependencies between ES modules, which is forbidden.
|
200 | 207 | */
|
201 | 208 | ctx.settings.scalajs.value && encClass.isSubClass(jsdefn.DynamicImportThunkClass)
|
202 |
| - ) |
| 209 | + |
203 | 210 | logicOwner(sym) = if preferEncClass then encClass else local.enclosingPackageClass
|
204 | 211 |
|
205 | 212 | tree match
|
|
0 commit comments