{"id":"c8c6e1cc85717210","repo":"rust-lang/rust","slug":"item-kind-cannot-be-converted","errorCode":null,"errorMessage":"Item kind `{:?}` cannot be converted","messagePattern":"Item kind `(.+?)` cannot be converted","errorType":"exception","errorClass":"bridge::Error","httpStatus":null,"severity":"error","filePath":"compiler/rustc_public/src/mir/mono.rs","lineNumber":237,"sourceCode":"                Ok(context.mono_instance(def_id))\n            } else {\n                Err(bridge::Error::new(\"Item requires monomorphization\".to_string()))\n            }\n        })\n    }\n}\n\n/// Try to convert an instance into a crate item.\n/// Only user defined instances can be converted.\nimpl TryFrom<Instance> for CrateItem {\n    type Error = crate::Error;\n\n    fn try_from(value: Instance) -> Result<Self, Self::Error> {\n        with(|context| {\n            if value.kind == InstanceKind::Item && context.has_body(value.def.def_id()) {\n                Ok(CrateItem(context.instance_def_id(value.def)))\n            } else {\n                Err(bridge::Error::new(format!(\"Item kind `{:?}` cannot be converted\", value.kind)))\n            }\n        })\n    }\n}\n\nimpl From<Instance> for MonoItem {\n    fn from(value: Instance) -> Self {\n        MonoItem::Fn(value)\n    }\n}\n\nimpl From<StaticDef> for MonoItem {\n    fn from(value: StaticDef) -> Self {\n        MonoItem::Static(value)\n    }\n}\n\nimpl From<StaticDef> for CrateItem {","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_public/src/mir/mono.rs#L219-L255","documentation":"Thrown by `TryFrom<Instance> for CrateItem` in compiler/rustc_public/src/mir/mono.rs:237 when the instance is not a user-defined `InstanceKind::Item`, or when it has no available body. Only plain `Item` instances whose body is present (`has_body` is true) round-trip back to a `CrateItem`; shims, vtables, intrinsics, closures, and fn-pointer shims do not. rustc_public enforces this because a `CrateItem` must identify a real source-level definition.","triggerScenarios":"Converting an `Instance` produced from a trait-method vtable, an intrinsic, a `FnPtrShim`, a `Virtual`/`ClosureOnceShim`, or any other non-`Item` kind via `CrateItem::try_from(instance)`. Calling `.into()` on an instance without checking `instance.kind`. Converting an instance whose DefId has no MIR body available.","commonSituations":"Whole-crate mono-item walkers that try to reflect every instance back to a source item. Tooling that assumes all instances come from user code rather than compiler-generated shims. Version upgrades that introduced new `InstanceKind` variants.","solutions":["Guard the conversion: only call `CrateItem::try_from` when `value.kind == InstanceKind::Item && context.has_body(value.def.def_id())`.","When iterating `MonoItem`s, handle non-`Item` instances separately rather than uniformly converting them.","Treat the `Err` as a signal to skip the instance instead of propagating the error."],"exampleFix":"// before\nlet item: CrateItem = instance.into();\n// after\nlet item = if instance.kind == InstanceKind::Item\n    && context.has_body(instance.def.def_id())\n{\n    CrateItem::try_from(instance)?\n} else {\n    return Ok(None);\n};","handlingStrategy":"type-guard","validationCode":"// Before CrateItem::try_from(instance):\nif instance.kind != InstanceKind::Item || !instance.has_body() {\n    return Err(format!(\"instance {:?} is not a user-defined item\", instance));\n}\nlet item = CrateItem::try_from(instance)?;","typeGuard":"// Narrows an Instance to a convertible user-defined item.\n// Only InstanceKind::Item with an available body round-trips to CrateItem.\nfn is_user_item_instance(inst: &Instance) -> bool {\n    inst.kind == InstanceKind::Item && inst.has_body()\n}","tryCatchPattern":"match CrateItem::try_from(instance) {\n    Ok(item) => item,\n    Err(e) if e.to_string().contains(\"cannot be converted\") => {\n        // Shim / Intrinsic / Virtual / bodyless: keep the Instance, do not\n        // attempt CrateItem conversion.\n        return handle_non_item(instance);\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Only `InstanceKind::Item` instances convert back to `CrateItem`; shims, intrinsics, and virtual-call instances do not — branch on `instance.kind` first.","Pair the kind check with `instance.has_body()`: foreign items and builtins lack a body and will also fail conversion.","When iterating over monomorphization items, keep the original `Instance` around as the fallback identity instead of forcing a `CrateItem`."],"tags":["rust","mir","instance","conversion"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}