rust-lang/rust · error · bridge::Error
Item kind ` ` cannot be converted
Error message
Item kind `{:?}` cannot be converted What it means
Thrown by `impl TryFrom<Instance> for CrateItem` (mono.rs:234) when converting an `Instance` back into a `CrateItem`. Only `InstanceKind::Item` instances that also have a body convert; intrinsics, virtual functions, LLVM intrinsics, and compiler shims are rejected because they do not map to a user-defined crate item.
Solutions
- Guard on `instance.kind == InstanceKind::Item` (and `instance.has_body()`) before converting.
- For intrinsics, use `instance.intrinsic_name()` and handle the intrinsic path separately.
- Skip shims/virtuals when your consumer only deals with source-level items.
Example fix
// before
let item: CrateItem = instance.try_into()?;
// after
let item: CrateItem = match instance.kind {
InstanceKind::Item if instance.has_body() => instance.try_into()?,
_ => continue, // skip non-convertible instances
}; Defensive patterns
Strategy: type-guard
Type guard
fn is_user_item_with_body(inst: &Instance) -> bool {
matches!(inst.kind, InstanceKind::Item) && inst.has_body()
} Try / catch
match CrateItem::try_from(instance) {
Ok(item) => item,
Err(_) => {
// intrinsic/shim/virtual: handle separately or skip
continue;
}
} Prevention
- Check InstanceKind before converting; only Item is convertible.
- Use has_body() to also rule out bodyless item instances.
When it happens
Trigger: Calling `CrateItem::try_from(instance)` / `instance.try_into()` on an instance whose `kind` is `Intrinsic`, `LlvmIntrinsic`, `Virtual`, or `Shim`, or an `Item` instance that has no MIR body.
Common situations: Tooling that iterates `MonoItem`s and converts every instance to a `CrateItem`; handling intrinsic fallback bodies; converting vtable-resolved virtual instances obtained from a dispatch table.
Related errors
- Failed to resolve ` ` with
- Expected a static item, but found
- Item requires monomorphization
- {self:?}
- Const ` ` cannot be encoded as u64
AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10).
Data as JSON: /api/errors/c8c6e1cc85717210.
Report an issue: GitHub.
Appendix: 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 7088e4b63a)