{"record":{"id":"ff0c8c34766df8ff","repo":"oracle/graal","slug":"expected-an-array-type-got","errorCode":null,"errorMessage":"Expected an array type, got ","messagePattern":"Expected an array type, got ","errorType":"exception","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":634,"sourceCode":"        }\n        try {\n            invoke(java_lang_System_arraycopy, null, srcArray, JavaConstant.forInt(srcPos), destArray, JavaConstant.forInt(destPos), JavaConstant.forInt(length));\n        } catch (InvocationException e) {\n            if (e.getCause() instanceof PolyglotException polyglotException) {\n                throw throwHostException(polyglotException);\n            }\n            throw e;\n        }\n    }\n\n    @Override\n    public void writeArrayElement(JavaConstant array, int index, JavaConstant element) {\n        if (!(array instanceof EspressoExternalObjectConstant espressoArray)) {\n            throw new IllegalArgumentException(\"Expected an EspressoExternalObjectConstant, got \" + safeGetClass(array));\n        }\n        EspressoResolvedObjectType arrayType = espressoArray.getType();\n        if (!arrayType.isArray()) {\n            throw new IllegalArgumentException(\"Expected an array type, got \" + arrayType);\n        }\n        ResolvedJavaType componentType = arrayType.getComponentType();\n        Object unwrappedValue;\n        if (componentType.isPrimitive()) {\n            if (componentType.getJavaKind() != element.getJavaKind()) {\n                throw new IllegalArgumentException(\"Element \" + element + \" should be a \" + componentType.getJavaKind() + \", got \" + element.getJavaKind());\n            }\n            unwrappedValue = element.asBoxedPrimitive();\n        } else if (element.isNull()) {\n            unwrappedValue = null;\n        } else {\n            if (!(element instanceof EspressoExternalObjectConstant objectElement)) {\n                throw new IllegalArgumentException(\"Element \" + element + \" should be an espresso object constant, got \" + safeGetClass(element));\n            }\n            unwrappedValue = objectElement.getValue();\n        }\n        try {\n            espressoArray.getValue().setArrayElement(index, unwrappedValue);","sourceCodeStart":616,"sourceCodeEnd":652,"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#L616-L652","documentation":"After unwrapping the constant, writeArrayElement requires its guest type to be an array type (isArray()) before reading the component type and performing the store. Writing into a non-array Espresso constant (a plain object, string, or class mirror) throws IllegalArgumentException('Expected an array type, ...') naming the actual type.","triggerScenarios":"Passing a scalar Espresso object constant, a String constant, or a mirror object where an array constant is expected.","commonSituations":"Element/enclosing-array variable confusion; placeholders passed before allocation; JSON/descriptor-driven store logic that mislabels fields as arrays.","solutions":["Check the constant's type with isArray() (on EspressoExternalObjectConstant.getType()) before the store.","Ensure the array is allocated (createPrimitiveArray / asArrayConstant) before the first writeArrayElement.","Log the type on guard failure to trace where the scalar constant was produced."],"exampleFix":"// before\nvmAccess.writeArrayElement(scalarConstant, 0, element);\n\n// after\nEspressoExternalObjectConstant arr = (EspressoExternalObjectConstant) constant;\nif (arr.getType().isArray()) {\n    vmAccess.writeArrayElement(arr, 0, element);\n}","handlingStrategy":"validation","validationCode":"if (array instanceof EspressoExternalObjectConstant e && !e.getType().isArray()) {\n    // scalar constant passed by mistake — resolve the intended array constant\n}","typeGuard":"static boolean isGuestArray(JavaConstant c) {\n    return c instanceof EspressoExternalObjectConstant e && e.getType().isArray();\n}","tryCatchPattern":null,"preventionTips":["Check isArray() on the constant's type before element writes.","Allocate the array before the first writeArrayElement call.","In descriptor-driven stores, validate that the target is declared as an array."],"tags":["jvmci","espresso","graalvm","arrays","validation"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}