rust-lang/rust · error

arg must exist for infer

Error message

arg must exist for infer

What it means

delegation/mod.rs:500: while rewriting generic args, for each index contained in result.generics.infer_indices(), the code pulls the next synthesized arg from args_iter via .next(...).expect("arg must exist for infer"). The invariant: the number of infer indices never exceeds the number of args the iterator can produce.

Solutions

  1. File a rustc ICE with the minimized delegation generic-args repro.
  2. Provide explicit generic args on the delegation path so fewer slots are inferred.
  3. Remove angle-bracketed args / constraints on the delegation to reduce resolution complexity.
  4. Bisect with cargo bisect-rustc across nightlies.
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Delegation generic resolution computes infer_indices whose count is greater than the args the create_args_iterator yields, e.g. a mismatch between inferred arg slots and available generic parameters.

Common situations: Unstable delegation with partially-inferred generic args; a mismatch in the delegation generics-resolution accounting.

Related errors


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/0323c5fe8d6f084f. Report an issue: GitHub.

Appendix: source

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

    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 7088e4b63a)