scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Polymorphic extension method corrupts java class containing it: "bad class file"

Open Sporarum opened this issue 2 years ago • 1 comments

Compiler version

scalaVersion: 3.2.2-RC2

Minimized code

Scala file:

class A:
  extension (k: Int)
    def getOrElse[V](default: V): V = ???

Java file:

public class B_2 {

  public static void test() {
    A a = new A();
  }
}

Output

[error]   - java compilation failed with:
[error]       tests/run/java-extension/B_2.java:4: error: cannot access A
[error]           A a = new A();
[error]           ^
[error]         bad class file: out/runAll/run/java-extension/A.class
[error]           undeclared type variable: V
[error]           Please remove or make sure it appears in the correct subdirectory of the classpath.

Expectation

Should compile. Especially given we do not even access the problematic member, the whole class file is unusable.

Related problems

It compiles (and should) if:

  • You change the result type to for example Int, even though default still has type V

It does not compile (but should) if:

  • You remove the term clause containing default
  • You make getOrElse an interleaved method

Sporarum avatar Jan 19 '23 12:01 Sporarum

still reproduces in 3.4.2-RC1-bin-20240219

@Kordyjan do you want to remain assigned to this ticket?

SethTisue avatar Feb 19 '24 17:02 SethTisue