{"record":{"id":"807f1128859a579c","repo":"oracle/graal","slug":"expected-an-array-constant-for-dest-got","errorCode":null,"errorMessage":"Expected an array constant for dest, got {}","messagePattern":"Expected an array constant for dest, got (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"compiler/src/jdk.graal.compiler.hostvmaccess/src/jdk/graal/compiler/hostvmaccess/HostVMAccess.java","lineNumber":336,"sourceCode":"            case char[] array -> array.clone();\n            case int[] array -> array.clone();\n            case long[] array -> array.clone();\n            case float[] array -> array.clone();\n            case double[] array -> array.clone();\n            default -> throw new IllegalArgumentException(\"Expected a primitive array constant, got \" + primitiveArray);\n        };\n        return providers.getSnippetReflection().forObject(copy);\n    }\n\n    @Override\n    public void copyArray(JavaConstant src, int srcPos, JavaConstant dest, int destPos, int length) {\n        Object srcArray = providers.getSnippetReflection().asObject(Object.class, src);\n        if (srcArray == null || !srcArray.getClass().isArray()) {\n            throw new IllegalArgumentException(\"Expected an array constant for src, got \" + src);\n        }\n        Object destArray = providers.getSnippetReflection().asObject(Object.class, dest);\n        if (destArray == null || !destArray.getClass().isArray()) {\n            throw new IllegalArgumentException(\"Expected an array constant for dest, got \" + dest);\n        }\n        System.arraycopy(srcArray, srcPos, destArray, destPos, length);\n    }\n\n    private void doWriteArrayElement(Object array, ResolvedJavaType componentType, int index, JavaConstant element) {\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 {\n            if (!element.getJavaKind().isObject()) {\n                throw new IllegalArgumentException(\"Element \" + element + \" should be an object, got \" + componentType);\n            }\n            unwrappedValue = providers.getSnippetReflection().asObject(Object.class, element);\n        }\n        Array.set(array, index, unwrappedValue);","sourceCodeStart":318,"sourceCodeEnd":354,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/compiler/src/jdk.graal.compiler.hostvmaccess/src/jdk/graal/compiler/hostvmaccess/HostVMAccess.java#L318-L354","documentation":"Thrown by HostVMAccess.copyArray when the dest constant cannot be unwrapped to a live host array object: asObject(Object.class, dest) returned null or its class is not an array. It is the destination-side twin of the src guard, checked just before System.arraycopy.","triggerScenarios":"Calling copyArray with a dest constant that is null-valued, a non-array constant, or not backed by a host object; also reached when src and dest are swapped in the argument list so dest receives a scalar constant.","commonSituations":"Destination arrays not yet materialized (created only as type plans rather than via asArrayConstant); reusing a src constant variable for dest by mistake; constants deserialized from another context without object backing.","solutions":["Materialize the destination with asArrayConstant(componentType, ...) (or forObject over a real array) before copying","Verify dest unwraps to an array object before the call, mirroring the guard","Double-check src/dest argument order and that dest length >= destPos + length"],"exampleFix":"// before\nhostVM.copyArray(srcConstant, 0, unmaterializedDestConstant, 0, n);  // throws\n\n// after\nJavaConstant dest = hostVM.asArrayConstant(componentType, new JavaConstant[n]);\nhostVM.copyArray(srcConstant, 0, dest, 0, n);","handlingStrategy":"validation","validationCode":"Object d = snippetReflection.asObject(Object.class, dest);\nif (d == null || !d.getClass().isArray() || Array.getLength(d) < destPos + length) {\n    throw new IllegalArgumentException(\"dest not a sufficiently large array: \" + dest);","typeGuard":"static boolean isLiveArrayConstant(JavaConstant c, SnippetReflectionProvider sp) {\n    Object o = sp.asObject(Object.class, c);\n    return o != null && o.getClass().isArray();\n}","tryCatchPattern":"catch (IllegalArgumentException e) { allocate dest with asArrayConstant of the right length and retry }","preventionTips":["Allocate the destination before the copy, sized destPos+length","Never reuse the src variable for dest by copy-paste","Assert capacity of both arrays in test harnesses before copyArray"],"tags":["graalvm","hostvmaccess","arrays","validation"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}