Type inference bug

Zheka Kozlov orionllmain at gmail.com
Fri Dec 14 05:00:26 UTC 2018


javac fails to compile the following simple code:

import java.util.List;
import java.util.Set;
import java.util.function.IntFunction;

public class App {
    static <A> List<A> tabulate(int size, IntFunction<A> f) {
        throw new UnsupportedOperationException();
    }

    static <A extends Comparable<A>> Set<A> fromIterable(Iterable<A>
iterable) {
        throw new UnsupportedOperationException();
    }

    public static void main(String[] args) {
        Set<String> unique = fromIterable(tabulate(100_000, i -> "a"));
    }
}

Obviously, this is a correctly typed program, but javac fails with an error.

JDK 8:
App.java:15: error: method fromIterable in class App cannot be applied to
given types;
        Set<String> unique = fromIterable(tabulate(100_000, i -> "a"));
                             ^
  required: Iterable<A>
  found: List<Object>
  reason: inferred type does not conform to upper bound(s)
    inferred: Object
    upper bound(s): Comparable<Object>,Object
  where A is a type-variable:
    A extends Comparable<A> declared in method <A>fromIterable(Iterable<A>)
1 error

JDK 11:
App.java:15: error: method fromIterable in class App cannot be applied to
given types;
        Set<String> unique = fromIterable(tabulate(100_000, i -> "a"));
                             ^
  required: Iterable<A#1>
  found: List<Object>
  reason: inferred type does not conform to equality constraint(s)
    inferred: A#2
    equality constraints(s): A#3
  where A#1,A#2,A#3 are type-variables:
    A#1 extends Comparable<A#1> declared in method
<A#1>fromIterable(Iterable<A#1>)
    A#2 extends Comparable<A#3>
    A#3 extends Comparable<A#3>
1 error

IDEA and Eclipse do not report any compilation errors.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20181214/97c2ca81/attachment.html>


More information about the compiler-dev mailing list