rust-lang/rust · error · bridge::Error

Item requires monomorphization

Error message

Item requires monomorphization

What it means

Thrown by `impl TryFrom<CrateItem> for Instance` (mono.rs:218) when a crate item is converted to an `Instance` but the item requires monomorphization (i.e. it is generic). A non-generic item can become a single concrete instance; a generic one cannot without explicit type arguments.

Solutions

  1. For generic items, use `Instance::resolve(fn_def, &generic_args)` with concrete args instead of `TryFrom`.
  2. Before converting, call the item's generic-count check / inspect `generics_of` and branch to `resolve` when parameters exist.
  3. If you truly need a non-generic instance, ensure the source item has no type parameters.

Example fix

// before
let inst: Instance = crate_item.try_into()?;

// after
let def = FnDef::try_from(crate_item)?;
let args = GenericArgs::empty_for(&def); // or concrete type args
let inst = Instance::resolve(def, &args)?;
Defensive patterns

Strategy: validation

Validate before calling

// Check whether the item is generic before attempting TryFrom conversion.
// requires_monomorphization is exposed via the context; if unavailable, inspect generics_of.
if item_has_type_params(crate_item) {
    // must resolve with explicit args, cannot use TryFrom
    return Instance::resolve(fn_def, &concrete_args);
}
Instance::try_from(crate_item)

Type guard

fn is_non_generic_item(item: &CrateItem) -> bool {
    // heuristic: an item convertible to a mono instance has no type params
    !item.requires_monomorphization()
}

Prevention

When it happens

Trigger: Calling `Instance::try_from(crate_item)` / `crate_item.try_into()` on a `CrateItem` whose definition has type parameters, so `context.requires_monomorphization(def_id)` returns true.

Common situations: Iterating over `crate_items()` and blindly converting each to an `Instance`; handling a function that was just made generic during a refactor; version changes that add a generic parameter to a previously concrete item.

Related errors


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/306f4b828da0d2db. Report an issue: GitHub.

Appendix: source

Thrown at compiler/rustc_public/src/mir/mono.rs:221

            .field("kind", &self.kind)
            .field("def", &self.mangled_name())
            .field("args", &self.args())
            .finish()
    }
}

/// Try to convert a crate item into an instance.
/// The item cannot be generic in order to be converted into an instance.
impl TryFrom<CrateItem> for Instance {
    type Error = crate::Error;

    fn try_from(item: CrateItem) -> Result<Self, Self::Error> {
        with(|context| {
            let def_id = item.def_id();
            if !context.requires_monomorphization(def_id) {
                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)))
            }
        })

View on GitHub (pinned to 7088e4b63a)