oracle/graal · error · IllegalArgumentException

Element {} is not an instance of the component type

Error message

Element {} is not an instance of the component type

What it means

When storing an Espresso object constant into a guest object array, the Truffle setArrayElement call performs a real guest type check; a ClassCastException means the element's guest type is not assignable to the array's component type. EspressoExternalVMAccess rethrows it as IllegalArgumentException with the index, preserving the cause for diagnosis. This is the same failure you would get from ArrayStoreException semantics in a plain Java store.

Source

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

                }
                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) {
                    throw new IllegalArgumentException("Element " + i + " is not an instance of the component type", e);
                }
            }
        }
        return new EspressoExternalObjectConstant(this, array);
    }

    @Override
    public JavaConstant createPrimitiveArray(JavaKind kind, int length) {
        if (kind == null || !kind.isPrimitive() || kind == JavaKind.Void) {
            throw new IllegalArgumentException("Expected a non-void primitive kind, got " + kind);
        }
        if (length < 0) {
            throw new NegativeArraySizeException("Negative array size: " + length);
        }
        Value array = invokeJVMCIHelper("newPrimitiveArray", (int) kind.getTypeChar(), 1, length);
        return new EspressoExternalObjectConstant(this, array);
    }

View on GitHub (pinned to a66e9ccd1d)

Solutions

  1. Check element type compatibility first: element.getType().getTypeInterfaces()/superclass walk or element Value's isInstance against the component meta object.
  2. Fix the element production so it yields constants of the exact component type.
  3. If heterogeneous storage is intended, use Object[] (java.lang.Object) as the component type.

Example fix

// before
vmAccess.asArrayConstant(stringArrayType, new JavaConstant[]{intBoxConstant});

// after
vmAccess.asArrayConstant(objectArrayType, new JavaConstant[]{intBoxConstant}); // Object[]
Defensive patterns

Strategy: validation

Validate before calling

Value compMeta = /* component type meta object */;
for (int i = 0; i < elements.length; i++) {
    JavaConstant c = elements[i];
    if (!c.isNull() && !c.isDefaultForKind() && !compMeta.isInstance(((EspressoExternalObjectConstant) c).getValue())) {
        // element will not store; fix here
    }
}

Type guard

static boolean canStore(Value componentMetaObject, Value elementValue) {
    return componentMetaObject.isInstance(elementValue);
}

Try / catch

catch (IllegalArgumentException e) with cause ClassCastException -> inspect element i's guest type vs component type; switch to Object[] or fix the element source.

Prevention

When it happens

Trigger: Storing, e.g., an Integer constant into a String[] built via asArrayConstant; storing a subtype into an unrelated type's array; multi-dimensional arrays where the inner array's element type differs from what the outer store expects.

Common situations: Dynamically assembled constant arrays whose elements come from reflection over arbitrary guest objects; refactors that change a component type while reusing old element constants; polymorphic element sets passed to a monomorphic array.

Related errors


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