Skip to content

Private class constructor is public at the bytecode level #16651

Closed as not planned
@julienrf

Description

@julienrf

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

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