### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: compiler/org/eclipse/jdt/internal/compiler/ast/Expression.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/Expression.java,v retrieving revision 1.106 diff -u -r1.106 Expression.java --- compiler/org/eclipse/jdt/internal/compiler/ast/Expression.java 13 Oct 2006 19:20:45 -0000 1.106 +++ compiler/org/eclipse/jdt/internal/compiler/ast/Expression.java 30 Oct 2006 17:24:33 -0000 @@ -287,7 +287,7 @@ // ( TYPE_PARAMETER ) ARRAY TypeBinding match = expressionType.findSuperTypeWithSameErasure(castType); if (match == null) { - checkUnsafeCast(scope, castType, expressionType, match, true); + checkUnsafeCast(scope, castType, expressionType, null /*no match*/, true); } // recurse on the type variable upper bound return checkCastTypesCompatibility(scope, ((TypeVariableBinding)castType).upperBound(), expressionType, expression); @@ -341,7 +341,7 @@ // ( INTERFACE ) TYPE_PARAMETER match = expressionType.findSuperTypeWithSameErasure(castType); if (match == null) { - checkUnsafeCast(scope, castType, expressionType, match, true); + checkUnsafeCast(scope, castType, expressionType, null /*no match*/, true); } // recurse on the type variable upper bound return checkCastTypesCompatibility(scope, ((TypeVariableBinding)castType).upperBound(), expressionType, expression); @@ -360,6 +360,7 @@ return checkUnsafeCast(scope, castType, interfaceType, match, true); } if (use15specifics) { + checkUnsafeCast(scope, castType, expressionType, null /*no match*/, true); // ensure there is no collision between both interfaces: i.e. I1 extends List, I2 extends List if (interfaceType.hasIncompatibleSuperType((ReferenceBinding)castType)) return false; @@ -445,6 +446,7 @@ return checkUnsafeCast(scope, castType, expressionType, match, true); } if (use15specifics) { + checkUnsafeCast(scope, castType, expressionType, null /*no match*/, true); // ensure there is no collision between both interfaces: i.e. I1 extends List, I2 extends List if (refExprType.hasIncompatibleSuperType((ReferenceBinding) castType)) return false; Index: compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java,v retrieving revision 1.105 diff -u -r1.105 CastExpression.java --- compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java 28 Oct 2006 04:11:27 -0000 1.105 +++ compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java 30 Oct 2006 17:24:33 -0000 @@ -272,7 +272,7 @@ } public boolean checkUnsafeCast(Scope scope, TypeBinding castType, TypeBinding expressionType, TypeBinding match, boolean isNarrowing) { - if (match == castType) { + if (match == castType) { if (!isNarrowing && match == this.resolvedType.leafComponentType()) { // do not tag as unnecessary when recursing through upper bounds tagAsUnnecessaryCast(scope, castType); } @@ -301,6 +301,10 @@ this.bits |= UnsafeCast; return true; } + if (match == null && castType.isBoundParameterizedType()) { // cast between unrelated types + this.bits |= UnsafeCast; + return true; + } if (leafType.isTypeVariable()) { this.bits |= UnsafeCast; return true; #P org.eclipse.jdt.core.tests.compiler Index: src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java,v retrieving revision 1.549 diff -u -r1.549 GenericTypeTest.java --- src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java 30 Oct 2006 14:31:46 -0000 1.549 +++ src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java 30 Oct 2006 17:24:49 -0000 @@ -7357,6 +7357,11 @@ " Set channel = channels.get(0);\n" + " ^^^^^^^^\n" + "channels cannot be resolved\n" + + "----------\n" + + "2. WARNING in X.java (at line 7)\n" + + " element = (Set) iter.next();\n" + + " ^^^^^^^^^^^^^^^^^^^^\n" + + "Type safety: The cast from X to Set is actually checking against the erased type Set\n" + "----------\n"); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=70243 unsafe cast when wildcards @@ -26562,9 +26567,14 @@ "1. WARNING in X.java (at line 5)\n" + " return (Bar)f;\n" + " ^^^^^^^^^^^^^^\n" + + "Type safety: The cast from Foo to Bar is actually checking against the erased type Bar\n" + + "----------\n" + + "2. WARNING in X.java (at line 5)\n" + + " return (Bar)f;\n" + + " ^^^^^^^^^^^^^^\n" + "Unnecessary cast from Foo to Bar\n" + "----------\n" + - "2. ERROR in X.java (at line 7)\n" + + "3. ERROR in X.java (at line 7)\n" + " Zork z;\n" + " ^^^^\n" + "Zork cannot be resolved to a type\n" + @@ -34170,4 +34180,96 @@ }, ""); } +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=148041 +public void test1062() { + this.runNegativeTest( + new String[] { + "X.java", // ================= + "import java.util.HashSet;\n" + + "import java.util.Iterator;\n" + + "import java.util.Set;\n" + + "\n" + + "public class X {\n" + + " public static void main(String[] args) {\n" + + " Set set = new HashSet();\n" + + " for (Iterator iterator = set.iterator(); iterator.hasNext();) {\n" + + " Set element1 = iterator.next();\n" + + " Set element2 = (Set) iterator.next(); // warning\n" + + " }\n" + + " }\n" + + "}", // ================= + }, + "----------\n" + + "1. ERROR in X.java (at line 9)\n" + + " Set element1 = iterator.next();\n" + + " ^^^^^^^^^^^^^^^\n" + + "Type mismatch: cannot convert from X to Set\n" + + "----------\n" + + "2. WARNING in X.java (at line 10)\n" + + " Set element2 = (Set) iterator.next(); // warning\n" + + " ^^^^^^^^^^^^^^^^^^^^^^^^\n" + + "Type safety: The cast from X to Set is actually checking against the erased type Set\n" + + "----------\n"); +} +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=148041 - variation +public void test1063() { + this.runNegativeTest( + new String[] { + "X.java", // ================= + "import java.util.HashSet;\n" + + "import java.util.Iterator;\n" + + "import java.util.Set;\n" + + "\n" + + "public class X {\n" + + " public static void main(String[] args) {\n" + + " Set set = new HashSet();\n" + + " for (Iterator iterator = set.iterator(); iterator.hasNext();) {\n" + + " Set element1 = iterator.next();\n" + + " Set element2 = (Set) iterator.next(); // warning\n" + + " }\n" + + " }\n" + + "}", // ================= + }, + "----------\n" + + "1. ERROR in X.java (at line 9)\n" + + " Set element1 = iterator.next();\n" + + " ^^^^^^^^^^^^^^^\n" + + "Type mismatch: cannot convert from Cloneable to Set\n" + + "----------\n" + + "2. WARNING in X.java (at line 10)\n" + + " Set element2 = (Set) iterator.next(); // warning\n" + + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + + "Type safety: The cast from Cloneable to Set is actually checking against the erased type Set\n" + + "----------\n"); +} +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=148041 - variation +public void test1064() { + this.runNegativeTest( + new String[] { + "X.java", // ================= + "import java.util.HashSet;\n" + + "import java.util.Iterator;\n" + + "\n" + + "public class X {\n" + + " public static void main(String[] args) {\n" + + " HashSet set = new HashSet();\n" + + " for (Iterator iterator = set.iterator(); iterator.hasNext();) {\n" + + " HashSet element1 = iterator.next();\n" + + " HashSet element2 = (HashSet) iterator.next();\n" + + " }\n" + + " }\n" + + "}", // ================= + }, + "----------\n" + + "1. ERROR in X.java (at line 8)\n" + + " HashSet element1 = iterator.next();\n" + + " ^^^^^^^^^^^^^^^\n" + + "Type mismatch: cannot convert from X to HashSet\n" + + "----------\n" + + "2. ERROR in X.java (at line 9)\n" + + " HashSet element2 = (HashSet) iterator.next();\n" + + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + + "Cannot cast from X to HashSet\n" + + "----------\n"); +} } \ No newline at end of file