Community
Participate
Working Groups
3.2RC4 Though the VM tolerates it, it feels like a receiver generic cast is mandated for the #compareTo(...) invocation (checkcast to Comparable). public class X { public static void main(String[] args) { int foo = 0; String bar = "zero"; System.out.println((foo != 0 ? foo : bar).compareTo(null)); } } Currently, the intersection (receiver) type is Object&Comparable<?>&Serializable it thinks the erasure (Comparable) is good enough. Though since there is no real artifact it relates to, this might be a case where cast is still mandated. Need to check
Indeed we produce: // Method descriptor #15 ([Ljava/lang/String;)V // Stack: 3, Locals: 3 public static void main(String[] args); 0 iconst_0 1 istore_1 [foo] 2 ldc <String "zero"> [16] 4 astore_2 [bar] 5 getstatic System.out : PrintStream [18] 8 iload_1 [foo] 9 ifeq 19 12 iload_1 [foo] 13 invokestatic Integer.valueOf(int) : Integer [24] 16 goto 20 19 aload_2 [bar] 20 aconst_null 21 invokeinterface Comparable.compareTo(Object) : int [30] [nargs: 2] 26 invokevirtual PrintStream.println(int) : void [36] 29 return where javac 1.6 does: // Method descriptor #15 ([Ljava/lang/String;)V // Stack: 3, Locals: 3 public static void main(String[] arg0); 0 iconst_0 1 istore_1 2 ldc <String "zero"> [2] 4 astore_2 5 getstatic System.out : PrintStream [3] 8 iload_1 9 ifeq 19 12 iload_1 13 invokestatic Integer.valueOf(int) : Integer [4] 16 goto 20 19 aload_2 20 checkcast Comparable [5] 23 aconst_null 24 invokeinterface Comparable.compareTo(Object) : int [6] [nargs: 2] 29 invokevirtual PrintStream.println(int) : void [7] 32 return
Fix for bug 159738 should address this one, but is not enough since intersectionType#erasure() doesn't answer 'Object', but rather 'Comparable<...>' !? Checking the logic.
Created attachment 52925 [details] Proposed patch Fix ensures that intersection type erasures return the first bound, and not the first bound distinct from Object (as it did in the past). This guarantees that fix for bug 159738 performs properly from now on.
Released for 3.3M3.
Added GenericTypeTest#test1058
Verified for 3.3 M3 using build I20061030-0010
Reopening. Fix is altering generated bytecodes, and degradating stackmap attributes (see StackMapTableAttributeTest#test008). Reverting the change, and added more tests: GenericTypeTest#test1065-1066, showing that needed casts are present anyway, and that only ClassCastException are issued on negative cases.
Closing as INVALID.