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

Failed to resolve `{def:?}` with `{args:?}`

Error message

Failed to resolve `{def:?}` with `{args:?}`

What it means

Error returned by `Instance::resolve` in the `rustc_public` stable compiler API when the underlying `resolve_instance` query returns `None` for the given `FnDef` and `GenericArgs`. It means no monomorphized instance could be produced — typically because the supplied generic arguments do not satisfy the function's where-clause/trait bounds, the def is not monomorphizable in this context, or required inference/normalization had not completed.

Source

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

    /// The plain name does not include type arguments (as `trimmed_name` does),
    /// which is more convenient to match with intrinsic symbols.
    pub fn intrinsic_name(&self) -> Option<Symbol> {
        match self.kind {
            InstanceKind::Intrinsic => {
                Some(with(|context| context.intrinsic(self.def.def_id()).unwrap().fn_name()))
            }
            InstanceKind::LlvmIntrinsic
            | InstanceKind::Item
            | InstanceKind::Virtual { .. }
            | InstanceKind::Shim => None,
        }
    }

    /// Resolve an instance starting from a function definition and generic arguments.
    pub fn resolve(def: FnDef, args: &GenericArgs) -> Result<Instance, Error> {
        with(|context| {
            context.resolve_instance(def, args).ok_or_else(|| {
                bridge::Error::new(format!("Failed to resolve `{def:?}` with `{args:?}`"))
            })
        })
    }

    /// Resolve the drop in place for a given type.
    pub fn resolve_drop_in_place(ty: Ty) -> Instance {
        with(|cx| cx.resolve_drop_in_place(ty))
    }

    /// Resolve an instance for a given function pointer.
    pub fn resolve_for_fn_ptr(def: FnDef, args: &GenericArgs) -> Result<Instance, Error> {
        with(|context| {
            context.resolve_for_fn_ptr(def, args).ok_or_else(|| {
                bridge::Error::new(format!("Failed to resolve `{def:?}` with `{args:?}`"))
            })
        })
    }

View on GitHub (pinned to 22057b88b0)

Solutions

  1. Verify the `GenericArgs` match the `FnDef`'s generic parameter count and kinds (types, lifetimes, const)
  2. Check that every trait/where-clause bound on the function is satisfied by the supplied argument types
  3. Confirm the `FnDef` points to a body-having, non-extern function
  4. If using a custom driver, ensure inference and projection normalization are complete before calling `resolve`

Example fix

// before: wrong number/type of generic args -> resolve fails
let inst = Instance::resolve(fn_def, &generic_args)?;
// after: build args matching the FnDef's own generics
let args = fn_def.args(); // or GenericArgs::new_for(...) matching param kinds
let inst = Instance::resolve(fn_def, &args)?;
Defensive patterns

Strategy: validation

Validate before calling

use rustc_middle::ty::{DefId, GenericArgsRef};
fn mono_def_is_resolvable(def: DefId, args: GenericArgsRef<'_>, tcx: TyCtxt<'_>) -> bool {
    let generics = tcx.generics_of(def);
    args.len() == generics.count() && !tcx.is_foreign_item(def)
}
if !mono_def_is_resolvable(def, args, tcx) {
    return Err(format!("unresolvable mono def {:?} args {:?}", def, args));
}

Type guard

pub fn args_arity_matches(tcx: TyCtxt<'_>, def: DefId, args: GenericArgsRef<'_>) -> bool {
    tcx.generics_of(def).count() == args.len()
}

Try / catch

use std::panic;
match panic::catch_unwind(panic::AssertUnwindSafe(|| mir::mono::resolve(def, args))) {
    Ok(Some(instance)) => instance,
    _ => /* resolution failed; skip this mono item */ continue,
}

Prevention

When it happens

Trigger: A tool or driver built on `rustc_public` (e.g. an analyzer, Kani, or a custom compiler driver) calls `Instance::resolve(fn_def, &generic_args)` and the query yields no instance.

Common situations: Passing `GenericArgs` whose count/kinds don't match the `FnDef`'s parameters; arguments that violate a trait bound or where-clause; resolving a function whose body is unavailable (extern/FFI declaration); calling resolve before type inference and projection normalization finished.

Related errors


AI-assisted analysis of rust-lang/rust@22057b88b0 (2026-08-03). Data as JSON: /data/errors/71eef5008d4d46ab.json. Report an issue: GitHub.