{"record":{"id":"7f10bcbbc547341e","repo":"bevyengine/bevy","slug":"kind-mismatch-expected-expected-received-re-7f10bc","errorCode":null,"errorMessage":"kind mismatch: expected {expected:?}, received {received:?}","messagePattern":"kind mismatch: expected (.+?), received (.+?)","errorType":"exception","errorClass":"ReflectKindMismatchError","httpStatus":null,"severity":"error","filePath":"crates/bevy_reflect/src/kind.rs","lineNumber":138,"sourceCode":"                    $name::List(_) => Self::List,\n                    $name::Array(_) => Self::Array,\n                    $name::Map(_) => Self::Map,\n                    $name::Set(_) => Self::Set,\n                    $name::Enum(_) => Self::Enum,\n                    #[cfg(feature = \"functions\")]\n                    $name::Function(_) => Self::Function,\n                    $name::Opaque(_) => Self::Opaque,\n                }\n            }\n        }\n    };\n}\n\n/// Caused when a type was expected to be of a certain [kind], but was not.\n///\n/// [kind]: ReflectKind\n#[derive(Debug, Error)]\n#[error(\"kind mismatch: expected {expected:?}, received {received:?}\")]\npub struct ReflectKindMismatchError {\n    /// Expected kind.\n    pub expected: ReflectKind,\n    /// Received kind.\n    pub received: ReflectKind,\n}\n\nmacro_rules! impl_cast_method {\n    ($name:ident : Opaque => $retval:ty) => {\n        #[doc = \"Attempts a cast to a [`PartialReflect`] trait object.\"]\n        #[doc = \"\\n\\nReturns an error if `self` is not the [`Self::Opaque`] variant.\"]\n        pub fn $name(self) -> Result<$retval, ReflectKindMismatchError> {\n            match self {\n                Self::Opaque(value) => Ok(value),\n                _ => Err(ReflectKindMismatchError {\n                    expected: ReflectKind::Opaque,\n                    received: self.kind(),\n                }),","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_reflect/src/kind.rs#L120-L156","documentation":"ReflectKindMismatchError is returned by the cast helpers generated with impl_cast_method! on the ReflectRef/ReflectMut/ReflectOwned enums (obtained via PartialReflect::reflect_ref/reflect_mut/reflect_owned), e.g. casting to the Map/List/Struct variant. If the value's actual kind differs, the error reports expected vs received ReflectKind. It also converts into ApplyError::MismatchedKinds inside try_apply.","triggerScenarios":"Calling a kind-cast on reflect_ref()/reflect_mut()/reflect_owned() for the wrong variant, e.g. value.reflect_ref().into_map() on a Vec, or try_as_struct-style helpers on an opaque value; also surfaced as 'attempted to apply X to Y' when try_apply hits a kind mismatch.","commonSituations":"Generic reflection tools (inspectors, serializers, diff/patch code) that assume input kinds; processing deserialized dynamic data whose kind differs from the destination; handling values from untrusted/external sources through PartialReflect.","solutions":["Branch on value.reflect_kind() (or match ReflectRef) before casting, and cover all kinds.","Use try_apply/try_as_* Result-returning APIs and handle Err instead of unwrap.","Validate external data kind early (e.g. require ReflectKind::Map before map processing) and reject with a clear message.","If the kinds should match, fix the data source or the generic bounds so the right kind reaches the cast."],"exampleFix":"// before\nlet r = value.reflect_ref().into_map().unwrap(); // panics on Err(ReflectKindMismatchError)\n\n// after\nmatch value.reflect_ref() {\n    ReflectRef::Map(m) => { /* map logic */ }\n    other => warn!(\"expected map, got {:?}\", ReflectKind::from(other)),\n}","handlingStrategy":"try-catch","validationCode":"if value.reflect_kind() != ReflectKind::Map {\n    return; // skip early instead of hitting the cast error\n}","typeGuard":"fn is_reflected_map(value: &dyn PartialReflect) -> bool {\n    value.reflect_kind() == ReflectKind::Map\n}","tryCatchPattern":"use bevy_reflect::ReflectKindMismatchError;\n\nmatch value.reflect_ref().into_map() {\n    Ok(map) => { /* map logic */ }\n    Err(ReflectKindMismatchError { expected, received }) => {\n        warn!(\"kind mismatch: expected {expected:?}, received {received:?}\");\n    }\n}","preventionTips":["Branch on reflect_kind()/ReflectRef before kind casts.","Use try_apply instead of apply so kind mismatches surface as ApplyError you can handle."],"tags":["rust","bevy","bevy-reflect","reflectkind","kind-mismatch","reflect-ref"],"backgroundTag":"reflection-kind-mismatch","analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}