-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serializing val that refers to another val captures enclosing class #8033
Comments
scalac generates an anonymous class that decompiles to this: public final class Foo$$anon$2
implements Okay {
private final Okay okay;
@Override
public Okay okay() {
return this.okay;
}
public Foo$$anon$2(Foo $outer) {
this.okay = $outer.okay1();
}
} Whereas we generate this: private static final class Foo$$anon$2
implements Okay {
private final Okay okay;
private final Foo $outer;
public Foo$$anon$2(Foo $outer) {
if ($outer == null) {
throw new NullPointerException();
}
this.$outer = $outer;
this.okay = this.Foo$_$$anon$$$outer().okay1();
}
@Override
public Okay okay() {
return this.okay;
}
private Foo $outer() {
return this.$outer;
}
public final Foo Foo$_$$anon$$$outer() {
return this.$outer();
}
} I guess https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala needs to learn to not generate outer accessors when the outer class is only referenced in the constructor ? |
@smarter It's not hard for us to work around this, but it wasn't especially easy to characterise at first, and seems likely to cause confusion for other people as well. |
Actually, scalac also generates that outer accessor and field in ExplicitOuter, but optimizes it away in its Constructors phase. Our Constructors phase should be doing similar optimizations but apparently they're not working as well. |
Fix #8033: Eliminate unnecessary outer accessors
minimized code
Compiles but fails at runtime
expectation
This is minimisation from a failing test in Cats, where this arrangement has never been a problem in Scala 2, and the code above runs fine on Scala 2:
Note that if you change
val okay
inokay2
to adef
, you see the same error on Scala 2 and Dotty.The text was updated successfully, but these errors were encountered: