Skip to content

Commit f4c8aab

Browse files
committed
Don't assume final member class in another comp. unit will stay final
The optimization in scala#5099 that avoided needless capture of the enclosing class, and hence improved serializability of local classes and functions, went too far. It also caused constructor calls of member classes to use `null` rather than the actual outer reference as the `$outer` constructor argument. The enclosed test case exhibits this problem by witnessing an null `Outer.this`. This commit limits the strategy to use `null`-s to the outer refererences of anonymous and local classes, which rules out cross compilation unit effects (in absence of `-opt`.)
1 parent 8721ec3 commit f4c8aab

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/compiler/scala/tools/nsc/transform/Constructors.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ abstract class Constructors extends Statics with Transform with TypingTransforme
764764
primaryConstrBody.expr)
765765
})
766766

767-
if (omittableAccessor.exists(_.isOuterField) && !constructorStats.exists(_.exists { case i: Ident if i.symbol.isOuterParam => true; case _ => false}))
767+
if ((exitingPickler(clazz.isAnonymousClass) || clazz.originalOwner.isTerm) && omittableAccessor.exists(_.isOuterField) && !constructorStats.exists(_.exists { case i: Ident if i.symbol.isOuterParam => true; case _ => false}))
768768
primaryConstructor.symbol.updateAttachment(OuterArgCanBeElided)
769769

770770
val constructors = primaryConstructor :: auxConstructors

test/files/run/t10423/A_1.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Outer {
2+
final class Inner {
3+
def foo: Unit = ()
4+
}
5+
}
6+
object Test {
7+
def main(args: Array[String]): Unit = {
8+
val o = new Outer
9+
new o.Inner().foo
10+
}
11+
}

test/files/run/t10423/A_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Outer {
2+
class Inner {
3+
def foo: Unit = assert(Outer.this ne null)
4+
}
5+
}
6+

0 commit comments

Comments
 (0)