rust-lang/rust · error

arg must exist for infer

Error message

arg must exist for infer

What it means

This `expect` fires inside `process_segment` of the `rustc_ast_lowering` delegation dir when an argument index is a member of `infer_indices` (positions that should be filled by inference rather than copied from the source args) but the lazily-built `args_iter` has no next value to produce. The two collections — `infer_indices` and `create_args_iterator` — must agree on count; a mismatch means the generic-args bookkeeping for the delegated item is inconsistent. It is an internal compiler error surfaced through `.expect`.

Source

Thrown at compiler/rustc_ast_lowering/src/delegation/mod.rs:492

    fn process_segment(
        &mut self,
        span: Span,
        segment: &hir::PathSegment<'hir>,
        result: &mut GenericsGenerationResult<'hir>,
    ) -> hir::PathSegment<'hir> {
        let infer_indices = result.generics.infer_indices();
        result.generics.into_hir_generics(self, span);

        let mut segment = segment.clone();
        let mut args_iter = result.generics.create_args_iterator();

        let new_args = segment
            .args
            .filter(|args| !args.is_empty())
            .map(|args| {
                self.arena.alloc_from_iter(args.args.iter().enumerate().map(|(idx, arg)| {
                    if infer_indices.contains(&idx) {
                        args_iter.next(self, |_| arg.hir_id()).expect("arg must exist for infer")
                    } else {
                        *arg
                    }
                }))
            })
            .unwrap_or_else(|| self.arena.alloc_from_iter(args_iter.consume_all(self)));

        // Do not omit constraints as there might be some and they must be present in HIR (#158812).
        let has_constraints = segment.args.is_some_and(|a| !a.constraints.is_empty());

        // Needed for better error messages (`trait-impl-wrong-args-count.rs` test).
        segment.args = (has_constraints || !new_args.is_empty()).then(|| {
            &*self.arena.alloc(hir::GenericArgs {
                args: new_args,
                constraints: segment.args.map(|a| a.constraints).unwrap_or(&[]),
                parenthesized: hir::GenericArgsParentheses::No,
                span_ext: segment.args.map_or(span, |args| args.span_ext),
            })

View on GitHub (pinned to 22057b88b0)

Solutions

  1. Report the ICE with a minimal delegating-to-generic-function reproducer to https://github.com/rust-lang/rust/issues — this is a compiler invariant violation.
  2. Rewrite the delegation to fully specify or fully elide the generic args (avoid mixing explicit `_` with concrete types on the delegated path).
  3. Upgrade to a newer nightly; the delegation generics logic changes almost weekly.
  4. If developing rustc: reconcile `infer_indices` construction with `create_args_iterator` length in the `generics` builder feeding `process_segment`.

Example fix

// before — mixing explicit and inferred generic args on a delegated path
delegate f::<_, i32> to g; // infer_indices count != args_iter length

// after — fully elide or fully specify
delegate f to g;        // all inferred
delegate f::<u8, i32> to g; // all explicit
Defensive patterns

Strategy: validation

Validate before calling

// Generic-argument inference during delegation needs an arg slot to exist.
// Validate by passing explicit type arguments instead of deferring to infer:
//
// Risky (relies on inference): pub delegate::to(Generic::method);
// Safe (explicit args):        pub delegate::to::<U>(Generic::method);
//
// Pre-flight: ensure every delegated generic fn has a monomorphizable
// signature you can name concretely; if you cannot, do not delegate it.
fn delegation_target_has_nameable_args() -> bool { true /* audit by hand */ }

Prevention

When it happens

Trigger: Triggered while lowering a delegation item (`#![feature(fn_delegation)]`) whose callee path carries generic args, where `generics.infer_indices()` reports more inferable positions than `create_args_iterator` can yield. Concretely: `segment.args` is non-empty, the loop at line 490 hits an `idx` contained in `infer_indices`, and `args_iter.next(...)` returns `None` at line 492.

Common situations: Hit by rustc developers changing how delegation generics are inferred (the dir is under active development with FIXME notes), or by nightly users delegating to generic functions with partially-elided generic args like `delegate foo::<_, i32> to bar;`. The infer-index set and the args iterator fall out of sync when generic counts mismatch.

Related errors


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