Skip to content

private objects are inaccessible from classes in same package #1287

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 30, 2016 · 2 comments
Closed

private objects are inaccessible from classes in same package #1287

smarter opened this issue May 30, 2016 · 2 comments

Comments

@smarter
Copy link
Member

smarter commented May 30, 2016

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;
    }
}
@olafurpg
Copy link
Contributor

I'm able to reproduce this error on the latest Dotty master (318700f).

I bumped into this error while trying to compile https://github.com/johnynek/paiges with Dotty. The bug manifests in an uncaught exception during compilation


exception occurred while compiling tests/pos/private-object/Chunk.scala, tests/pos/private-object/Doc.scala
Exception in thread "main" java.lang.AssertionError: assertion failed: private object Chunk in package bar in tests/pos/private-object/Chunk.scala accessed from constructor Doc$ in object Doc in tests/pos/private-object/Doc.scala
	at scala.Predef$.assert(Predef.scala:165)
	at dotty.tools.dotc.transform.ExpandPrivate.ensurePrivateAccessible(ExpandPrivate.scala:90)
	at dotty.tools.dotc.transform.ExpandPrivate.transformIdent(ExpandPrivate.scala:96)
	at dotty.tools.dotc.transform.ExpandPrivate.transformIdent(ExpandPrivate.scala:39)
	at dotty.tools.dotc.transform.TreeTransforms$TreeTransformer.goIdent(TreeTransform.scala:562)
	at dotty.tools.dotc.transform.TreeTransforms$TreeTransformer.transformNamed(TreeTransform.scala:944)
	at dotty.tools.dotc.transform.TreeTransforms$TreeTransformer$$anonfun$transform$2.apply(TreeTransform.scala:1182)
	at dotty.tools.dotc.transform.TreeTransforms$TreeTransformer$$anonfun$transform$2.apply(TreeTransform.scala:1175)
	at dotty.tools.dotc.reporting.Reporting$class.traceIndented(Reporter.scala:136)
	at dotty.tools.dotc.core.Contexts$Context.traceIndented(Contexts.scala:57)

Removing the private modifier "fixed" the problem.

@smarter
Copy link
Member Author

smarter commented Jan 11, 2018

@olafurpg Could you check if the crash still happens in paiges? It doesn't happen in dotty master for the testcase I gave in my original comment.

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