bevyengine/bevy · error · GridPlacementError

Zero is not a valid grid position

Error message

Zero is not a valid grid position

What it means

GridPlacementError::InvalidZeroIndex is the thiserror variant behind every zero-line panic in GridPlacement: the internal try_into_grid_index helper returns it when an i16 grid line of 0 fails NonZero::new. In the public API the constructors/setters immediately .expect() this error away (panicking with 'Invalid start/end value of 0.'), so the Display text 'Zero is not a valid grid position' surfaces whenever the error itself is formatted or propagated — for example in user-written fallible wrappers that match on GridPlacementError, reflection tooling, or logging that reports the cause.

Solutions

  1. Never pass 0 as a grid line: use 1-based positive lines or negative lines counted from the end.
  2. In fallible wrappers, match GridPlacementError::InvalidZeroIndex and map it to an automatic/None placement instead of propagating.
  3. When reporting, include the field name (start vs end) so callers know which argument was 0.
  4. Use GridPlacement::auto()/start()/end() variants that leave the offending line unset when the value is unknown.

Example fix

// before
fn place(start: i16) -> Result<GridPlacement, GridPlacementError> {
    Ok(GridPlacement::start(start)) // panics on 0 before the Result can help
}

// after
fn place(start: i16) -> Result<GridPlacement, GridPlacementError> {
    if start == 0 { return Err(GridPlacementError::InvalidZeroIndex); }
    Ok(GridPlacement::start(start))
}
Defensive patterns

Strategy: validation

Validate before calling

use std::num::NonZero;

// gate every line before it reaches a GridPlacement API
fn checked_line(line: i16) -> Result<NonZero<i16>, GridPlacementError> {
    NonZero::new(line).ok_or(GridPlacementError::InvalidZeroIndex)
}

Type guard

fn is_valid_grid_line(line: i16) -> bool { line != 0 }

Try / catch

// in fallible wrappers, match the enum and degrade to automatic placement
match checked_line(line) {
    Ok(line) => Ok(GridPlacement::start(line.get())),
    Err(e @ GridPlacementError::InvalidZeroIndex) => {
        warn!("{e}");
        Ok(GridPlacement::auto())
    }
    Err(e) => Err(e),
}

Prevention

When it happens

Trigger: Passing 0 as start or end to any GridPlacement constructor/setter (which converts this error into the corresponding panic); calling the conversion helpers from code copied into your crate; formatting or matching a GridPlacementError::InvalidZeroIndex produced by a fallible wrapper around GridPlacement.

Common situations: Writing a validate-placement layer that returns Result<GridPlacement, GridPlacementError>; upgrading Bevy versions where placement validation moved from ad-hoc checks to this enum; error reporting pipelines that Display the underlying cause chain of a layout failure.

Related errors


AI-assisted analysis of bevyengine/bevy@a62cce8c05 (2026-08-20). Data as JSON: /api/errors/0be54f196e6940e8. Report an issue: GitHub.

Appendix: source

Thrown at crates/bevy_ui/src/ui_node.rs:2250

        Self {
            repetition: GridTrackRepetition::Count(repetition),
            tracks: SmallVec::from_buf([GridTrack::min_content()]),
        }
        .into()
    }

    /// Create a repeating set of grid tracks with max-content size
    pub fn max_content<T: From<Self>>(repetition: u16) -> T {
        Self {
            repetition: GridTrackRepetition::Count(repetition),
            tracks: SmallVec::from_buf([GridTrack::max_content()]),
        }
        .into()
    }

    /// Create a repeating set of `fit-content()` grid tracks with fixed pixel limit
    pub fn fit_content_px<T: From<Self>>(repetition: u16, limit: f32) -> T {
        Self {
            repetition: GridTrackRepetition::Count(repetition),
            tracks: SmallVec::from_buf([GridTrack::fit_content_px(limit)]),
        }
        .into()
    }

    /// Create a repeating set of `fit-content()` grid tracks with percentage limit
    pub fn fit_content_percent<T: From<Self>>(repetition: u16, limit: f32) -> T {
        Self {
            repetition: GridTrackRepetition::Count(repetition),
            tracks: SmallVec::from_buf([GridTrack::fit_content_percent(limit)]),
        }
        .into()
    }

    /// Create a repeating set of `minmax()` grid track
    pub fn minmax<T: From<Self>>(
        repetition: impl Into<GridTrackRepetition>,

View on GitHub (pinned to a62cce8c05)