bug icon indicating copy to clipboard operation
bug copied to clipboard

False-negative: class needs to be abstract when inherits java class and interface

Open XYZboom opened this issue 11 months ago • 10 comments

Reproduction steps

Scala version: 2.13.15

// FILE: I0.java
public interface I0<T> {
    public void func(A<String> a, T s);
}
// FILE: I1.java
public interface I1 extends I0<String> {
    public default void func(A<Object> a, String s) {

    }
}
class A[T0]
class A0 extends I1 {
}

Problem

The compiler passed the code above. There should be a class A0 needs to be abstract here.

XYZboom avatar Jan 05 '25 04:01 XYZboom

The Scala compiler only reads Java source files (in mixed compilation) to know their signatures, but it does not produce .class files for them. Java sources are not checked for correctness, many checks are skipped. We delegate checking to the Java compiler, which needs to processe Java sources anyway after mixed compilation.

lrytz avatar Jan 06 '25 13:01 lrytz

@lrytz Yes, I understand the working principle of scalac, which is similar to Kotlin's delegation of Javac to compile Java source code. In this issue, what I want to express is that class A0 should give an error because it does not implement abstract method func in the Java interface, but in fact the compiler passed this code.

XYZboom avatar Jan 06 '25 13:01 XYZboom

I (again) overlooked what is Scala code.

lrytz avatar Jan 06 '25 13:01 lrytz

I (again) overlooked what is Scala code.

Perhaps the title I gave to the issue was a bit misleading

XYZboom avatar Jan 06 '25 13:01 XYZboom

I tried this out with Lukas's https://github.com/scala/scala/pull/10580 but no difference. I wasn't in a position to disappear down a rabbit hole or warren.

som-snytt avatar Jan 06 '25 14:01 som-snytt

Does Scala 3 handle it correctly?

SethTisue avatar Jan 06 '25 17:01 SethTisue

-- Error: test/files/neg/t13074/b.scala:2:6 ----------------------------------------------------------------------------
2 |class B extends I1
  |      ^
  |      class B needs to be abstract, since def f(x$0: A[String], x$1: T): Unit in trait I0 is not defined
  |      (The class implements a member with a different type: def f(x$0: A[Object], x$1: String): Unit in trait I1)
1 error found

The bug template needs a new section for What does Dotty do?

som-snytt avatar Jan 06 '25 17:01 som-snytt

Perhaps I have made some new discoveries, and this bug may be a Javac bug. Change I1 to the following code, Javac will report a "name clash".

interface I1 extends I0<Object> {
    public default void func(A<Object> a, Object s) {
    }
}

If change Object in I0<Object> to another type, such as I0<String>, Javac will not report a "name clash".

XYZboom avatar Jan 09 '25 03:01 XYZboom

Java developers have reproduced this bug in JDK-8347330. But before they fix this bug, Scala2 should also report an error like what Scala3 does.

XYZboom avatar Jan 09 '25 13:01 XYZboom

Java developers currently do not believe that this is a bug in Javac

XYZboom avatar Mar 08 '25 01:03 XYZboom