diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2016-04-15 14:05:02 +1000 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2016-06-03 11:28:08 +1000 |
commit | f07019ffa56ec2dfab8ab0d9a83133005761a877 (patch) | |
tree | 44b0a9ee54c662885ee96ebff80dc8b181976ebf /test | |
parent | e077c24525bf8f9bd8b73684e630eb7fc6bcb5f6 (diff) | |
download | scala-f07019ffa56ec2dfab8ab0d9a83133005761a877.tar.gz scala-f07019ffa56ec2dfab8ab0d9a83133005761a877.tar.bz2 scala-f07019ffa56ec2dfab8ab0d9a83133005761a877.zip |
SI-9390 Avoid needless outer capture with local classes
An existing optimization in `Constructors` elides the outer
field in member and local classes, if the class doesn't use
the outer reference. (Member classes also need to be final,
which is a secret handshake to say we're also happy to weaken
prefix matching in the pattern matcher.)
That optimization leaves the constructor signature as is: the
constructor still accepts the outer instance, but does not store
it. For member classes, this means that we can separately compile
code that calls the constructor.
Local classes need not be hampered by this constraint, we could
remove the outer instance from the constructor call too.
Why would we want to do this?
Let's look at the case before and after this commit.
Before:
```
class C extends Object {
def foo(): Function1 = $anonfun();
final <static> <artifact> def $anonfun$foo$1($this: C, x: Object): Object = new <$anon: Object>($this);
def <init>(): C = {
C.super.<init>();
()
}
};
final class anon$1 extends Object {
def <init>($outer: C): <$anon: Object> = {
anon$1.super.<init>();
()
}
}
```
After:
```
class C extends Object {
def foo(): Function1 = $anonfun();
final <static> <artifact> def $anonfun$foo$1(x: Object): Object = new <$anon: Object>(null);
def <init>(): C = {
C.super.<init>();
()
}
};
final class anon$1 extends Object {
def <init>($outer: C): <$anon: Object> = {
anon$1.super.<init>();
()
}
}
```
However, the status quo means that a lambda that
This in turn makes lambdas that refer to such classes serializable
even when the outer class is not itself serialiable.
I have not attempted to extend this to calls to secondary constructors.
Diffstat (limited to 'test')
-rw-r--r-- | test/files/jvm/t9105.check | 14 | ||||
-rw-r--r-- | test/files/run/t9390c.scala | 21 | ||||
-rw-r--r-- | test/files/run/t9390d.scala | 12 |
3 files changed, 35 insertions, 12 deletions
diff --git a/test/files/jvm/t9105.check b/test/files/jvm/t9105.check index 48439ee004..9447e0cf29 100644 --- a/test/files/jvm/t9105.check +++ b/test/files/jvm/t9105.check @@ -1,18 +1,8 @@ -#partest -Ydelambdafy:inline -(class C$$anonfun$1$A$1,class C$$anonfun$1,null) -(class C$$anonfun$1$B$1,class C$$anonfun$1,private final java.lang.Object C$$anonfun$1.m$1()) -(class C$$anonfun$1$C$1,class C$$anonfun$1,null) -(class C$$anonfun$1$$anonfun$2$D$1,class C$$anonfun$1$$anonfun$2,null) -(class C$$anonfun$met$1$E$1,class C$$anonfun$met$1,null) -(class C$$anonfun$met$1$F$1,class C$$anonfun$met$1,private final java.lang.Object C$$anonfun$met$1.m$2()) -(class C$$anonfun$met$1$G$1,class C$$anonfun$met$1,null) -(class C$$anonfun$met$1$$anonfun$3$H$1,class C$$anonfun$met$1$$anonfun$3,null) -#partest !-Ydelambdafy:inline (class C$A$1,class C,null) -(class C$B$1,class C,private final java.lang.Object C.m$1()) +(class C$B$1,class C,private static final java.lang.Object C.m$1()) (class C$C$1,class C,null) (class C$D$1,class C,null) (class C$E$1,class C,public scala.Function0 C.met()) -(class C$F$1,class C,private final java.lang.Object C.m$2()) +(class C$F$1,class C,private static final java.lang.Object C.m$2()) (class C$G$1,class C,public scala.Function0 C.met()) (class C$H$1,class C,public scala.Function0 C.met()) diff --git a/test/files/run/t9390c.scala b/test/files/run/t9390c.scala new file mode 100644 index 0000000000..db39da57cd --- /dev/null +++ b/test/files/run/t9390c.scala @@ -0,0 +1,21 @@ +class C { // C is not serializable + def foo = { + { (x: Any) => new Object {} } + } +} +object Test { + def main(args: Array[String]): Unit = { + val c = new C + val f = c.foo + val f1 = serializeDeserialize(f) + } + + def serializeDeserialize[T <: AnyRef](obj: T): T = { + import java.io._ + val buffer = new ByteArrayOutputStream + val out = new ObjectOutputStream(buffer) + out.writeObject(obj) + val in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray)) + in.readObject.asInstanceOf[T] + } +} diff --git a/test/files/run/t9390d.scala b/test/files/run/t9390d.scala new file mode 100644 index 0000000000..3c5de3abf7 --- /dev/null +++ b/test/files/run/t9390d.scala @@ -0,0 +1,12 @@ +class C { // C is not serializable + def foo: () => Any = { + { () => class UseOuterInConstructor { C.this.toString }; new UseOuterInConstructor : Any} + } +} +object Test { + def main(args: Array[String]): Unit = { + val c = new C + val f = c.foo + f() // Doesn't NPE, as we didn't elide the outer instance in the constructor call. + } +} |