{"record":{"id":"4581071a398dd813","repo":"apple/pkl","slug":"elementindexoutofrange-458107","errorCode":"elementIndexOutOfRange","errorMessage":"elementIndexOutOfRange","messagePattern":"elementIndexOutOfRange","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/stdlib/base/ListNodes.java","lineNumber":87,"sourceCode":"      return self.getLastIndex();\n    }\n  }\n\n  public abstract static class getOrNull extends ExternalMethod1Node {\n    @Specialization\n    protected Object eval(VmList self, long index) {\n      return self.getOrNull(index);\n    }\n  }\n\n  public abstract static class sublist extends ExternalMethod2Node {\n    @Specialization\n    protected Object eval(VmList self, long start, long exclusiveEnd) {\n      var length = self.getLength();\n\n      if (start < 0 || start > length) {\n        CompilerDirectives.transferToInterpreter();\n        throw exceptionBuilder()\n            .evalError(\"elementIndexOutOfRange\", start, 0, length)\n            .withProgramValue(\"Collection\", self)\n            .build();\n      }\n      if (exclusiveEnd < start || exclusiveEnd > length) {\n        CompilerDirectives.transferToInterpreter();\n        throw exceptionBuilder()\n            .evalError(\"elementIndexOutOfRange\", exclusiveEnd, start, length)\n            .withProgramValue(\"Collection\", self)\n            .build();\n      }\n      return self.subList(start, exclusiveEnd);\n    }\n  }\n\n  public abstract static class sublistOrNull extends ExternalMethod2Node {\n    @Specialization\n    protected Object eval(VmList self, long start, long exclusiveEnd) {","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/stdlib/base/ListNodes.java#L69-L105","documentation":"Pkl's List.slice(start, exclusiveEnd) validates the start index before slicing. If start is negative or greater than the list length, the runtime throws elementIndexOutOfRange with the valid range [0, length]. The collection itself is attached as program value 'Collection' for debugging.","triggerScenarios":"Calling List.slice() with a negative start (e.g. slice(-1, 3)) or a start beyond the list length (e.g. slice(5, 6) on a 3-element list). Start == length is allowed (yields empty list) but start > length is not.","commonSituations":"Computing slice bounds from dynamic values such as page sizes, offsets derived from external config, or lengths of other collections that may be shorter than expected.","solutions":["Verify the start index is within 0..list.length before calling slice","Clamp the start index: Math.max(0, Math.min(start, list.length))","Check the upstream computation that produced the index (off-by-one, wrong list length)","Print/inspect list.length to confirm the expected size"],"exampleFix":"// before\nlocal start = items.length - extra  // extra > items.length -> negative\nitems.slice(start, items.length)\n// after\nlocal start = Math.max(0, items.length - extra)\nitems.slice(start, items.length)","handlingStrategy":"validation","validationCode":"if (start < 0 || start > list.length) throw new Error(\"slice start \"+start+\" out of [0, \"+list.length+\"]\")","typeGuard":"function isValidStart(list, start) { return typeof start === \"number\" && start >= 0 && start <= list.length; }","tryCatchPattern":null,"preventionTips":["Always clamp computed slice bounds to [0, list.length]","Never reuse indexes from a different collection without re-checking length","Assert list.length at the call site during development"],"tags":["pkl","list","slice","index-out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}