Community
Participate
Working Groups
HEAD. On the following code, we issue an error: "Cannot cast from S<Integer> to T". while this matches javac5,6 behavior, javac7 (b123) compiles this code fine. Needs to be investigated.
Test case: interface Super<P> {} class Y<C> implements Super<Integer>{} interface II extends Super<Double>{} class S<A> extends Y<Byte> {} interface T<B> extends II{} @SuppressWarnings("unchecked") public class X { public static void main(String argv[]) { S<Integer> s = null; T<Integer> t = null; t = (T) s; //casting to raw type, no error System.out.println(t); } }
Targetting M7. This might be released only in the Java 7 branch and a research has to be done to see why this behavior has changed since 1.6. I might have a patch, but I first need to see why it makes sense not to fail at compile time. The new behavior means that some code can fail at runtime with a ClassCastException.
Created attachment 190755 [details] Proposed fix I am still investigating why this is allowed.
In fact as long as the class S is not final, this is fine at compile time even if there is no obvious relationship between S and T. A subclass of S might implements T. So this patch just makes the "generic" case consistent with non generic case. Srikanth, I let you have a look at it. And we should release it soon.
Created attachment 192207 [details] Proposed fix Just make sure the last version is used. Regression test will be required once the patch is fine.
(In reply to comment #5) > Created attachment 192207 [details] > Proposed fix > > Just make sure the last version is used. Regression test will be required once > the patch is fine. See sun bugs : 6542952 and 6559182. Agree with the analysis. Olivier, shouldn't be the fix be restricted to the cast type interfaces ? With that in mind, the second set of diffs look incorrect to me. This change reconsidered, this looks good for HEAD and JAVA7 branch.
Regression test: // https://bugs.eclipse.org/bugs/show_bug.cgi?id=334493 public void test334493() { this.runNegativeTest( new String[] { "X.java", "interface Super<P> {}\n" + "class Y<C> implements Super<Integer>{}\n" + "interface II extends Super<Double>{}\n" + "class S<A> extends Y<Byte> {}\n" + "interface T<B> extends II{}\n" + "public class X {\n" + " public static void main(String argv[]) {\n" + " S<Integer> s = null;\n" + " T<Integer> t = null;\n" + " t = (T) s; //casting to raw type, no error\n" + " System.out.println(t);\n" + " }\n" + "}\n" }, this.complianceLevel < ClassFileConstants.JDK1_7 ? "----------\n" + "1. ERROR in X.java (at line 10)\n" + " t = (T) s; //casting to raw type, no error\n" + " ^^^^^\n" + "Cannot cast from S<Integer> to T\n" + "----------\n" + "2. WARNING in X.java (at line 10)\n" + " t = (T) s; //casting to raw type, no error\n" + " ^^^^^\n" + "Type safety: The expression of type T needs unchecked conversion to conform to T<Integer>\n" + "----------\n" : "----------\n" + "1. WARNING in X.java (at line 10)\n" + " t = (T) s; //casting to raw type, no error\n" + " ^^^^^\n" + "Type safety: The expression of type T needs unchecked conversion to conform to T<Integer>\n" + "----------\n" + "2. WARNING in X.java (at line 10)\n" + " t = (T) s; //casting to raw type, no error\n" + " ^\n" + "T is a raw type. References to generic type T<B> should be parameterized\n" + "----------\n"); }
(In reply to comment #6) > Olivier, shouldn't be the fix be restricted to the cast type > interfaces ? With that in mind, the second set of diffs look > incorrect to me. Sorry, that was muddled thinking, patch looks good. Patch and test released in HEAD for 3.7 M7
Verified for 3.7M7 using build I20110425-1800.
*** Bug 346051 has been marked as a duplicate of this bug. ***