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