{"record":{"id":"9231d3957c607894","repo":"pola-rs/polars","slug":"internal-error-entered-unreachable-code-offset","errorCode":null,"errorMessage":"internal error: entered unreachable code","messagePattern":"internal error: entered unreachable code","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-arrow/src/offset.rs","lineNumber":529,"sourceCode":"    ///\n    /// # Safety\n    /// The caller must ensure `offset + length <= self.len()`\n    #[inline]\n    pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) {\n        self.0.slice_in_place_unchecked(offset..offset + length);\n    }\n\n    /// Returns an iterator with the lengths of the offsets\n    #[inline]\n    pub fn lengths(&self) -> impl ExactSizeIterator<Item = usize> + '_ {\n        self.0.windows(2).map(|w| (w[1] - w[0]).to_usize())\n    }\n\n    /// Returns `(offset, len)` pairs.\n    #[inline]\n    pub fn offset_and_length_iter(&self) -> impl ExactSizeIterator<Item = (usize, usize)> + '_ {\n        self.windows(2).map(|x| {\n            let [l, r] = x else { unreachable!() };\n            let l = l.to_usize();\n            let r = r.to_usize();\n            (l, r - l)\n        })\n    }\n\n    /// Offset and length of the primitive (leaf) array for a double+ nested list for every outer\n    /// row.\n    pub fn leaf_ranges_iter(\n        offsets: &[Self],\n    ) -> impl Iterator<Item = core::ops::Range<usize>> + '_ {\n        let others = &offsets[1..];\n\n        offsets[0].windows(2).map(move |x| {\n            let [l, r] = x else { unreachable!() };\n            let mut l = l.to_usize();\n            let mut r = r.to_usize();\n","sourceCodeStart":511,"sourceCodeEnd":547,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-arrow/src/offset.rs#L511-L547","documentation":"`OffsetsBuffer::offset_and_length_iter` walks the offsets buffer with `.windows(2)` and destructures each window with `let [l, r] = x else { unreachable!() }`. Since `windows(2)` always yields length-2 slices, the `unreachable!()` can only fire if the internal iterator contract is violated (or a buffer shorter than 2 elements is iterated in a way that surfaces a 1-element window, which should not happen). This is a defensive internal-invariant assertion, not a validated error path.","triggerScenarios":"Iterating `(offset, len)` pairs over an offsets buffer whose underlying slice has fewer than 2 elements — in that case the windows iterator is simply empty and the panic should never occur; it fires only if the windows API contract is broken or the code is refactored to produce different-sized windows. Practically: a bug/regression in the crate, not user input.","commonSituations":"Debugging polars-arrow internals; using a patched or nightly-modified build where `windows` semantics or the offset buffer layout changed; hitting a genuine crate bug while converting offset buffers to (start, length) pair iterators in list/array processing.","solutions":["Guard the input: check `offsets.len() >= 2` before calling offset_and_length_iter and handle empty/single-element buffers explicitly.","Update polars/polars-arrow to the latest patched version; if reproducible, file an issue with the offsets buffer contents.","Avoid patching internals; use the public `offsets().windows(2)` yourself with safe destructuring."],"exampleFix":"// before\nfor (start, len) in offsets.offset_and_length_iter() { ... }\n// after\nlet slice = offsets.as_slice();\nif slice.len() < 2 {\n    return Ok(()); // nothing to iterate\n}\nfor (start, len) in offsets.offset_and_length_iter() { ... }","handlingStrategy":"validation","validationCode":"if offsets.as_slice().len() < 2 {\n    // no (offset, len) pairs exist; skip iteration\n    return;\n}","typeGuard":"fn has_offset_pairs(offsets: &[impl Offset]) -> bool { offsets.len() >= 2 }","tryCatchPattern":null,"preventionTips":["Check offsets buffer length >= 2 before pair iteration.","Treat unreachable!-style panics in polars-arrow as crate bugs: reproduce minimally and report.","Prefer public, bounds-checked iteration (windows(2) with slice patterns) in your own code.","Pin polars versions in CI so internal invariants do not shift silently."],"tags":["arrow","offsets","unreachable","internal-invariant","rust"],"backgroundTag":"internal-invariant-violation","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}