Skip to content

private objects are inaccessible from classes in same package #1287

Closed
@smarter

Description

@smarter

Foo.scala:

private object Foo {
  val x: Int = 1
}                

Test.scala:

object Test {
  val a = Foo.x
}
% ./bin/dotc Foo.scala Test.scala
Exception in thread "main" java.lang.AssertionError: assertion failed:
private object Foo in Foo.scala accessed from
constructor Test$ in object Test$ in Test.scala
        at scala.Predef$.assert(Predef.scala:165)
        at dotty.tools.dotc.transform.ExpandPrivate.ensurePrivateAccessible(ExpandPrivate.scala:75)

Note: here's how scalac compiles Foo.scala:

public final class Foo {
    public static int x() {
        return Foo$.MODULE$.x();
    }
}

public final class Foo$ {
    public static final Foo$ MODULE$;
    private final int x;

    public static {
        new Foo$();
    }

    public int x() {
        return this.x;
    }

    private Foo$() {
        MODULE$ = this;
        this.x = 1;
    }
}

And here's how dotty does it:

public final class Foo {
    public static int x() {
        return Foo$.MODULE$.x();
    }
}

private final class Foo$ {
    public static final Foo$ MODULE$;
    private final int x$$local;

    public static {
        new Foo$();
    }

    public Foo$() {
        MODULE$ = this;
        this.x$$local = 1;
    }

    public int x() {
        return this.x$$local;
    }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions