{"id":"c8ca520080bd621c","repo":"rust-lang/rust","slug":"this-method-is-not-called-in-free-functions-as-pa","errorCode":null,"errorMessage":"This method is not called in free functions, as patterns are always allowed there","messagePattern":"This method is not called in free functions, as patterns are always allowed there","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_parse/src/parser/diagnostics.rs","lineNumber":2400,"sourceCode":"            return if self.token == token::Lt { None } else { Some(ident) };\n        }\n        None\n    }\n\n    #[cold]\n    pub(super) fn recover_arg_parse(\n        &mut self,\n        context: FnContext,\n    ) -> PResult<'a, (Box<ast::Pat>, Box<ast::Ty>)> {\n        let pat = self.parse_pat_no_top_alt(Some(Expected::ArgumentName), None)?;\n        self.expect(exp!(Colon))?;\n        let ty = self.parse_ty()?;\n        self.dcx().emit_err(PatternMethodParamWithoutBody {\n            span: pat.span,\n            target: match context {\n                FnContext::Trait => \"methods without bodies\",\n                FnContext::FunctionPtrType => \"function pointer types\",\n                FnContext::Free => unreachable!(\"This method is not called in free functions, as patterns are always allowed there\"),\n                FnContext::Impl => unreachable!(\"This method is not called in impls, as patterns are always allowed there\"),\n            },\n        });\n\n        // Pretend the pattern is `_`, to avoid duplicate errors from AST validation.\n        let pat = Box::new(Pat { kind: PatKind::Wild, span: pat.span, id: ast::DUMMY_NODE_ID });\n        Ok((pat, ty))\n    }\n\n    pub(super) fn recover_bad_self_param(&mut self, mut param: Param) -> PResult<'a, Param> {\n        let span = param.pat.span;\n        let guar = self.dcx().emit_err(SelfParamNotFirst { span });\n        param.ty.kind = TyKind::Err(guar);\n        Ok(param)\n    }\n\n    pub(super) fn consume_block(\n        &mut self,","sourceCodeStart":2382,"sourceCodeEnd":2418,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_parse/src/parser/diagnostics.rs#L2382-L2418","documentation":"Internal `unreachable!` in `recover_arg_parse`, which recovers from malformed function parameters by re-parsing `pat: Ty`. This recovery is only valid for trait method signatures and function-pointer types, where identifier-only parameters (no pattern) are disallowed; for free functions and inherent impls patterns are always permitted, so the caller must never pass `FnContext::Free` or `FnContext::Impl`. The `target` field's match arms for those two variants panic.","triggerScenarios":"The parser's argument-recovery logic is invoked with `context = FnContext::Free` or `FnContext::Impl`, i.e. the caller misclassified the surrounding function kind while recovering from a bad parameter.","commonSituations":"Compiler regression in parameter parsing/recovery after refactor of `FnContext`; surfaced when a user writes a malformed parameter (e.g. missing type) and the recovery path is misrouted.","solutions":["Fix the syntax error the compiler was attempting to recover from (add the missing type annotation) so recovery is not invoked","Report as a rustc ICE with the malformed function signature and the backtrace","Try a different toolchain to check whether it is a recent regression"],"exampleFix":"// before: malformed parameter triggers recovery\nfn f(x) {}\n// after: provide the parameter type so recovery is unnecessary\nfn f(x: i32) {}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"use rustc_ast::ast::{Item, ItemKind};\npub fn item_allows_patterns_unconditionally(item: &Item) -> bool {\n    // Free functions always allow patterns; the guarded method is meant for\n    // closures/associated fns/other contexts where patterns may be restricted.\n    matches!(item.kind, ItemKind::Fn(..)) && !item_is_in_impl_or_trait(item)\n}\nfn item_is_in_impl_or_trait(_item: &Item) -> bool { /* consult parent context */ false }","tryCatchPattern":null,"preventionTips":["Only call this pattern-restriction method on parameters of closures or associated functions, never on free functions.","Inspect the parent item kind before invoking; for top-level `fn`, patterns are always allowed and the method will panic.","Carry the enclosing item context alongside the param so you can branch on free-fn vs associated-fn.","If walking params generically, skip the method call when the owner is an ItemKind::Fn at module scope."],"tags":["compiler-ice","parser","fn-signatures","error-recovery"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}