{"record":{"id":"48e01ebd27d32599","repo":"emilk/egui","slug":"pos2-index-out-of-bounds-index","errorCode":null,"errorMessage":"Pos2 index out of bounds: {index}","messagePattern":"Pos2 index out of bounds: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/emath/src/pos2.rs","lineNumber":217,"sourceCode":"\n    /// Linearly interpolate towards another point, so that `0.0 => self, 1.0 => other`.\n    pub fn lerp(&self, other: Self, t: f32) -> Self {\n        Self {\n            x: lerp(self.x..=other.x, t),\n            y: lerp(self.y..=other.y, t),\n        }\n    }\n}\n\nimpl core::ops::Index<usize> for Pos2 {\n    type Output = f32;\n\n    #[inline(always)]\n    fn index(&self, index: usize) -> &f32 {\n        match index {\n            0 => &self.x,\n            1 => &self.y,\n            _ => panic!(\"Pos2 index out of bounds: {index}\"),\n        }\n    }\n}\n\nimpl core::ops::IndexMut<usize> for Pos2 {\n    #[inline(always)]\n    fn index_mut(&mut self, index: usize) -> &mut f32 {\n        match index {\n            0 => &mut self.x,\n            1 => &mut self.y,\n            _ => panic!(\"Pos2 index out of bounds: {index}\"),\n        }\n    }\n}\n\nimpl PartialEq for Pos2 {\n    #[track_caller]\n    #[inline]","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/emilk/egui/blob/441971a776322a482e371775219380eca812cfa9/crates/emath/src/pos2.rs#L199-L235","documentation":"Pos2 implements Index<usize> so p[0]/p[1] read x/y, but any other index panics with \"Pos2 index out of bounds\". This is a deliberate guard because a 2D point has only two components; there is no z or further element. The panic reports the offending index and points at the indexing site via track_caller.","triggerScenarios":"Indexing a Pos2 with a value other than 0 or 1, e.g. p[2], p[i] with a loop bound of 3, or indexing with a variable derived from a 3-component loop (pos2[i] in code shared with Vec3).","commonSituations":"Generic code written for 3D vectors (Vec3) reused on 2D points, loop bounds copied from 3-component data, confusing Pos2 with Rgba (which has 4 components) or Vec3, off-by-one when iterating coordinates.","solutions":["Fix the index to be 0 (x) or 1 (y); for z you need a Vec3/emath Vec3, not Pos2.","Clamp or bound any dynamic index: assert!(i < 2) before p[i].","Use p.x / p.y field access instead of indexing to get compile-time safety.","If the data is truly 3D, change the type from Pos2 to Vec3 rather than indexing Pos2 out of range."],"exampleFix":"// before\nlet z = pos2[2]; // panics\n\n// after\nlet z = vec3.z; // use Vec3 for 3 components, or:\nlet y = pos2.y; // field access instead of index","handlingStrategy":"type-guard","validationCode":"// before indexing\nlet component = |p: emath::Pos2, i: usize| -> Option<f32> {\n    match i { 0 => Some(p.x), 1 => Some(p.y), _ => None }\n};\nassert!(idx < 2, \"Pos2 has only x and y\");","typeGuard":"fn pos2_component(p: emath::Pos2, i: usize) -> Option<f32> {\n    match i { 0 => Some(p.x), 1 => Some(p.y), _ => None }\n}","tryCatchPattern":"// panics are not catchable idiomatically in Rust; validate instead\n// std::panic::catch_unwind only as a last resort around test code\nlet v = match idx { 0 => p.x, 1 => p.y, _ => return Err(\"index out of range\") };","preventionTips":["Use p.x and p.y field access instead of indexing for compile-time safety.","Don't reuse 3-component (Vec3) loop bounds or generic index-based code with Pos2.","Bounds-check any dynamic index against 2 before indexing a Pos2.","Remember component count: Pos2=2, Vec3=3, Rgba=4; choose the right type for the data."],"tags":["panic","index-out-of-bounds","rust","math"],"backgroundTag":"index-out-of-bounds","analyzedSha":"441971a776322a482e371775219380eca812cfa9","analyzedAt":"2026-09-12T04:29:21.500Z","contentChangedAt":"2026-09-12T04:29:21.500Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}