Skip to content

Different behaviour between ECJ and javac for generic type inference #3351

Description

@Madjosz

Issue seems similar to #366 but this time it's the other way around: ecj compiles while javac fails.

import java.util.List;
public class PassThroughGenerics {
    private class MyComp implements Comparable<MyComp> {
        @Override public int compareTo(MyComp other) { return 0; }
    }
    
    static <E extends Comparable<E>> List<E> sort(List<E> list) {
        return list;
    }
    
    static <T> List<T> genericList() {
        return null;
    }
    
    public static void main(String[] args) {
        List<MyComp> sorted = sort(genericList());
        System.out.println(sorted);
    }
}

So sort should pass its generics through to genericList and ecj is capable of inferring T to MyComp while javac fails compilation with

PassThroughGenerics.java:16: error: method sort in class PassThroughGenerics cannot be applied to given types;
        List<MyComp> sorted = sort(genericList());
                              ^
  required: List<E#1>
  found:    List<Object>
  reason: inferred type does not conform to equality constraint(s)
    inferred: T
    equality constraints(s): E#2
  where E#1,T,E#2 are type-variables:
    E#1 extends Comparable<E#1> declared in method <E#1>sort(List<E#1>)
    T extends Comparable<E#2>
    E#2 extends Comparable<E#2>

Versions
javac
openjdk version "24-ea" 2025-03-18
OpenJDK Runtime Environment (build 24-ea+17-1938)
OpenJDK 64-Bit Server VM (build 24-ea+17-1938, mixed mode, sharing)
Tested in OpenJDK 7 - JDK 24, IBM Semeru 11, 17, 21; fails in every version

ecj
Eclipse Compiler for Java(TM) v20240215-1558, 3.37.0
Eclipse Compiler for Java(TM) v20240405-1402, 3.38.0
Eclipse Compiler for Java(TM) v20241112-0530, 3.40.0

Metadata

Metadata

Labels

javacecj not compatible with javac

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions