RFR: 7039014: Confusing error message for method conflict

Alex Buckley alex.buckley at oracle.com
Tue Oct 18 23:07:26 UTC 2022


I like the new error message.

Although the problem lies in the methods of the type A<String>, the 
error is technically due to a rule about the inheritance of those 
methods by B. (JLS 9.4.1.3: "One of the inherited methods must be 
return-type substitutable for every other inherited method, or else a 
compile-time error occurs.")  Since whether a method is default or 
private has bearing on the rules of inheritance in interfaces, I 
recommend adding tests with these variations of A:

// Error when B extends A<String>
interface A<T> {
     default byte m(String x) { return 0; }
     char m(T x);
}

// No error when B extends A<String>
interface A<T> {
     private byte m(String x) { return 0; }
     char m(T x);
}

Alex

On 10/18/2022 3:01 PM, Archie L. Cobbs wrote:
> This fixes a confusing error message.
> 
> Currently, this input:
> 
> interface A<T> {
>      byte m(String x);
>      char m(T x);
> }
> 
> interface B extends A<String> {
> }
> 
> produces this error:
> 
> A.java:6: error: types A<String> and A<String> are incompatible;
> interface B extends A<String> {
> ^
>    both define m(String), but with unrelated return types
> 1 error
> 
> This patch detects when the two types are actually the same type and gives this error message instead:
> 
> A.java:6: error: type A<String> defines m(String) more than once with unrelated return types
> interface B extends A<String> {
> ^
> 1 error
> 
> -------------
> 
> Commit messages:
>   - 7039014: Confusing error message for method conflict
> 
> Changes: https://git.openjdk.org/jdk/pull/10752/files
>   Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=10752&range=00
>    Issue: https://bugs.openjdk.org/browse/JDK-7039014
>    Stats: 44 lines in 4 files changed: 41 ins; 0 del; 3 mod
>    Patch: https://git.openjdk.org/jdk/pull/10752.diff
>    Fetch: git fetch https://git.openjdk.org/jdk pull/10752/head:pull/10752
> 
> PR: https://git.openjdk.org/jdk/pull/10752


More information about the compiler-dev mailing list