rust-lang/rust · error · bridge::Error
Item kind `{:?}` cannot be converted
Error message
Item kind `{:?}` cannot be converted What it means
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.
Source
Thrown at compiler/rustc_public/src/mir/mono.rs:237
Ok(context.mono_instance(def_id))
} else {
Err(bridge::Error::new("Item requires monomorphization".to_string()))
}
})
}
}
/// Try to convert an instance into a crate item.
/// Only user defined instances can be converted.
impl TryFrom<Instance> for CrateItem {
type Error = crate::Error;
fn try_from(value: Instance) -> Result<Self, Self::Error> {
with(|context| {
if value.kind == InstanceKind::Item && context.has_body(value.def.def_id()) {
Ok(CrateItem(context.instance_def_id(value.def)))
} else {
Err(bridge::Error::new(format!("Item kind `{:?}` cannot be converted", value.kind)))
}
})
}
}
impl From<Instance> for MonoItem {
fn from(value: Instance) -> Self {
MonoItem::Fn(value)
}
}
impl From<StaticDef> for MonoItem {
fn from(value: StaticDef) -> Self {
MonoItem::Static(value)
}
}
impl From<StaticDef> for CrateItem {View on GitHub (pinned to 22057b88b0)
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.
Example fix
// before
let item: CrateItem = instance.into();
// after
let item = if instance.kind == InstanceKind::Item
&& context.has_body(instance.def.def_id())
{
CrateItem::try_from(instance)?
} else {
return Ok(None);
}; Defensive patterns
Strategy: type-guard
Validate before calling
// Before CrateItem::try_from(instance):
if instance.kind != InstanceKind::Item || !instance.has_body() {
return Err(format!("instance {:?} is not a user-defined item", instance));
}
let item = CrateItem::try_from(instance)?; Type guard
// Narrows an Instance to a convertible user-defined item.
// Only InstanceKind::Item with an available body round-trips to CrateItem.
fn is_user_item_instance(inst: &Instance) -> bool {
inst.kind == InstanceKind::Item && inst.has_body()
} Try / catch
match CrateItem::try_from(instance) {
Ok(item) => item,
Err(e) if e.to_string().contains("cannot be converted") => {
// Shim / Intrinsic / Virtual / bodyless: keep the Instance, do not
// attempt CrateItem conversion.
return handle_non_item(instance);
}
Err(e) => return Err(e.into()),
} Prevention
- 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`.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Expected a static item, but found: {value:?}
- Item requires monomorphization
- {self:?}
- Const `{cnst:?}` cannot be encoded as u64
- first index in inbounds_gep
AI-assisted analysis of rust-lang/rust@22057b88b0 (2026-08-03).
Data as JSON: /data/errors/c8c6e1cc85717210.json.
Report an issue: GitHub.