Bug 86488

Summary: Improper warning for varargs argument of parameterized type
Product: [Eclipse Project] JDT Reporter: John Bollinger <jbollinger>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: RESOLVED WORKSFORME QA Contact:
Severity: normal    
Priority: P3 CC: markus.kell.r
Version: 3.1   
Target Milestone: 3.2 M5   
Hardware: PC   
OS: Windows 2000   
Whiteboard:

Description John Bollinger CLA 2005-02-24 11:02:54 EST
(Observed against 3.1 M4:)

The compiler reports this warning:
---
Varargs argument Collection<? extends E>[] should be cast to Collection[] when
passed to the constructor UnionIterator<E>(Collection...)
---

where the constructor in question of UnionIterator<E> is declared as 

public UnionIterator(Collection<? extends E>...) { [...] }

and I invoke it as

Collection<? extends T>[] collections = [...];
UnionIterator<T> iterator = new UnionIterator<T>(collections);

There are several problems with this:

(1) The compiler is losing the type information for the varargs argument. 
(Although it seems to remember the type information until the end of the file in
which it appears, because the warning only showed up when I moved the
UnionIterator class to its own file.)

(2) Collection is not a supertype of Collection<? extends E>, thus the warning
message is demanding an unsafe cast.  The compiler should instead be issuing a
type safety warning if it cannot verify the type-correctness of the constructor
invocation (though better would be for it to recognize that the invocation is
indeed type-correct).

(3) If for some reason the compiler *cannot* retain the type information in such
a case, then it should flag the constructor declaration with a warning.
Comment 1 Markus Keller CLA 2005-07-13 11:45:33 EDT
Cannot reproduce in 3.1.

This code is compiled without any warning:

public class UnionIterator<E> {
    public UnionIterator(Collection<? extends E>... arg) {  }
    
    void test(Collection<? extends E>[] collections) {
        UnionIterator<E> iterator = new UnionIterator<E>(collections);
    }
}

class UnionIterTest {
    static <T> void test(Collection<? extends T>[] collections) {
        UnionIterator<T> iterator = new UnionIterator<T>(collections);
    }
}
Comment 2 John Bollinger CLA 2005-07-13 12:59:05 EDT
I agree that the warning observed under 3.1 M4 is not observed under 3.1.0. 
Evidently this issue has been resolved.  Thanks for looking into it.
Comment 3 Philipe Mulet CLA 2006-02-05 15:41:27 EST
closing