Skip to content

Unnecessary outer accessors are sometimes generated #4502

Closed
@smarter

Description

@smarter

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions