Bug 60563

Summary: [1.5] VerifyError when using generics and arrays
Product: [Eclipse Project] JDT Reporter: Michael Forster <email>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: 3.0   
Target Milestone: 3.1 M1   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Michael Forster CLA 2004-04-30 09:11:46 EDT
The following code:

    interface A<T> {
        T[] m1(T x);                          
    }
    class B { 
        void m2(A<B> x) { 
            m3(x.m1(new B())); 
        }
        void m3(B[] x) {
        }                    
    }
 
compiles but gives this Exception

    java.lang.VerifyError: (class: B, method: m2 signature: (LA;)V)
        Incompatible argument to function

when executed. I am using Eclipse I20040428 with Cheetah04.
Comment 1 Olivier Thomann CLA 2004-04-30 10:33:20 EDT
Here is the bytecodes we produce using latest:
  void m2(A x);
     0  aload_0
     1  aload_1
     2  new #2 X
     5  dup
     6  invokespecial #18 <Method X.<init>()V>
     9  invokeinterface [nargs : 2] #24 <Interface method
A.m1(Ljava/lang/Object;)[Ljava/lang/Object;>
    14  invokevirtual #28 <Method X.m3([LX;)V>
    17  return

You can see that the invokeinterface method returns an array of java.lang.Object
and the invokevirtual expects an array of X on the stack.

javac produces:
  void m2(A arg);
     0  aload_0
     1  aload_1
     2  new #2 X
     5  dup
     6  invokespecial #3 <Method X.<init>()V>
     9  invokeinterface [nargs : 2] #4 <Interface method
A.m1(Ljava/lang/Object;)[Ljava/lang/Object;>
    14  checkcast #5 [LX;
    17  invokevirtual #6 <Method X.m3([LX;)V>
    20  return

The intermediate checkcast converts the array of java.lang.Object into an array
of X and then the invokevirtual call is fine.
Comment 2 Philipe Mulet CLA 2004-05-07 12:21:24 EDT
Our check for adding generic casts did only consider case where original 
return type was a variable. Array of variable (T[]) wasn't handled.

Added regression test: GenericTypeTest#test175. Also upgraded similarily all 
generic cast inserted: field, name, qualified name, ...

Fixed