Bug 321085

Summary: Enhanced for loops need to implement type safety checks on array initializers
Product: [Eclipse Project] JDT Reporter: Luke Hutchison <luke.hutch>
Component: CoreAssignee: Srikanth Sankaran <srikanth_sankaran>
Status: VERIFIED FIXED QA Contact:
Severity: major    
Priority: P3 CC: amj87.iitr, daniel_megert, satyam.kandula, srikanth_sankaran
Version: 3.7   
Target Milestone: 3.7 M2   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
Patch under consideration none

Description Luke Hutchison CLA 2010-07-27 23:29:13 EDT
Build Identifier: 20100218-1602

Enhanced for loops do not implement type safety checks on array initializers, e.g.

		HashSet<String> x = new HashSet<String>();
		x.add("a");
		HashSet<Integer> y = new HashSet<Integer>();
		y.add(1);
		// The following gives no warning
		for (Set<String> refSet : new Set[] { x, y }) {
			for (String str : refSet)
				System.out.println(str);
		}


Gives


a
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String


Reproducible: Always

Steps to Reproduce:
As above
Comment 1 Luke Hutchison CLA 2010-07-27 23:31:29 EDT
PS of course it's impossible to create a generic Set<String>[] array, so that warning should be given if attempted, and a normal type safety warning for new Set[] as shown in the listing.
Comment 2 Dani Megert CLA 2010-07-28 01:44:06 EDT
Complete test case:

import java.util.HashSet;
import java.util.Set;

public class Snippet {
	void foo() {
	   HashSet<String> x = new HashSet<String>();
	    x.add("a");
	    HashSet<Integer> y = new HashSet<Integer>();
	    y.add(1);
	    Set<String> OK= new Set[] { x, y };
	    // The following gives no warning
	    for (Set<String> BUG : new Set[] { x, y }) {
	        for (String str : BUG)
	            System.out.println(str);
	    }
	}
}
Comment 3 Srikanth Sankaran CLA 2010-08-03 01:38:35 EDT
(In reply to comment #2)
> Complete test case:

Thanks Dani. There was a minor typo, the test should be:

import java.util.HashSet;
import java.util.Set;

public class Snippet {
    void foo() {
       HashSet<String> x = new HashSet<String>();
        x.add("a");
        HashSet<Integer> y = new HashSet<Integer>();
        y.add(1);
        Set<String> [] OK= new Set[] { x, y };  // <<------ OK is an array.
        // The following gives no warning
        for (Set<String> BUG : new Set[] { x, y }) {
            for (String str : BUG)
                System.out.println(str);
        }
    }
}
Comment 4 Dani Megert CLA 2010-08-03 01:45:33 EDT
>There was a minor typo, the test should be:
Strange - I thought I just copied and pasted the example. Mmh.
Comment 5 Srikanth Sankaran CLA 2010-08-03 02:23:08 EDT
Created attachment 175742 [details]
Patch under consideration
Comment 6 Srikanth Sankaran CLA 2010-08-03 04:25:11 EDT
All tests pass. Satyam, please review, TIA.
Comment 7 Satyam Kandula CLA 2010-08-09 00:01:26 EDT
(In reply to comment #6)
Srikanth, Patch look good.
Comment 8 Srikanth Sankaran CLA 2010-08-09 00:10:22 EDT
Released in HEAD for 3.7 M2.
Comment 9 Ayushman Jain CLA 2010-09-14 07:30:35 EDT
Verified for 3.7M2 using build I20100909-1700.