Bug 321085 - Enhanced for loops need to implement type safety checks on array initializers
Summary: Enhanced for loops need to implement type safety checks on array initializers
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: All All
: P3 major (vote)
Target Milestone: 3.7 M2   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-07-27 23:29 EDT by Luke Hutchison CLA
Modified: 2010-09-14 07:30 EDT (History)
4 users (show)

See Also:


Attachments
Patch under consideration (4.19 KB, patch)
2010-08-03 02:23 EDT, Srikanth Sankaran CLA
no flags Details | Diff

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