pola-rs/polars · error

offset + length overflowed

Error message

offset + length overflowed

What it means

Compile-time panic from #[polars_expr] in pyo3-polars-derive (lib.rs:128). With two extra parameters after the input slice, the only accepted pair is ("context", "kwargs"); any other combination of names panics with "didn't expect arguments {a}, {b}" showing both offending names. The names are matched exactly — misspellings like ctx, contex, kw_args, or unrelated names like options/extra all land here.

Source

Thrown at crates/polars-arrow/src/array/mod.rs:466

        let f = |x: &$ty| Box::new(x.clone());
        general_dyn!($array, $ty, f)
    }};
}

// macro implementing `sliced` and `sliced_unchecked`
macro_rules! impl_sliced {
    () => {
        /// Returns this array sliced.
        /// # Implementation
        /// This function is `O(1)`.
        /// # Panics
        /// iff `offset + length > self.len()`.
        #[inline]
        #[must_use]
        pub fn sliced(self, offset: usize, length: usize) -> Self {
            let total = offset
                .checked_add(length)
                .expect("offset + length overflowed");
            assert!(
                total <= self.len(),
                "the offset of the new Buffer cannot exceed the existing length"
            );
            unsafe { Self::sliced_unchecked(self, offset, length) }
        }

        /// Returns this array sliced.
        /// # Implementation
        /// This function is `O(1)`.
        ///
        /// # Safety
        /// The caller must ensure that `offset + length <= self.len()`.
        #[inline]
        #[must_use]
        pub unsafe fn sliced_unchecked(mut self, offset: usize, length: usize) -> Self {
            Self::slice_unchecked(&mut self, offset, length);
            self

View on GitHub (pinned to 68506541d2)

Solutions

  1. Rename both parameters to exactly context: CallerContext and kwargs: YourKwargsStruct, in that order
  2. If one of the two is custom configuration, move it into the kwargs struct (a Deserialize type deserialized via the pickle protocol) instead of a separate positional parameter
  3. Drop unneeded parameters so only the recognized ones remain
  4. Re-run cargo check after each rename; the panic message names the exact identifiers it rejected

Example fix

// before — `ctx` and `options` are not recognized names
#[polars_expr(output_type=String)]
fn f(inputs: &[Series], ctx: CallerContext, options: MyKwargs) -> PolarsResult<Series> {
    todo!()
}

// after
#[polars_expr(output_type=String)]
fn f(inputs: &[Series], context: CallerContext, kwargs: MyKwargs) -> PolarsResult<Series> {
    todo!()
}
Defensive patterns

Strategy: validation

Validate before calling

# ci/check_polars_expr.py — run before cargo build
import re, sys, pathlib
pat = re.compile(r"#\[polars_expr\([^)]*\)\]\s*(?:pub\s+)?fn\s+\w+\(([^)]*)\)", re.S)
for f in pathlib.Path("src").rglob("*.rs"):
    for m in pat.finditer(f.read_text()):
        params = [p.strip() for p in m.group(1).split(",") if p.strip()]
        if len(params) == 3:
            names = [p.split(":")[0].strip() for p in params[1:]]
            if names != ["context", "kwargs"]:
                sys.exit(f"{f}: trailing pair {names} must be exactly ['context', 'kwargs']")

Prevention

When it happens

Trigger: fn f(inputs: &[Series], ctx: CallerContext, kwargs: MyKwargs) (ctx misspelled), fn f(inputs: &[Series], context: CallerContext, options: MyKwargs) (second name unrecognized), or fn f(inputs: &[Series], factor: f64, kwargs: MyKwargs) (a positional config parameter the macro does not support).

Common situations: Trying to pass extra configuration as an additional positional function parameter instead of through the kwargs struct; partial rename refactorings that fix one name but not the other; code review feedback that renamed parameters for clarity, silently breaking the macro's name-based dispatch.

Related errors


AI-assisted analysis of pola-rs/polars@68506541d2 (2026-08-19). Data as JSON: /api/errors/fde4bd50fd467634. Report an issue: GitHub.