bevyengine/bevy · error

Attempted to push invalid value of type {}.

Error message

Attempted to push invalid value of type {}.

What it means

The List implementation for smallvec::SmallVec<T> recovers pushed elements with value.try_take::<T::Item>() plus a <T as SmallArray>::Item::from_reflect(&*value) fallback. List::push panics with this message when the boxed value is neither the array's Item type nor FromReflect-convertible to it.

Source

Thrown at crates/bevy_reflect/src/impls/smallvec.rs:53

        let value = value.try_take::<T::Item>().unwrap_or_else(|value| {
            <T as SmallArray>::Item::from_reflect(&*value).unwrap_or_else(|| {
                panic!(
                    "Attempted to insert invalid value of type {}.",
                    value.reflect_type_path()
                )
            })
        });
        SmallVec::insert(self, index, value);
    }

    fn remove(&mut self, index: usize) -> Box<dyn PartialReflect> {
        Box::new(self.remove(index))
    }

    fn push(&mut self, value: Box<dyn PartialReflect>) {
        let value = value.try_take::<T::Item>().unwrap_or_else(|value| {
            <T as SmallArray>::Item::from_reflect(&*value).unwrap_or_else(|| {
                panic!(
                    "Attempted to push invalid value of type {}.",
                    value.reflect_type_path()
                )
            })
        });
        SmallVec::push(self, value);
    }

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

    fn len(&self) -> usize {
        <SmallVec<T>>::len(self)
    }

    fn iter(&self) -> ListIter<'_> {

View on GitHub (pinned to 396ca72708)

Solutions

  1. Push values of the exact Item type (verify via reflect_type_path()).
  2. Convert before pushing: Item::from_reflect(&*value), then push the converted value.
  3. Use the typed SmallVec::push after downcasting with try_downcast_mut::<SmallVec<T>>().
  4. Add a type gate in shared code that funnels Box<dyn PartialReflect> into lists.

Example fix

// before
sv.push(Box::new(1_u8)); // SmallVec<[u32; 16]> -> panics

// after
sv.push(Box::new(1_u32));
Defensive patterns

Strategy: type-guard

Type guard

use bevy_reflect::{FromReflect, PartialReflect};

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

if push_item_ok::<u32>(boxed.as_ref()) {
    sv.push(boxed);
}

Prevention

When it happens

Trigger: Calling List::push on a reflected SmallVec with a mismatched element, e.g. pushing Box::new("x") onto a SmallVec<[u32; 16]>.

Common situations: Editor/inspector appending elements; scene or network data appended into small-buffer component fields; interop code pushing host-typed values without conversion.

Related errors


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