Skip to content

Unnecessary outer accessors are sometimes generated #4502

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

Closed
smarter opened this issue May 10, 2018 · 2 comments
Closed

Unnecessary outer accessors are sometimes generated #4502

smarter opened this issue May 10, 2018 · 2 comments

Comments

@smarter
Copy link
Member

smarter commented May 10, 2018

Consider:

class Foo
class Bar { self =>
  new Foo {
    println(self)
  }
}

The anonymous class generated by Dotty looks like:

private static class Bar$.anon
extends Foo {
    private final Bar $outer;

    public Bar$.anon(Bar $outer) {
        if ($outer == null) {
            throw new NullPointerException();
        }
        this.$outer = $outer;
        Predef$.MODULE$.println((Object)this.Bar$_$$anon$$$outer());
    }

    private Bar $outer() {
        return this.$outer;
    }

    public final Bar Bar$_$$anon$$$outer() {
        return this.$outer();
    }
}

Whereas with scalac we get:

public final class Bar$.anon
extends Foo {
    public Bar$.anon(Bar $outer) {
        Predef$.MODULE$.println((Object)$outer);
    }
}

This seems to be implemented by removing outer accessors that are never called in the constructors phase: https://github.com/scala/scala/blob/a52482c7236a782adedf7523301a97659e4d720f/src/compiler/scala/tools/nsc/transform/Constructors.scala#L114-L117

smarter added a commit to dotty-staging/dotty that referenced this issue May 10, 2018
- They make debugging harder since it's not always clear what
  `Symbols$$anon$3` refes to
- If they take an outer reference as parameter, we currently generate
  inefficient code for them due to scala#4502
@DieBauer
Copy link
Contributor

I tried reproducing the above, and can't seem to get the same output with current master. Given that the issue is almost 2 years old, I was wondering is this still an issue? If yes, which reproducer would trigger this?

@smarter
Copy link
Member Author

smarter commented Apr 27, 2020

Thanks for checking, this was probably fixed by #8253

@smarter smarter closed this as completed Apr 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants