{"record":{"id":"5770f04f974347ae","repo":"tokio-rs/axum","slug":"can-t-infer-state-type-please-add-attr-name-s","errorCode":null,"errorMessage":"can't infer state type, please add #[{attr_name}(state = MyStateType)] attribute","messagePattern":"can't infer state type, please add #\\[(.+?)\\(state = MyStateType\\)\\] attribute","errorType":"validation","errorClass":"syn::Error","httpStatus":null,"severity":"error","filePath":"axum-macros/src/from_request/mod.rs","lineNumber":154,"sourceCode":"                    fields,\n                    &via,\n                    rejection.as_ref(),\n                    generic_ident.as_ref(),\n                    &state,\n                    tr,\n                )?,\n                (None, rejection) => {\n                    error_on_generic_ident(generic_ident, tr)?;\n                    impl_struct_by_extracting_each_field(&ident, &fields, rejection, &state, tr)?\n                }\n            };\n\n            if matches!(state, State::CannotInfer) {\n                let attr_name = match tr {\n                    Trait::FromRequest => \"from_request\",\n                    Trait::FromRequestParts => \"from_request_parts\",\n                };\n                let compile_error = syn::Error::new(\n                    Span::call_site(),\n                    format_args!(\n                        \"can't infer state type, please add \\\n                         `#[{attr_name}(state = MyStateType)]` attribute\",\n                    ),\n                )\n                .into_compile_error();\n\n                Ok(quote! {\n                    #trait_impl\n                    #compile_error\n                })\n            } else {\n                Ok(trait_impl)\n            }\n        }\n        syn::Item::Enum(item) => {\n            let syn::ItemEnum {","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/tokio-rs/axum/blob/151cd5c12325373b86daf405a6afc0a0086a6706/axum-macros/src/from_request/mod.rs#L136-L172","documentation":"Thrown by #[derive(FromRequest)] / #[derive(FromRequestParts)] on a struct (from_request/mod.rs:154). The macro infers the state type by scanning fields: infer_state_type_from_field_types collects inner types of any State<T> field, and infer_state_type_from_field_attributes collects the field type of any #[from_request(via(State))] field. If these yield two or more distinct candidate types, state becomes State::CannotInfer. The macro still emits the generated impl (with an unimplemented!() body so downstream errors are visible) followed by this compile_error.","triggerScenarios":"#[derive(FromRequest)] struct Extractor { a: State<AppState>, b: State<OtherState> } with no #[from_request(state = ...)] container attribute, where AppState and OtherState are different types. Also triggered when a State<A> field and a #[from_request(via(State))] field: B disagree (A != B), or when via(State) on the container combined with conflicting field-level State<...> types yields more than one candidate.","commonSituations":"Building a composite extractor that aggregates several sub-extractors each carrying their own state. Mixing a State<T> field with a #[from_request(via(State))] field on the same struct. Migrating a struct from one state type to another and leaving a stale field behind.","solutions":["Add the container attribute naming the router's root state: #[from_request(state = AppState)] (or #[from_request_parts(state = AppState)]).","Make all State<T> / #[from_request(via(State))] fields agree on the same inner type.","Drop the redundant State<_> field and obtain sub-state via FromRef instead."],"exampleFix":"// before\n#[derive(FromRequest)]\nstruct Extractor {\n    inner_state: State<AppState>,\n    other_state: State<OtherState>,\n}\n\n// after\n#[derive(FromRequest)]\n#[from_request(state = AppState)]\nstruct Extractor {\n    inner_state: State<AppState>,\n    other_state: State<OtherState>,\n}","handlingStrategy":"validation","validationCode":"// Safe invocation pattern: if a FromRequest struct has fields whose inferred\n// state types could disagree, always set the container `state = ...`.\n#[derive(FromRequest)]\n#[from_request(state = AppState)] // names the router root state explicitly\nstruct Extractor {\n    inner_state: State<AppState>,\n    other_state: State<OtherState>,\n}\n\n// Single-state structs infer fine without the attribute:\n#[derive(FromRequest)]\nstruct Simple { s: State<AppState> }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Audit every #[derive(FromRequest)]/FromRequestParts struct: count distinct inner types across State<T> fields and #[from_request(via(State))] fields; if >1, add #[from_request(state = ...)].","Keep all State<_> fields of a derived extractor anchored to the same root state; pull sub-state through FromRef.","When refactoring state types, re-check derived extractors that referenced the old type.","Treat #[from_request(via(State))] on a field as another state-type candidate — it counts toward inference ambiguity."],"tags":["axum","axum-macros","from-request","derive","state","proc-macro","compile-error"],"backgroundTag":null,"analyzedSha":"151cd5c12325373b86daf405a6afc0a0086a6706","analyzedAt":"2026-08-11T16:57:37.422Z","contentChangedAt":"2026-08-11T16:57:37.422Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}