Incoherent invocation type inference?

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Jan 16 12:51:26 UTC 2017



On 16/01/17 11:37, B. Blaser wrote:
> So, I suggest a general "lint" warning if javac infers T to an
> intersection type, because T is widened without being sure that it is
> possible; this would be cohernent with the "lint" warning for the
> unchecked cast "(T)" in "get()".
I don't think you want that - there are cases where javac infers an 
intersection type which are totally legitimate:

interface I { }
class S { }
class A extends S implements I { }
class B extends S implements I { }

class Test {
    <Z> Z choose(Z z1, Z z2, boolean cond) { return cond ? z1 : z2; }

    void test() {
      choose(new A(), new B(), false);
    }
}

I don't think intersection types are the evil in your example. Even 
leaving intersection types aside:

class Issue  {
     <T> T get() { return (T)new ArrayList<String>(); }

     void run() {
         LinkedList<String> ll = null;
         ll = get(); // OK, but will fail at runtime due to unchecked 
conversion in get().
     }

     public static void main(String... args) {
         new Issue().run();
     }
}

So, I guess I'm not exactly sure of what we're trying to improve here?

Maurizio


More information about the compiler-dev mailing list