{"id":"c8ace451f9978498","repo":"rust-lang/rust","slug":"self-generic-param-is-not-found-while-expected","errorCode":null,"errorMessage":"`Self` generic param is not found while expected","messagePattern":"`Self` generic param is not found while expected","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_ast_lowering/src/delegation/generics.rs","lineNumber":219,"sourceCode":"\n    pub(super) fn is_trait_impl(&self) -> bool {\n        match self {\n            HirOrTyGenerics::Ty(ty) => ty.trait_impl,\n            HirOrTyGenerics::Hir(hir) => hir.trait_impl,\n        }\n    }\n\n    pub(super) fn find_self_param(&self) -> &'hir hir::GenericParam<'hir> {\n        match self {\n            HirOrTyGenerics::Ty(_) => {\n                bug!(\"accessed ty-level generics while searching for uplifted `Self` param\")\n            }\n            HirOrTyGenerics::Hir(hir) => hir\n                .data\n                .params\n                .iter()\n                .find(|p| p.name.ident().name == kw::SelfUpper)\n                .expect(\"`Self` generic param is not found while expected\"),\n        }\n    }\n\n    pub(crate) fn pos(&self) -> GenericsPosition {\n        match self {\n            HirOrTyGenerics::Ty(ty) => ty.pos,\n            HirOrTyGenerics::Hir(hir) => hir.pos,\n        }\n    }\n}\n\nimpl<'hir> GenericsGenerationResult<'hir> {\n    fn new(generics: DelegationGenerics<TyGenerics<'hir>>) -> GenericsGenerationResult<'hir> {\n        GenericsGenerationResult {\n            generics: HirOrTyGenerics::Ty(generics),\n            args_segment_id: HirId::INVALID,\n            use_for_sig_inheritance: false,\n        }","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_ast_lowering/src/delegation/generics.rs#L201-L237","documentation":"expect panic in delegation generics (find_self_param) when searching HIR generics for a parameter named Self. The delegation-feature reuse path that propagates a `Self` self-type assumes the parent's HIR generics contain a `Self` param; if none is found, this expect fires. It indicates a mismatch between the self-propagation analysis that requested a SelfParam and the actual HIR generics of the delegation's parent.","triggerScenarios":"Triggered only with the unstable `delegation` feature (reuse Trait::method as ...) in free-to-trait delegation paths where SelfParam propagation is computed as needed, but the parent generics (e.g. an inherent impl or a free function) contain no Self parameter. Reachable from malformed delegation macros or compiler regressions in the self-propagation logic.","commonSituations":"Using #![feature(delegation)] on nightly with a `reuse` on a path where the parent has no Self (free functions, certain inherent impls); proc-macro-generated delegation code; nightly regressions in rustc_ast_lowering/delegation. Not reachable on stable.","solutions":["Report the ICE to rust-lang/rust — include the delegation snippet and the nightly hash, and tag it with the `delegation` feature.","Rewrite the `reuse` to avoid free-to-trait propagation that the compiler cannot resolve (e.g. delegate from an inherent impl rather than a free function).","Disable the unstable `delegation` feature and use explicit wrapper methods until the feature stabilizes.","Bisect to identify the regression in the delegation self-propagation analysis."],"exampleFix":"// before (free-to-trait reuse triggering Self propagation that cannot be satisfied)\n#![feature(delegation)]\nfn outer() { reuse Trait::method; }\n// after — delegate from a context that actually has Self\n#![feature(delegation)]\nimpl Trait for T {\n    reuse Trait::method;\n}","handlingStrategy":"validation","validationCode":"// find_self_param expects an uplifted `Self` generic param in the HIR\n// generics. It is reached for delegations that need Self propagation in a\n// context where the impl/inherent item has no Self param. Validate first:\nfn has_self_param(generics: &hir::Generics<'_>) -> bool {\n    generics.params.iter().any(|p| p.name.ident().name == rustc_span::symbol::kw::SelfUpper)\n}\n// For a delegation that requires Self, ensure the receiver is inside an\n// impl block whose HIR generics were built with the Self param:\nif needs_self && !has_self_param(&hir_generics) {\n    return Err(\"delegation requires `Self` but the enclosing item has no Self generic param\");\n}","typeGuard":"fn delegation_can_use_self(generics: &hir::Generics<'_>) -> bool {\n    generics.params.iter().any(|p| p.name.ident().name == rustc_span::symbol::kw::SelfUpper)\n}","tryCatchPattern":"let self_param = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| generics.parent.generics.find_self_param()));\nmatch self_param {\n    Ok(p) => p,\n    Err(_) => return Err(\"cannot resolve `Self` for this delegation; not inside a supported impl context\"),\n}","preventionTips":["Only use `delegate` to a receiver that references `Self` when the delegation target is inside a trait impl or inherent impl whose generics actually carry the uplifted Self param — not in free functions or associated fns where Self is unavailable.","Avoid mixing delegation with Self-propagation in contexts where the generics were built at Ty level (HirOrTyGenerics::Ty) — find_self_param explicitly bugs out on those.","Keep delegation features aligned with the nightly compiler version that supports your exact impl shape; Self propagation in delegation is feature-gated and shapes change.","If you hit this in tooling, fall back to monomorphizing the delegated call manually (self.method()) instead of using `delegate`."],"tags":["rustc","ice","delegation","generics","self-type","nightly"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}