oxc-project/oxc · error

Iterator supplied too few elements

Error message

Iterator supplied too few elements

What it means

Panic inside alloc_slice_fill_iter: the arena allocates a slice sized by the iterator's ExactSizeIterator::len(), then the fill closure calls iter.next().expect(...). If the iterator reports a larger len() than the number of items it actually yields, next() returns None and this contract assertion fires. The fault is a misimplemented ExactSizeIterator, not oxc code.

Source

Thrown at crates/oxc_allocator/src/arena/alloc.rs:379

    ///
    /// # Example
    ///
    /// ```
    /// # use oxc_allocator::arena::Arena;
    ///
    /// let arena = Arena::new();
    /// let x: &[i32] = arena.alloc_slice_fill_iter([2, 3, 5].iter().cloned().map(|i| i * i));
    /// assert_eq!(x, [4, 9, 25]);
    /// ```
    #[inline(always)]
    pub fn alloc_slice_fill_iter<T, I>(&self, iter: I) -> &mut [T]
    where
        I: IntoIterator<Item = T>,
        I::IntoIter: ExactSizeIterator,
    {
        let mut iter = iter.into_iter();
        self.alloc_slice_fill_with(iter.len(), |_| {
            iter.next().expect("Iterator supplied too few elements")
        })
    }

    /// Allocate a new slice of size `len` into this `Arena`. Returns an exclusive reference to the slice.
    ///
    /// All elements of the slice are initialized to [`T::default()`].
    ///
    /// [`T::default()`]: https://doc.rust-lang.org/std/default/trait.Default.html#tymethod.default
    ///
    /// # Panics
    ///
    /// Panics if reserving space for the slice fails.
    ///
    /// # Example
    ///
    /// ```
    /// # use oxc_allocator::arena::Arena;
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Fix the iterator so len() exactly matches the number of remaining items
  2. Collect into a Vec/VecFromIter first and allocate from the collection when the iterator is not trusted
  3. Use alloc_slice_fill_with with an explicit count instead
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_allocator/src/arena/alloc.rs:379 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/be26997852c6ddb7. Report an issue: GitHub.