View | Details | Raw Unified | Return to bug 321085
Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/ast/ForeachStatement.java (+2 lines)
Lines 388-393 Link Here
388
				if (!this.collectionElementType.isCompatibleWith(elementType)
388
				if (!this.collectionElementType.isCompatibleWith(elementType)
389
						&& !this.scope.isBoxingCompatibleWith(this.collectionElementType, elementType)) {
389
						&& !this.scope.isBoxingCompatibleWith(this.collectionElementType, elementType)) {
390
					this.scope.problemReporter().notCompatibleTypesErrorInForeach(this.collection, this.collectionElementType, elementType);
390
					this.scope.problemReporter().notCompatibleTypesErrorInForeach(this.collection, this.collectionElementType, elementType);
391
				} else if (this.collectionElementType.needsUncheckedConversion(elementType)) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=321085
392
				    this.scope.problemReporter().unsafeTypeConversion(this.collection, collectionType, upperScope.createArrayType(elementType, 1));
391
				}
393
				}
392
				// in case we need to do a conversion
394
				// in case we need to do a conversion
393
				int compileTimeTypeID = this.collectionElementType.id;
395
				int compileTimeTypeID = this.collectionElementType.id;
(-)src/org/eclipse/jdt/core/tests/compiler/regression/ForeachStatementTest.java (+54 lines)
Lines 2779-2784 Link Here
2779
		},
2779
		},
2780
		"12345");
2780
		"12345");
2781
}
2781
}
2782
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=321085
2783
public void test054() throws Exception {
2784
	this.runNegativeTest(
2785
			new String[] {
2786
				"X.java",
2787
				"import java.util.HashSet;\n" +
2788
				"import java.util.Set;\n" +
2789
				"public class X {\n" +
2790
				"    void foo() {\n" +
2791
				"       HashSet<String> x = new HashSet<String>();\n" +
2792
				"        x.add(\"a\");\n" +
2793
				"        HashSet<Integer> y = new HashSet<Integer>();\n" +
2794
				"        y.add(1);\n" +
2795
				"        Set<String> [] OK= new Set[] { x, y };\n" +
2796
				"        for (Set<String> BUG : new Set[] { x, y }) {\n" +
2797
				"            for (String str : BUG)\n" +
2798
				"                System.out.println(str);\n" +
2799
				"        }\n" +
2800
				"        Set [] set = new Set[] { x, y };\n" +
2801
				"        for (Set<String> BUG : set) {\n" +
2802
				"            for (String str : BUG)\n" +
2803
				"                System.out.println(str);\n" +
2804
				"        }\n" +
2805
				"    }\n" +
2806
				"    Zork z;\n" +
2807
				"}\n",
2808
			},
2809
			"----------\n" + 
2810
			"1. WARNING in X.java (at line 9)\n" + 
2811
			"	Set<String> [] OK= new Set[] { x, y };\n" + 
2812
			"	                   ^^^^^^^^^^^^^^^^^^\n" + 
2813
			"Type safety: The expression of type Set[] needs unchecked conversion to conform to Set<String>[]\n" + 
2814
			"----------\n" + 
2815
			"2. WARNING in X.java (at line 10)\n" + 
2816
			"	for (Set<String> BUG : new Set[] { x, y }) {\n" + 
2817
			"	                       ^^^^^^^^^^^^^^^^^^\n" + 
2818
			"Type safety: The expression of type Set[] needs unchecked conversion to conform to Set<String>[]\n" + 
2819
			"----------\n" + 
2820
			"3. WARNING in X.java (at line 14)\n" + 
2821
			"	Set [] set = new Set[] { x, y };\n" + 
2822
			"	^^^\n" + 
2823
			"Set is a raw type. References to generic type Set<E> should be parameterized\n" + 
2824
			"----------\n" + 
2825
			"4. WARNING in X.java (at line 15)\n" + 
2826
			"	for (Set<String> BUG : set) {\n" + 
2827
			"	                       ^^^\n" + 
2828
			"Type safety: The expression of type Set[] needs unchecked conversion to conform to Set<String>[]\n" + 
2829
			"----------\n" + 
2830
			"5. ERROR in X.java (at line 20)\n" + 
2831
			"	Zork z;\n" + 
2832
			"	^^^^\n" + 
2833
			"Zork cannot be resolved to a type\n" + 
2834
			"----------\n");
2835
}
2782
public static Class testClass() {
2836
public static Class testClass() {
2783
	return ForeachStatementTest.class;
2837
	return ForeachStatementTest.class;
2784
}
2838
}

Return to bug 321085