bevyengine/bevy · error

Attempted to clone invalid key of type {}.

Error message

Attempted to clone invalid key of type {}.

What it means

Panic in the reflected Map impl for IndexMap's to_dynamic_map: a key could not be cloned via K::from_reflect because its reflected value does not match K. The offending key's type path is in the message; ensure the map's keys are correctly reflected as K.

Source

Thrown at crates/bevy_reflect/src/impls/indexmap.rs:66

            .map(|(key, value)| {
                (
                    Box::new(key) as Box<dyn PartialReflect>,
                    Box::new(value) as Box<dyn PartialReflect>,
                )
            })
            .collect()
    }

    fn retain(&mut self, f: &mut dyn FnMut(&dyn PartialReflect, &mut dyn PartialReflect) -> bool) {
        self.retain(move |key, value| f(key, value));
    }

    fn to_dynamic_map(&self) -> Result<DynamicMap, ReflectCloneError> {
        let mut dynamic_map = DynamicMap::default();
        dynamic_map.set_represented_type(PartialReflect::get_represented_type_info(self));
        for (k, v) in self {
            let key = K::from_reflect(k).unwrap_or_else(|| {
                panic!(
                    "Attempted to clone invalid key of type {}.",
                    k.reflect_type_path()
                )
            });
            dynamic_map.insert_boxed(Box::new(key), v.to_dynamic()?);
        }
        Ok(dynamic_map)
    }

    fn insert_boxed(
        &mut self,
        key: Box<dyn PartialReflect>,
        value: Box<dyn PartialReflect>,
    ) -> Option<Box<dyn PartialReflect>> {
        let key = K::take_from_reflect(key).unwrap_or_else(|key| {
            panic!(
                "Attempted to insert invalid key of type {}.",
                key.reflect_type_path()

View on GitHub (pinned to 396ca72708)

Solutions

  1. Ensure the key type K implements FromReflect
  2. Avoid to_dynamic_map for maps with non-reflectable keys
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at crates/bevy_reflect/src/impls/indexmap.rs:66 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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