{"record":{"id":"048c959ddea2b0c2","repo":"bevyengine/bevy","slug":"spans-cannot-be-zero-length","errorCode":null,"errorMessage":"Spans cannot be zero length","messagePattern":"Spans cannot be zero length","errorType":"exception","errorClass":"GridPlacementError","httpStatus":null,"severity":"error","filePath":"crates/bevy_ui/src/ui_node.rs","lineNumber":2252,"sourceCode":"fn try_into_grid_index(index: i16) -> Result<Option<NonZero<i16>>, GridPlacementError> {\n    Ok(Some(\n        NonZero::<i16>::new(index).ok_or(GridPlacementError::InvalidZeroIndex)?,\n    ))\n}\n\n/// Convert a `u16` to `NonZero<u16>`, fails on `0` and returns the `InvalidZeroSpan` error.\nfn try_into_grid_span(span: u16) -> Result<Option<NonZero<u16>>, GridPlacementError> {\n    Ok(Some(\n        NonZero::<u16>::new(span).ok_or(GridPlacementError::InvalidZeroSpan)?,\n    ))\n}\n\n/// Errors that occur when setting constraints for a `GridPlacement`\n#[derive(Debug, Eq, PartialEq, Clone, Copy, Error)]\npub enum GridPlacementError {\n    #[error(\"Zero is not a valid grid position\")]\n    InvalidZeroIndex,\n    #[error(\"Spans cannot be zero length\")]\n    InvalidZeroSpan,\n}\n\n/// The background color of the node\n///\n/// This serves as the \"fill\" color.\n#[derive(Component, Copy, Clone, Debug, Deref, DerefMut, PartialEq, Reflect)]\n#[reflect(Component, Default, Debug, PartialEq, Clone)]\n#[cfg_attr(\n    feature = \"serialize\",\n    derive(serde::Serialize, serde::Deserialize),\n    reflect(Serialize, Deserialize)\n)]\npub struct BackgroundColor(pub Color);\n\nimpl BackgroundColor {\n    /// Background color is transparent by default.\n    pub const DEFAULT: Self = Self(Color::NONE);","sourceCodeStart":2234,"sourceCodeEnd":2270,"githubUrl":"https://github.com/bevyengine/bevy/blob/227d3a6c661b3bdf3020d3e8290b5a6ffd0226f7/crates/bevy_ui/src/ui_node.rs#L2234-L2270","documentation":"GridPlacementError::InvalidZeroSpan is the thiserror variant produced by try_into_grid_span when a u16 span of 0 fails NonZero::new — 'Spans cannot be zero length'. The public constructors .expect() it (surfacing as the 'Invalid span value of 0.' panics), and the Display text appears when the error is propagated or formatted by fallible user code built on the enum.","triggerScenarios":"Passing span == 0 to GridPlacement::span/start_span/end_span or set_span (converted to a panic); handling or logging a GridPlacementError::InvalidZeroSpan returned from a fallible placement-validation layer.","commonSituations":"Defensive placement builders that return Result and need to distinguish bad spans from bad lines; data-validation pipelines reporting why a serialized layout was rejected; shared layout libraries reused across Bevy versions.","solutions":["Guarantee spans are >= 1 at the call site; one track is the minimum occupancy.","Map InvalidZeroSpan in fallible code to a default span of 1 (or reject the record with a precise message).","Skip creating the node when a computed span legitimately equals 0 rather than remapping silently.","Unit-test data sources (configs, save files) for zero spans before they reach GridPlacement."],"exampleFix":"// before\nfn item(span: u16) -> Result<GridPlacement, GridPlacementError> {\n    Ok(GridPlacement::span(span)) // panics on span == 0\n}\n\n// after\nfn item(span: u16) -> Result<GridPlacement, GridPlacementError> {\n    if span == 0 { return Err(GridPlacementError::InvalidZeroSpan); }\n    Ok(GridPlacement::span(span))\n}","handlingStrategy":"validation","validationCode":"use std::num::NonZero;\n\nfn checked_span(span: u16) -> Result<NonZero<u16>, GridPlacementError> {\n    NonZero::new(span).ok_or(GridPlacementError::InvalidZeroSpan)\n}","typeGuard":"fn is_valid_grid_span(span: u16) -> bool { span != 0 }","tryCatchPattern":"match checked_span(span) {\n    Ok(span) => Ok(GridPlacement::span(span.get())),\n    Err(e @ GridPlacementError::InvalidZeroSpan) => {\n        warn!(\"{e}\");\n        Ok(GridPlacement::auto()) // span defaults to 1\n    }\n    Err(e) => Err(e),\n}","preventionTips":["Store spans as NonZero<u16> in your own layout types so invalid states are unrepresentable.","Reject zero spans in serialized layout schemas with a precise message.","Add a property test asserting generated placements always have span >= 1."],"tags":["rust","bevy","bevy-ui","grid","error-enum","validation"],"backgroundTag":"grid-placement-zero-value","analyzedSha":"227d3a6c661b3bdf3020d3e8290b5a6ffd0226f7","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}