oracle/graal · error · IllegalArgumentException

Element {} should be a {} but was {}

Error message

Element {} should be a {} but was {}

What it means

While filling a guest primitive array in asArrayConstant, each element's JavaKind must exactly match the array's elemental kind (an int[] element must be JavaKind.Int, etc.). A mismatch throws IllegalArgumentException naming the index, expected kind, and actual kind. This mirrors Java's strict array-store typing for primitives, where no implicit primitive widening happens.

Source

Thrown at espresso-compiler-stub/src/com.oracle.truffle.espresso.vmaccess/src/com/oracle/truffle/espresso/vmaccess/EspressoExternalVMAccess.java:552

        EspressoResolvedJavaType elementalType = arrayType.getElementalType();
        Value array;
        boolean isPrimitiveArray;
        int dimensions = arrayType.getDimensions();
        if (elementalType instanceof EspressoExternalResolvedInstanceType elementalInstanceType) {
            array = invokeJVMCIHelper("newObjectArray", elementalInstanceType.getMetaObject(), dimensions, elements.length);
            isPrimitiveArray = false;
        } else {
            JavaKind javaKind = elementalType.getJavaKind();
            assert javaKind.isPrimitive() && javaKind != JavaKind.Void;
            array = invokeJVMCIHelper("newPrimitiveArray", (int) javaKind.getTypeChar(), dimensions, elements.length);
            isPrimitiveArray = dimensions == 1;
        }
        if (isPrimitiveArray) {
            JavaKind javaKind = elementalType.getJavaKind();
            for (int i = 0; i < elements.length; i++) {
                JavaConstant element = elements[i];
                if (javaKind != element.getJavaKind()) {
                    throw new IllegalArgumentException("Element " + i + " should be a " + javaKind + " but was " + element.getJavaKind());
                }
                if (element.isDefaultForKind()) {
                    continue;
                }
                array.setArrayElement(i, element.asBoxedPrimitive());
            }
        } else {
            for (int i = 0; i < elements.length; i++) {
                JavaConstant element = elements[i];
                if (element.isNull()) {
                    continue;
                }
                if (!(element instanceof EspressoExternalObjectConstant objectElement)) {
                    throw new IllegalArgumentException("Element " + i + " should be an espresso object constant, got " + safeGetClass(element));
                }
                try {
                    array.setArrayElement(i, objectElement.getValue());
                } catch (ClassCastException e) {

View on GitHub (pinned to a66e9ccd1d)

Solutions

  1. Create each element with the constant factory matching the component kind: JavaConstant.forInt/forLong/forDouble/... so kinds line up.
  2. Validate kinds in a loop before calling asArrayConstant when elements come from external input.
  3. Re-check the component type (including dimensions) after refactors of the array being built.

Example fix

// before
JavaConstant[] elems = {JavaConstant.forFloat(1.5f)};
vmAccess.asArrayConstant(intType, elems); // int[] vs Float kind

// after
JavaConstant[] elems = {JavaConstant.forInt(1)};
vmAccess.asArrayConstant(intType, elems);
Defensive patterns

Strategy: validation

Validate before calling

for (int i = 0; i < elements.length; i++) {
    if (elements[i].getJavaKind() != expectedKind) {
        throw new IllegalArgumentException("bad kind at " + i);
    }
}

Type guard

static boolean kindsMatch(JavaConstant[] elems, JavaKind kind) {
    for (JavaConstant c : elems) {
        if (c.getJavaKind() != kind) return false;
    }
    return true;
}

Try / catch

catch (IllegalArgumentException e) parsing the index/kind from the message is brittle; pre-validate instead and let the exception surface as a bug indicator.

Prevention

When it happens

Trigger: Passing elements[i] with a different JavaKind than the component type — e.g. JavaConstant.forFloat(...) into an int[] (multi-dimensional primitive arrays with dimensions > 1 take object elements and go down the other branch), or boxed/object constants into a 1-D primitive array.

Common situations: Auto-generated constant-array code that assumes int/long interchangeability; elements produced by asBoxedPrimitive or forObject that carry Object kind; refactor changing an array's component type without updating the element constants.

Related errors


AI-assisted analysis of oracle/graal@a66e9ccd1d (2026-08-14). Data as JSON: /api/errors/e102b6a73528b4ad. Report an issue: GitHub.