rust-lang/rust · critical

`Self` generic param is not found while expected

Error message

`Self` generic param is not found while expected

What it means

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.

Source

Thrown at compiler/rustc_ast_lowering/src/delegation/generics.rs:219

    pub(super) fn is_trait_impl(&self) -> bool {
        match self {
            HirOrTyGenerics::Ty(ty) => ty.trait_impl,
            HirOrTyGenerics::Hir(hir) => hir.trait_impl,
        }
    }

    pub(super) fn find_self_param(&self) -> &'hir hir::GenericParam<'hir> {
        match self {
            HirOrTyGenerics::Ty(_) => {
                bug!("accessed ty-level generics while searching for uplifted `Self` param")
            }
            HirOrTyGenerics::Hir(hir) => hir
                .data
                .params
                .iter()
                .find(|p| p.name.ident().name == kw::SelfUpper)
                .expect("`Self` generic param is not found while expected"),
        }
    }

    pub(crate) fn pos(&self) -> GenericsPosition {
        match self {
            HirOrTyGenerics::Ty(ty) => ty.pos,
            HirOrTyGenerics::Hir(hir) => hir.pos,
        }
    }
}

impl<'hir> GenericsGenerationResult<'hir> {
    fn new(generics: DelegationGenerics<TyGenerics<'hir>>) -> GenericsGenerationResult<'hir> {
        GenericsGenerationResult {
            generics: HirOrTyGenerics::Ty(generics),
            args_segment_id: HirId::INVALID,
            use_for_sig_inheritance: false,
        }

View on GitHub (pinned to 22057b88b0)

Solutions

  1. Report the ICE to rust-lang/rust — include the delegation snippet and the nightly hash, and tag it with the `delegation` feature.
  2. 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).
  3. Disable the unstable `delegation` feature and use explicit wrapper methods until the feature stabilizes.
  4. Bisect to identify the regression in the delegation self-propagation analysis.

Example fix

// before (free-to-trait reuse triggering Self propagation that cannot be satisfied)
#![feature(delegation)]
fn outer() { reuse Trait::method; }
// after — delegate from a context that actually has Self
#![feature(delegation)]
impl Trait for T {
    reuse Trait::method;
}
Defensive patterns

Strategy: validation

Validate before calling

// find_self_param expects an uplifted `Self` generic param in the HIR
// generics. It is reached for delegations that need Self propagation in a
// context where the impl/inherent item has no Self param. Validate first:
fn has_self_param(generics: &hir::Generics<'_>) -> bool {
    generics.params.iter().any(|p| p.name.ident().name == rustc_span::symbol::kw::SelfUpper)
}
// For a delegation that requires Self, ensure the receiver is inside an
// impl block whose HIR generics were built with the Self param:
if needs_self && !has_self_param(&hir_generics) {
    return Err("delegation requires `Self` but the enclosing item has no Self generic param");
}

Type guard

fn delegation_can_use_self(generics: &hir::Generics<'_>) -> bool {
    generics.params.iter().any(|p| p.name.ident().name == rustc_span::symbol::kw::SelfUpper)
}

Try / catch

let self_param = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| generics.parent.generics.find_self_param()));
match self_param {
    Ok(p) => p,
    Err(_) => return Err("cannot resolve `Self` for this delegation; not inside a supported impl context"),
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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