Bug 100271 - Cast to generic type within "for" iterator no longer works under 3.1RC2
Summary: Cast to generic type within "for" iterator no longer works under 3.1RC2
Status: RESOLVED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.1 RC3   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 100879 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-06-15 14:47 EDT by David Pickens CLA
Modified: 2005-06-20 13:16 EDT (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 David Pickens CLA 2005-06-15 14:47:54 EDT
I just moved up to 3.1RC2 from 3.1M6 and some of my 1.5 code no longer compiles.
Here is a simple test case to demonstrate the problem. It compiles without error
under 3.1M6, but fails with an obscure error under 3.1RC2:

--------------------------------

import java.util.ArrayList;
import java.util.List;

public class Test1 {  
    static <T> List<T> foo() { return new ArrayList<T>(); }
    public static void main(String[] args) {
        for (String s: (List<String>)foo()) {}
    }
}
Comment 1 Philipe Mulet CLA 2005-06-15 15:18:30 EDT
M6 was too permissive. We now flag an error, and javac agrees with us.

Our compiler was incorrectly using type expectation from cast to drive
inference, which is against the spec. So really, if the spec was changing we
would revert to old behavior.

Write instead:
import java.util.ArrayList;
import java.util.List;

public class Test1 {  
    static <T> List<T> foo() { return new ArrayList<T>(); }
    public static void main(String[] args) {
    	List<String> foo = foo();
        for (String s: foo) {}
    }
}
Comment 2 Philipe Mulet CLA 2005-06-20 13:16:55 EDT
*** Bug 100879 has been marked as a duplicate of this bug. ***