{"record":{"id":"55b9f455a45eb0bf","repo":"bevyengine/bevy","slug":"attempted-to-insert-invalid-value-of-type-55b9f4","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/macros/list.rs","lineNumber":18,"sourceCode":"macro_rules! impl_reflect_for_veclike {\n    ($ty:ty, $insert:expr, $remove:expr, $push:expr, $pop:expr, $sub:ty) => {\n        const _: () = {\n            impl<T: $crate::from_reflect::FromReflect + $crate::info::MaybeTyped + $crate::type_path::TypePath + $crate::type_registry::GetTypeRegistration> $crate::list::List for $ty {\n                #[inline]\n                fn get(&self, index: usize) -> Option<&dyn $crate::reflect::PartialReflect> {\n                    <$sub>::get(self, index).map(|value| value as &dyn $crate::reflect::PartialReflect)\n                }\n\n                #[inline]\n                fn get_mut(&mut self, index: usize) -> Option<&mut dyn $crate::reflect::PartialReflect> {\n                    <$sub>::get_mut(self, index).map(|value| value as &mut dyn $crate::reflect::PartialReflect)\n                }\n\n                fn insert(&mut self, index: usize, value: bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>) {\n                    let value = value.try_take::<T>().unwrap_or_else(|value| {\n                        T::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                    $insert(self, index, value);\n                }\n\n                fn remove(&mut self, index: usize) -> bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect> {\n                    bevy_platform::prelude::Box::new($remove(self, index))\n                }\n\n                fn push(&mut self, value: bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>) {\n                    let value = T::take_from_reflect(value).unwrap_or_else(|value| {\n                        panic!(\n                            \"Attempted to push invalid value of type {}.\",\n                            value.reflect_type_path()\n                        )","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_reflect/src/impls/macros/list.rs#L1-L36","documentation":"The impl_list!-family macro implements List::insert(index, Box<dyn PartialReflect>) for reflected list types (e.g. arrays, VecDeque-like containers). The element is recovered with value.try_take::<T>() and, if that fails, a second chance is given via T::from_reflect(&*value) so dynamic values can convert. Only when both paths fail does this panic fire, printing the value's type path.","triggerScenarios":"Calling List::insert on a reflected list whose element type is T with a boxed value that is neither T nor FromReflect-convertible — e.g. list.insert(0, Box::new(1u8)) on a Vec<f32>, or inserting a DynamicUsize-like dynamic value into a Vec<String>.","commonSituations":"Scene deserialization after an element type changed; editor/inspector inserting property values chosen by users; generic code that moves elements between different lists via PartialReflect without matching element types.","solutions":["Make the inserted value's type equal the list's element type T (log reflect_type_path() on the boxed value to compare).","Convert dynamic values first: T::from_reflect(&*value) and insert only on Some.","Prefer typed access: downcast to &mut Vec<T> (or the concrete list) and call the ordinary insert.","If the value genuinely has another type, convert it with TryFrom/From or bevy's reflect conversion utilities before boxing it."],"exampleFix":"// before\nlist.insert(0, Box::new(2_u8)); // Vec<f32> -> panics\n\n// after\nlist.insert(0, Box::new(2.0_f32));","handlingStrategy":"type-guard","validationCode":"if list.get_represented_type_info().map(|i| i.kind() == bevy_reflect::ReflectKind::List).unwrap_or(false) { /* then insert is the only remaining risk: check the element */ }","typeGuard":"use bevy_reflect::{FromReflect, PartialReflect, List};\n\nfn element_ok<T: FromReflect>(value: &dyn PartialReflect) -> bool {\n    value.try_downcast_ref::<T>().is_some() || T::from_reflect(value).is_some()\n}\n\n// guard every insert:\nassert!(element_ok::<f32>(boxed.as_ref()), \"bad element for this list\");","tryCatchPattern":null,"preventionTips":["Funnel all erased List::insert calls through one helper that guards the element type first.","Derive FromReflect on element types expected to receive dynamic values."],"tags":["rust","bevy","bevy-reflect","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"}