{"record":{"id":"a1ea768302125127","repo":"apple/pkl","slug":"elementindexoutofrange-a1ea76","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/SetNodes.java","lineNumber":139,"sourceCode":"    @Specialization\n    protected boolean eval(VmSet self, VmCollection other) {\n      return self.startsWith(other);\n    }\n  }\n\n  public abstract static class endsWith extends ExternalMethod1Node {\n    @Specialization\n    protected boolean eval(VmSet self, VmCollection other) {\n      return self.endsWith(other);\n    }\n  }\n\n  public abstract static class split extends ExternalMethod1Node {\n    @Specialization\n    protected VmPair eval(VmSet self, long index) {\n      if (index < 0 || index > self.getLength()) {\n        CompilerDirectives.transferToInterpreter();\n        throw exceptionBuilder()\n            .evalError(\"elementIndexOutOfRange\", index, 0, self.getLength())\n            .withProgramValue(\"Collection\", self)\n            .build();\n      }\n      return self.split(index);\n    }\n  }\n\n  public abstract static class splitOrNull extends ExternalMethod1Node {\n    @Specialization\n    protected Object eval(VmSet self, long index) {\n      return self.splitOrNull(index);\n    }\n  }\n\n  public abstract static class partition extends ExternalMethod1Node {\n    @Child private ApplyVmFunction1Node applyLambdaNode = ApplyVmFunction1Node.create();\n","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/stdlib/base/SetNodes.java#L121-L157","documentation":"Thrown by Set.split(index) when the split index is out of range. The index must satisfy 0 <= index <= set.length; the error reports the offending index, the valid minimum 0, and the set's length, plus the collection itself.","triggerScenarios":"Calling someSet.split(n) where n < 0 or n > set.length. split(n) returns a pair of the first n elements and the rest, so only positions at or before the end are valid.","commonSituations":"Off-by-one errors using set.length as if it were invalid (it is valid — end split), computing the split point from another collection's size, or negative results from subtraction that were meant to be clamped.","solutions":["Check 0 <= index <= set.length before calling split","Clamp the index: math.max(0, math.min(idx, set.length))","Verify any computed split point against the actual set length (see the 'Collection' value in the error)","Remember split(n) with n == set.length is legal and yields (set, empty set)"],"exampleFix":"// before\nset.split(set.length + 1)\n// after\nset.split(math.min(idx, set.length))","handlingStrategy":"validation","validationCode":"function canSplit(s: Set, i: Int): Boolean = i >= 0 && i <= s.length","typeGuard":"function isValidSplitIndex(s: Set, i: Int): Boolean = i >= 0 && i <= s.length","tryCatchPattern":null,"preventionTips":["Validate/clamp the split index against set.length","Remember i == set.length is valid (yields the full set and empty set)","Clamp negative results of subtraction before splitting"],"tags":["pkl","set","index-out-of-range","stdlib"],"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-17T15:17:12.973Z"}