Bug 359284 - Unnecessary checkast from null
Summary: Unnecessary checkast from null
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.8 M4   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-09-28 13:44 EDT by Eugene Kuleshov CLA
Modified: 2011-12-05 01:58 EST (History)
4 users (show)

See Also:
srikanth_sankaran: review+


Attachments
Patch to roll back fix & tests (8.58 KB, patch)
2011-10-25 01:17 EDT, Srikanth Sankaran CLA
no flags Details | Diff
proposed fix (2.23 KB, patch)
2011-10-25 02:03 EDT, Ayushman Jain CLA
no flags Details | Diff
proposed fix + regression tests (5.21 KB, patch)
2011-11-10 04:25 EST, Ayushman Jain CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Eugene Kuleshov CLA 2011-09-28 13:44:18 EDT
Consider the following Java snippet:

public class Test {
    public static void use(Object[] x) {
    }

    public static void main(String[] args) {
        Object[] x = null;

        use(x);
    }
}
The Java bytecode produced for main() by the Eclipse 3.7 compiler looks like this:

public static void main(java.lang.String[]);
  Code:
   0:   aconst_null
   1:   checkcast   #20; //class "[Ljava/lang/Object;"
   4:   astore_1
   5:   aload_1
   6:   invokestatic    #21; //Method use:([Ljava/lang/Object;)V
   9:   return

On the contrary, this is the bytecode produced by the OpenJDK 1.6.0b22 compiler:

public static void main(java.lang.String[]);
  Code:
   0:   aconst_null
   1:   astore_1
   2:   aload_1
   3:   invokestatic    #2; //Method use:([Ljava/lang/Object;)V
   6:   return

Note that the Eclipse compiler issues an extra checkcast opcode. It also seems to do that only for arrays and not for any other variable type.
Comment 1 Olivier Thomann CLA 2011-09-28 14:11:10 EDT
This is a consequence of bug 26903. It looks like this is no longer an issue.
Srikanth, please follow up. All references need to be updated.
Comment 2 Srikanth Sankaran CLA 2011-10-12 05:00:16 EDT
See also bug 51300
Comment 3 Srikanth Sankaran CLA 2011-10-12 06:16:10 EDT
(In reply to comment #1)
> This is a consequence of bug 26903. It looks like this is no longer an issue.
> Srikanth, please follow up. All references need to be updated.

Looks like bug 26903 + bug 51300 modified only:

ArrayReference, SingleNameReference, LocalDeclaration.

Testing after backing out the change in progress.
Comment 4 Srikanth Sankaran CLA 2011-10-13 04:35:32 EDT
(In reply to comment #3)
> (In reply to comment #1)
> > This is a consequence of bug 26903. It looks like this is no longer an issue.
> > Srikanth, please follow up. All references need to be updated.
> 
> Looks like bug 26903 + bug 51300 modified only:
> 
> ArrayReference, SingleNameReference, LocalDeclaration.
> 
> Testing after backing out the change in progress.

All tests pass. I also verified that the code generated for the test case
attached to this bug and bug 26903 and bug 51300 verify alright and run
alright with JDK5,6,7.

Released for 3.8 M3 via commit id 237f280b16d1b493931020623a0e18d446c4fadf
Comment 5 Ayushman Jain CLA 2011-10-24 15:47:39 EDT
Srikanth, after this fix I now get this error with 1.4 VM -
java.lang.VerifyError: (class: A, method: main signature: ([Ljava/lang/String;)V) Incompatible types for storing into array of arrays or objects

class A {
    public static void main(String[] args) {
        try {
            int[][] tab = (int[][]) null;
            tab[0] = new int[1];
        } catch (NullPointerException e) {
            System.out.print("OK");
        }
    }
}


I tried with 1.4 because bug 26903 mentioned that javac 1.4 has a checkcast to make such a program run. Now we just omit that checkcast. OTOH, even javac7 still generates a checkcast for the above class. 

Perhaps there's something here?
Comment 6 Srikanth Sankaran CLA 2011-10-24 19:29:18 EDT
(In reply to comment #5)
> Srikanth, after this fix I now get this error with 1.4 VM -
> java.lang.VerifyError: (class: A, method: main signature:
> ([Ljava/lang/String;)V) Incompatible types for storing into array of arrays or
> objects

That's bad, thanks for catching it, I'll roll back the fix for M3
and take a look at it in leisure.
Comment 7 Srikanth Sankaran CLA 2011-10-25 01:17:05 EDT
Created attachment 205874 [details]
Patch to roll back fix & tests

Ayush, please release and tag. I am unable to push to upstream. (Auth failures and timeouts)
Comment 8 Ayushman Jain CLA 2011-10-25 02:03:20 EDT
Created attachment 205875 [details]
proposed fix

(to be applied on HEAD after the earlier changes have been reverted).

Basically, we dont need a checkcast for 
int[][] aray = null;

but do need it for
int[][] aray = (int[][])null;

to satisfy 1.4 VM
Comment 9 Ayushman Jain CLA 2011-10-25 03:44:59 EDT
(In reply to comment #7)
> Created attachment 205874 [details] [diff]
> Patch to roll back fix & tests
> 
> Ayush, please release and tag. I am unable to push to upstream. (Auth failures
> and timeouts)

Released via commit 4fdb8fd78fd76e33e8dba33f80633397bdb28e98
Comment 10 Srikanth Sankaran CLA 2011-10-27 01:49:52 EDT
I verified with build id: I20111025-1800, that the problem reported in
comment#6 does not show up anymore i.e the roll back is effective and
there are no issues.
Comment 11 Ayushman Jain CLA 2011-11-04 03:28:00 EDT
Srikanth, patch in comment 8 needs to be reviewed. Thanks.
Comment 12 Srikanth Sankaran CLA 2011-11-09 02:25:38 EST
(In reply to comment #11)
> Srikanth, patch in comment 8 needs to be reviewed. Thanks.

Patch looks good, Thanks Ayush. Could you please restore the test
from the previous patch that was rolled back. The test is testing
comment# 0 case and is still a valid test for the limited fix made
by the current patch.
Comment 13 Ayushman Jain CLA 2011-11-10 04:25:58 EST
Created attachment 206766 [details]
proposed fix + regression tests

restored the test from rolled back patch and added another test for the 
Object[] o = (Object[])null case, to verify generated bytecode contains checkcast
Comment 14 Ayushman Jain CLA 2011-11-10 04:27:12 EST
Released in HEAD via commit 9eac203f2453ab21a49dee5eceed3025656c3aee
Comment 15 Jay Arthanareeswaran CLA 2011-12-05 01:58:30 EST
Verified for 3.8M4 with build I20111202-0800.