bevyengine/bevy · error

Attempted to push invalid value of type {}.

Error message

Attempted to push invalid value of type {}.

What it means

The macro-generated List::push(Box<dyn PartialReflect>) for reflected list types recovers the element with T::take_from_reflect: an owning downcast to T, with a FromReflect fallback for dynamic values. Pushing a boxed value that is neither T nor convertible panics with this message naming the value's type path.

Source

Thrown at crates/bevy_reflect/src/impls/macros/list.rs:33

                fn insert(&mut self, index: usize, value: bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>) {
                    let value = value.try_take::<T>().unwrap_or_else(|value| {
                        T::from_reflect(&*value).unwrap_or_else(|| {
                            panic!(
                                "Attempted to insert invalid value of type {}.",
                                value.reflect_type_path()
                            )
                        })
                    });
                    $insert(self, index, value);
                }

                fn remove(&mut self, index: usize) -> bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect> {
                    bevy_platform::prelude::Box::new($remove(self, index))
                }

                fn push(&mut self, value: bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>) {
                    let value = T::take_from_reflect(value).unwrap_or_else(|value| {
                        panic!(
                            "Attempted to push invalid value of type {}.",
                            value.reflect_type_path()
                        )
                    });
                    $push(self, value);
                }

                fn pop(&mut self) -> Option<bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>> {
                    $pop(self).map(|value| bevy_platform::prelude::Box::new(value) as bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>)
                }

                #[inline]
                fn len(&self) -> usize {
                    <$sub>::len(self)
                }

                #[inline]
                fn iter(&self) -> $crate::list::ListIter<'_>  {

View on GitHub (pinned to 396ca72708)

Solutions

  1. Push a value of the exact element type T (verify with reflect_type_path() on the boxed value).
  2. Convert the incoming value first: T::from_reflect(&*value) or an explicit Into/TryFrom, then push.
  3. Use the typed API: downcast to &mut Vec<T> and call Vec::push for compile-time guarantees.
  4. Guard shared helper code that accepts Box<dyn PartialReflect> with an element-type check before pushing.

Example fix

// before
list.push(Box::new(3_i64)); // Vec<f32> -> panics

// after
let v = f32::from_reflect(&*boxed_value).expect("expected f32 element");
list.push(Box::new(v));
Defensive patterns

Strategy: type-guard

Type guard

use bevy_reflect::{FromReflect, PartialReflect};

fn push_ok<T: FromReflect>(value: &dyn PartialReflect) -> bool {
    value.try_downcast_ref::<T>().is_some() || T::from_reflect(value).is_some()
}

if push_ok::<f32>(value.as_ref()) {
    list.push(value);
}

Prevention

When it happens

Trigger: Calling List::push on a reflected list (Vec<T>, VecDeque<T>, etc.) with a mismatched boxed value, e.g. pushing Box::new("text") onto a Vec<usize>, or a dynamic value whose represented type is not T.

Common situations: Building lists dynamically from deserialized or network data; scripting/REPL bridges pushing host-language values into reflected Rust lists; copy logic that re-boxes elements from a list with a different element type.

Related errors


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