Missing CHECKCAST before unboxing conversion in assignment

Issue #257 resolved
Jesper Öqvist created an issue

ExtendJ 8.1.0-1-g87f5bbc Java SE 8

The following test generates incorrect bytecode:

// Test that Integer unboxing works in runtime.
import java.util.*;

public class Test {
  public static void main(String[] args) {
    Stack<Integer> stack = new Stack<Integer>();
    stack.push(10);
    stack.push(-20);
    stack.push(50);
    stack.pop();
    System.out.println(peek(stack));
  }

  static int peek(Stack<Integer> stack) {
    int res = 0;
    res += stack.peek(); // Conversion: Integer -> int.
    return res;
  }
}

Here is the generated bytecode for the peek method:

  static int peek(java.util.Stack<java.lang.Integer>);
    flags: ACC_STATIC
    Code:
      stack=2, locals=3, args_size=1
         0: iconst_0
         1: istore_2
         2: iload_2
         3: aload_0
         4: invokevirtual #57                 // Method java/util/Stack.peek:()Ljava/lang/Object;
         7: invokevirtual #61                 // Method java/lang/Integer.intValue:()I
        10: iadd
        11: istore_2
        12: iload_2
        13: ireturn

There should be a CHECKCAST before the call to Integer.intValue().

Expected result: the bytecode should be executable.

Actual result: the compiled program fails to run with this error message:

    [junit] [FAIL] runTest[generics/autoboxing_02p](tests.extendj.TestJava7)
    [junit] Error output files differ expected:<[]> but was:<[Error: A JNI error has occurred, please check your installation and try again
    [junit] Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
    [junit] Exception Details:
    [junit]   Location:
    [junit]     Test.peek(Ljava/util/Stack;)I @7: invokevirtual
    [junit]   Reason:
    [junit]     Type 'java/lang/Object' (current frame, stack[1]) is not assignable to 'java/lang/Integer'

Comments (1)

  1. Jesper Öqvist reporter

    Fix error in assignment casting conversions

    When converting the source type in an assignment expression, generic type erasure was not handled correctly, leading to missing casting operations.

    fixes #257 (bitbucket)

    → <<cset 3bdb4ee7405d>>

  2. Log in to comment