Bug 86488 - Improper warning for varargs argument of parameterized type
Summary: Improper warning for varargs argument of parameterized type
Status: RESOLVED WORKSFORME
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: 3.2 M5   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-02-24 11:02 EST by John Bollinger CLA
Modified: 2006-02-05 15:41 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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