{"id":"0323c5fe8d6f084f","repo":"rust-lang/rust","slug":"arg-must-exist-for-infer","errorCode":null,"errorMessage":"arg must exist for infer","messagePattern":"arg must exist for infer","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/rustc_ast_lowering/src/delegation/mod.rs","lineNumber":492,"sourceCode":"    fn process_segment(\n        &mut self,\n        span: Span,\n        segment: &hir::PathSegment<'hir>,\n        result: &mut GenericsGenerationResult<'hir>,\n    ) -> hir::PathSegment<'hir> {\n        let infer_indices = result.generics.infer_indices();\n        result.generics.into_hir_generics(self, span);\n\n        let mut segment = segment.clone();\n        let mut args_iter = result.generics.create_args_iterator();\n\n        let new_args = segment\n            .args\n            .filter(|args| !args.is_empty())\n            .map(|args| {\n                self.arena.alloc_from_iter(args.args.iter().enumerate().map(|(idx, arg)| {\n                    if infer_indices.contains(&idx) {\n                        args_iter.next(self, |_| arg.hir_id()).expect(\"arg must exist for infer\")\n                    } else {\n                        *arg\n                    }\n                }))\n            })\n            .unwrap_or_else(|| self.arena.alloc_from_iter(args_iter.consume_all(self)));\n\n        // Do not omit constraints as there might be some and they must be present in HIR (#158812).\n        let has_constraints = segment.args.is_some_and(|a| !a.constraints.is_empty());\n\n        // Needed for better error messages (`trait-impl-wrong-args-count.rs` test).\n        segment.args = (has_constraints || !new_args.is_empty()).then(|| {\n            &*self.arena.alloc(hir::GenericArgs {\n                args: new_args,\n                constraints: segment.args.map(|a| a.constraints).unwrap_or(&[]),\n                parenthesized: hir::GenericArgsParentheses::No,\n                span_ext: segment.args.map_or(span, |args| args.span_ext),\n            })","sourceCodeStart":474,"sourceCodeEnd":510,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_ast_lowering/src/delegation/mod.rs#L474-L510","documentation":"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`.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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.","Rewrite the delegation to fully specify or fully elide the generic args (avoid mixing explicit `_` with concrete types on the delegated path).","Upgrade to a newer nightly; the delegation generics logic changes almost weekly.","If developing rustc: reconcile `infer_indices` construction with `create_args_iterator` length in the `generics` builder feeding `process_segment`."],"exampleFix":"// before — mixing explicit and inferred generic args on a delegated path\ndelegate f::<_, i32> to g; // infer_indices count != args_iter length\n\n// after — fully elide or fully specify\ndelegate f to g;        // all inferred\ndelegate f::<u8, i32> to g; // all explicit","handlingStrategy":"validation","validationCode":"// Generic-argument inference during delegation needs an arg slot to exist.\n// Validate by passing explicit type arguments instead of deferring to infer:\n//\n// Risky (relies on inference): pub delegate::to(Generic::method);\n// Safe (explicit args):        pub delegate::to::<U>(Generic::method);\n//\n// Pre-flight: ensure every delegated generic fn has a monomorphizable\n// signature you can name concretely; if you cannot, do not delegate it.\nfn delegation_target_has_nameable_args() -> bool { true /* audit by hand */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass explicit generic arguments (`delegate::to::<T>(..)`) to delegated functions","Avoid delegating to functions whose signatures depend on inference the lowering stage cannot resolve","Keep delegation targets to non-generic or fully-monomorphized callees","Treat a hit here as an ICE: reduce and report, do not paper over it"],"tags":["rustc","ast-lowering","delegation","generics","ice"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}