Skip to content

Private class constructor is public at the bytecode level #16651

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
julienrf opened this issue Jan 10, 2023 · 1 comment
Closed

Private class constructor is public at the bytecode level #16651

julienrf opened this issue Jan 10, 2023 · 1 comment

Comments

@julienrf
Copy link
Contributor

julienrf commented Jan 10, 2023

Classes with private constructors are actually public at the bytecode level, this creates two issues:

  • it can be called from Java code
  • MiMa reports error if its signature changes

Compiler version

3.2.1

Minimized code

// Foo.scala
package bug

class Foo private (x: Int)

object Foo:
  def apply(x: Int) = new Foo(x)
// Bar.java
package bug

class Bar {
  public void bar() {
    Foo foo = new Foo(42);
  }
}

Output

The code compiles.

Expectation

The Java compilation should fail.

Possible solution

In a discussion, @smarter suggested to emit the constructor as ACC_SYNTHETIC to make it non-accessible from Java, and ignored by mima (see lightbend-labs/mima#92). Note that that change would still be binary compatible with possible existing Java code that would call such constructors (but it would be source incompatible).

Related Scala 2 issue: scala/bug#12711

@julienrf
Copy link
Contributor Author

After some discussion in scala/bug#12711, we’ve concluded that MiMa should handle that, not the compiler. Follow-up issue is lightbend-labs/mima#738.

@julienrf julienrf closed this as not planned Won't fix, can't repro, duplicate, stale Jan 13, 2023
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

1 participant