{"record":{"id":"71eef5008d4d46ab","repo":"rust-lang/rust","slug":"failed-to-resolve-def-with-args","errorCode":null,"errorMessage":"Failed to resolve `{def:?}` with `{args:?}`","messagePattern":"Failed to resolve `(.+?)` with `(.+?)`","errorType":"exception","errorClass":"bridge::Error","httpStatus":null,"severity":"error","filePath":"compiler/rustc_public/src/mir/mono.rs","lineNumber":129,"sourceCode":"    /// The plain name does not include type arguments (as `trimmed_name` does),\n    /// which is more convenient to match with intrinsic symbols.\n    pub fn intrinsic_name(&self) -> Option<Symbol> {\n        match self.kind {\n            InstanceKind::Intrinsic => {\n                Some(with(|context| context.intrinsic(self.def.def_id()).unwrap().fn_name()))\n            }\n            InstanceKind::LlvmIntrinsic\n            | InstanceKind::Item\n            | InstanceKind::Virtual { .. }\n            | InstanceKind::Shim => None,\n        }\n    }\n\n    /// Resolve an instance starting from a function definition and generic arguments.\n    pub fn resolve(def: FnDef, args: &GenericArgs) -> Result<Instance, Error> {\n        with(|context| {\n            context.resolve_instance(def, args).ok_or_else(|| {\n                bridge::Error::new(format!(\"Failed to resolve `{def:?}` with `{args:?}`\"))\n            })\n        })\n    }\n\n    /// Resolve the drop in place for a given type.\n    pub fn resolve_drop_in_place(ty: Ty) -> Instance {\n        with(|cx| cx.resolve_drop_in_place(ty))\n    }\n\n    /// Resolve an instance for a given function pointer.\n    pub fn resolve_for_fn_ptr(def: FnDef, args: &GenericArgs) -> Result<Instance, Error> {\n        with(|context| {\n            context.resolve_for_fn_ptr(def, args).ok_or_else(|| {\n                bridge::Error::new(format!(\"Failed to resolve `{def:?}` with `{args:?}`\"))\n            })\n        })\n    }\n","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_public/src/mir/mono.rs#L111-L147","documentation":"Returned by Instance::resolve in rustc_public::mir::mono when the compiler's internal resolve_instance query returns None for the given FnDef and GenericArgs. This means no valid monomorphized instance could be constructed — the generic arguments don't satisfy the function's generic parameter requirements, or the def/args combination is otherwise invalid. rustc_public is the stable IR API for compiler consumers (custom codegen backends, analysis tools, etc.).","triggerScenarios":"A rustc_public consumer calls Instance::resolve(def, args) where def is a FnDef and args is a GenericArgs that doesn't match the function's generic signature — wrong number of type/lifetime/const parameters, args that violate trait bounds, or a def that isn't resolvable in the current context.","commonSituations":"Custom codegen backend or analysis tool constructing GenericArgs manually with incorrect arity; using a FnDef from one crate with args shaped for another; passing args that include types not meeting the function's where-clauses; stale def_id after incremental recompilation invalidated it.","solutions":["Verify that the GenericArgs match the FnDef's generic parameter count and kinds (lifetimes, types, consts).","Use Instance::args() on a known-good instance to inspect the expected args shape, then compare with what you're passing.","Prefer using the compiler's own monomorphization queries (e.g., collecting MonoItems from the mono-item collector) rather than constructing instances manually.","Check that the FnDef was obtained from the same compilation context (same TyCtxt) as the args — cross-context def IDs are invalid.","Handle the Result::Err gracefully — if you're iterating over potentially-unresolvable defs, filter them rather than failing."],"exampleFix":"// before: manual args construction with wrong arity\nlet args = GenericArgs::new(&[/* only lifetime, missing type arg */]);\nlet instance = Instance::resolve(def, &args)?; // fails\n// after: obtain args from the def's own generic parameters\nlet correct_args = with(|cx| cx.instance_args(some_valid_def));\n// or iterate mono items instead of constructing instances manually\nfor item in tcx.collect_mono_items() { /* use pre-resolved instances */ }","handlingStrategy":"try-catch","validationCode":"// Before calling Instance::resolve, validate that the args match the def's generics.\nuse rustc_public::mir::mono::Instance;\nuse rustc_public::ty::{FnDef, GenericArgs};\n\nfn try_resolve_safe(def: FnDef, args: &GenericArgs) -> Option<Instance> {\n    Instance::resolve(def, args).ok()\n    // Returns None if resolution fails, instead of propagating the error.\n}","typeGuard":null,"tryCatchPattern":"// Instance::resolve returns Result<Instance, Error>. Handle gracefully:\nmatch Instance::resolve(def, &args) {\n    Ok(instance) => {\n        // use instance\n    }\n    Err(e) => {\n        // log and skip — don't treat as fatal unless all instances must resolve\n        eprintln!(\"warning: could not resolve instance: {e}\");\n        continue;\n    }\n}","preventionTips":["Don't construct GenericArgs manually — obtain them from the compiler's queries (e.g., instance_args).","Ensure FnDef and GenericArgs come from the same TyCtxt / compilation context.","Always handle the Result::Err case rather than unwrapping.","Prefer iterating pre-resolved MonoItems from the monomorphization collector."],"tags":["rustc-public","mir","monomorphization","instance","generic-args"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}