Bug 100271

Summary: Cast to generic type within "for" iterator no longer works under 3.1RC2
Product: [Eclipse Project] JDT Reporter: David Pickens <pickens>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: RESOLVED INVALID QA Contact:
Severity: normal    
Priority: P3 CC: cdsmith
Version: 3.1   
Target Milestone: 3.1 RC3   
Hardware: PC   
OS: Windows XP   
Whiteboard:

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. ***