{"record":{"id":"eba707d7e56b71b6","repo":"bevyengine/bevy","slug":"attempted-to-insert-invalid-value-of-type-eba707","errorCode":null,"errorMessage":"Attempted to insert invalid value of type {}.","messagePattern":"Attempted to insert invalid value of type (.+?)\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/bevy_reflect/src/impls/smallvec.rs","lineNumber":37,"sourceCode":"        if index < SmallVec::len(self) {\n            Some(&self[index] as &dyn PartialReflect)\n        } else {\n            None\n        }\n    }\n\n    fn get_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect> {\n        if index < SmallVec::len(self) {\n            Some(&mut self[index] as &mut dyn PartialReflect)\n        } else {\n            None\n        }\n    }\n\n    fn insert(&mut self, index: usize, value: Box<dyn PartialReflect>) {\n        let value = value.try_take::<T::Item>().unwrap_or_else(|value| {\n            <T as SmallArray>::Item::from_reflect(&*value).unwrap_or_else(|| {\n                panic!(\n                    \"Attempted to insert invalid value of type {}.\",\n                    value.reflect_type_path()\n                )\n            })\n        });\n        SmallVec::insert(self, index, value);\n    }\n\n    fn remove(&mut self, index: usize) -> Box<dyn PartialReflect> {\n        Box::new(self.remove(index))\n    }\n\n    fn push(&mut self, value: Box<dyn PartialReflect>) {\n        let value = value.try_take::<T::Item>().unwrap_or_else(|value| {\n            <T as SmallArray>::Item::from_reflect(&*value).unwrap_or_else(|| {\n                panic!(\n                    \"Attempted to push invalid value of type {}.\",\n                    value.reflect_type_path()","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_reflect/src/impls/smallvec.rs#L19-L55","documentation":"bevy_reflect implements List for smallvec::SmallVec<T> where T: SmallArray. List::insert(index, Box<dyn PartialReflect>) recovers the element with value.try_take::<T::Item>() and, failing that, <T as SmallArray>::Item::from_reflect(&*value). If neither an owning downcast nor a FromReflect conversion succeeds, this panic fires with the value's type path.","triggerScenarios":"Calling List::insert on a reflected SmallVec with an element that is not the array's Item type and cannot convert, e.g. smallvec.insert(0, Box::new('c')) on a SmallVec<[f32; 8]>.","commonSituations":"Component fields declared as SmallVec (positions, ids, small buffers) being patched via scenes or editors; generic reflection code that does not special-case SmallVec element types; version drift between serialized element types and current Item.","solutions":["Insert values whose type equals the SmallVec's Item type (log reflect_type_path() to confirm).","Convert dynamic/wrong-typed values explicitly with <T as SmallArray>::Item::from_reflect(&*value) before inserting.","Downcast to &mut SmallVec<T> and use the typed SmallVec::insert.","Check the value against the registered ListInfo/TypeInfo for the SmallVec before inserting."],"exampleFix":"// before\nsv.insert(0, Box::new(9_u8)); // SmallVec<[f32; 4]> -> panics\n\n// after\nsv.insert(0, Box::new(9.0_f32));","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"use bevy_reflect::{FromReflect, PartialReflect};\n\nfn item_ok<I: FromReflect>(value: &dyn PartialReflect) -> bool {\n    value.try_downcast_ref::<I>().is_some() || I::from_reflect(value).is_some()\n}\n\n// before List::insert on a SmallVec<[f32; 4]>:\nif item_ok::<f32>(boxed.as_ref()) {\n    sv.insert(0, boxed);\n}","tryCatchPattern":null,"preventionTips":["Remember SmallVec's element type is T::Item — guard with that exact type, not the SmallVec type.","Downcast to &mut SmallVec<T> for typed inserts in non-generic paths."],"tags":["rust","bevy","bevy-reflect","smallvec","list","insert","panic","type-mismatch"],"backgroundTag":"runtime-type-mismatch","analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}