astral-sh/ruff · error

index is bounded by INLINE_MAX_SEGMENTS

Error message

index is bounded by INLINE_MAX_SEGMENTS

What it means

SmallSegmentsInfoIterator decodes segments back out of the packed u64. Its .expect("index is bounded by INLINE_MAX_SEGMENTS") asserts the stored segment count never exceeds 7, which construction in try_from_slice guarantees; the loop also stops at the stored count.

Source

Thrown at crates/ty_python_core/src/member.rs:947

struct SmallSegmentsInfoIterator {
    segments: SmallSegments,
    index: usize,
    next_offset: TextSize,
}

impl Iterator for SmallSegmentsInfoIterator {
    type Item = SegmentInfo;

    fn next(&mut self) -> Option<Self::Item> {
        let count = self.segments.len();
        if self.index >= count {
            return None;
        }

        // Extract the relative offset and kind for the current segment
        let shift = INLINE_COUNT_BITS
            + (u32::try_from(self.index).expect("index is bounded by INLINE_MAX_SEGMENTS")
                * INLINE_SEGMENT_BITS);
        let segment_data = (self.segments.0 >> shift) & INLINE_SEGMENT_MASK;
        let kind = (segment_data & INLINE_KIND_MASK) as u8;
        let relative_offset = ((segment_data >> INLINE_KIND_BITS) & INLINE_PREV_LEN_MASK) as u32;

        // Update the running absolute offset
        self.next_offset += TextSize::new(relative_offset);

        let kind = match kind {
            0 => SegmentKind::Attribute,
            1 => SegmentKind::IntSubscript,
            2 => SegmentKind::StringSubscript,
            3 => SegmentKind::BytesSubscript,
            _ => panic!("Invalid SegmentKind bits"),
        };

        self.index += 1;
        Some(SegmentInfo::new(kind, self.next_offset))

View on GitHub (pinned to fca5c7cf2c)

Solutions

  1. Treat any occurrence as a bug: minimize the repro and report it upstream
  2. When altering the inline layout, update count encoding and iterator decoding together
  3. Add a pack-then-iterate round-trip test for 1..=7 segments
Defensive patterns

Strategy: validation

Validate before calling

// round-trip invariant test: decode exactly as many segments as were packed
for n in 1..=INLINE_MAX_SEGMENTS { assert_eq!(SmallSegments::try_from_slice(&segs[..n]).unwrap().iter().count(), n); }

Try / catch

let out = std::panic::catch_unwind(|| small.iter().collect::<Vec<_>>()); // isolate panics in fuzzing

Prevention

When it happens

Trigger: Iterating a SmallSegments whose packed count field is corrupted or was produced outside try_from_slice. Well-formed values cannot exceed 7 segments, so this fires only on internal bugs.

Common situations: Property/fuzz tests mutating the packed u64; refactors that change the count encoding without updating the iterator's decoding shifts.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of astral-sh/ruff@fca5c7cf2c (2026-08-20). Data as JSON: /api/errors/5bb6aef7bc3c4fb3. Report an issue: GitHub.