{"record":{"id":"0502fdc3f83b39dc","repo":"oracle/graal","slug":"element-should-be-an-espresso-object-constant","errorCode":null,"errorMessage":"Element {} should be an espresso object constant, got {}","messagePattern":"Element (.+?) should be an espresso object constant, got (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"espresso-compiler-stub/src/com.oracle.truffle.espresso.vmaccess/src/com/oracle/truffle/espresso/vmaccess/EspressoExternalVMAccess.java","lineNumber":566,"sourceCode":"            JavaKind javaKind = elementalType.getJavaKind();\n            for (int i = 0; i < elements.length; i++) {\n                JavaConstant element = elements[i];\n                if (javaKind != element.getJavaKind()) {\n                    throw new IllegalArgumentException(\"Element \" + i + \" should be a \" + javaKind + \" but was \" + element.getJavaKind());\n                }\n                if (element.isDefaultForKind()) {\n                    continue;\n                }\n                array.setArrayElement(i, element.asBoxedPrimitive());\n            }\n        } else {\n            for (int i = 0; i < elements.length; i++) {\n                JavaConstant element = elements[i];\n                if (element.isNull()) {\n                    continue;\n                }\n                if (!(element instanceof EspressoExternalObjectConstant objectElement)) {\n                    throw new IllegalArgumentException(\"Element \" + i + \" should be an espresso object constant, got \" + safeGetClass(element));\n                }\n                try {\n                    array.setArrayElement(i, objectElement.getValue());\n                } catch (ClassCastException e) {\n                    throw new IllegalArgumentException(\"Element \" + i + \" is not an instance of the component type\", e);\n                }\n            }\n        }\n        return new EspressoExternalObjectConstant(this, array);\n    }\n\n    @Override\n    public JavaConstant createPrimitiveArray(JavaKind kind, int length) {\n        if (kind == null || !kind.isPrimitive() || kind == JavaKind.Void) {\n            throw new IllegalArgumentException(\"Expected a non-void primitive kind, got \" + kind);\n        }\n        if (length < 0) {\n            throw new NegativeArraySizeException(\"Negative array size: \" + length);","sourceCodeStart":548,"sourceCodeEnd":584,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/espresso-compiler-stub/src/com.oracle.truffle.espresso.vmaccess/src/com/oracle/truffle/espresso/vmaccess/EspressoExternalVMAccess.java#L548-L584","documentation":"In asArrayConstant's object-array branch, every non-null element must be an EspressoExternalObjectConstant so the guest Value can be stored into the guest array with setArrayElement. An element constant from another JVMCI backend (or a primitive-boxed constant where an object constant was expected) has no guest Value and throws IllegalArgumentException naming the offending index.","triggerScenarios":"Building an object array (or a multi-dimensional primitive array, dimensions > 1) where some elements are host constants, JavaConstant.forObject host values, or constants from another Espresso context.","commonSituations":"Mixed-origin element lists in substitutions; element constants cached from a previous provider version; multi-dimensional array construction where callers wrongly pass primitive constants instead of nested Espresso array constants.","solutions":["Produce every element through this Espresso provider (its constant reflection / previous asArrayConstant results).","For nested arrays, build each inner array with asArrayConstant first so elements are EspressoExternalObjectConstant.","Filter or convert foreign constants before the call; keep nulls (they are skipped)."],"exampleFix":"// before\nelems[i] = JavaConstant.forObject(hostString);\n\n// after\nelems[i] = espressoConstantReflection.forObject(guestString); // same context","handlingStrategy":"validation","validationCode":"for (int i = 0; i < elements.length; i++) {\n    JavaConstant c = elements[i];\n    if (!c.isNull() && !(c instanceof EspressoExternalObjectConstant)) {\n        // convert or reject before the call\n    }\n}","typeGuard":"static boolean allEspressoOrNull(JavaConstant[] elems) {\n    for (JavaConstant c : elems) {\n        if (!c.isNull() && !(c instanceof EspressoExternalObjectConstant)) return false;\n    }\n    return true;\n}","tryCatchPattern":"catch (IllegalArgumentException e) naming the index — use the index to locate and re-create the offending element through the Espresso provider, then retry.","preventionTips":["Build object-array elements only via the same Espresso provider.","For nested arrays, create inner arrays with asArrayConstant first.","Null elements are fine — no need to filter them."],"tags":["jvmci","espresso","graalvm","arrays","constants"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}