{"record":{"id":"5d08e30f79a70227","repo":"rust-lang/rust","slug":"expected-a-static-item-but-found-value","errorCode":null,"errorMessage":"Expected a static item, but found: {value:?}","messagePattern":"Expected a static item, but found: (.+?)","errorType":"validation","errorClass":"bridge::Error","httpStatus":null,"severity":"error","filePath":"compiler/rustc_public/src/mir/mono.rs","lineNumber":284,"sourceCode":"    fn def_id(&self) -> DefId {\n        with(|context| context.instance_def_id(*self))\n    }\n}\n\ncrate_def! {\n    /// Holds information about a static variable definition.\n    #[derive(Serialize)]\n    pub StaticDef;\n}\n\nimpl TryFrom<CrateItem> for StaticDef {\n    type Error = crate::Error;\n\n    fn try_from(value: CrateItem) -> Result<Self, Self::Error> {\n        if matches!(value.kind(), ItemKind::Static) {\n            Ok(StaticDef(value.0))\n        } else {\n            Err(bridge::Error::new(format!(\"Expected a static item, but found: {value:?}\")))\n        }\n    }\n}\n\nimpl TryFrom<Instance> for StaticDef {\n    type Error = crate::Error;\n\n    fn try_from(value: Instance) -> Result<Self, Self::Error> {\n        StaticDef::try_from(CrateItem::try_from(value)?)\n    }\n}\n\nimpl From<StaticDef> for Instance {\n    fn from(value: StaticDef) -> Self {\n        // A static definition should always be convertible to an instance.\n        with(|cx| cx.mono_instance(value.def_id()))\n    }\n}","sourceCodeStart":266,"sourceCodeEnd":302,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_public/src/mir/mono.rs#L266-L302","documentation":"Thrown by `impl TryFrom<CrateItem> for StaticDef` (mono.rs:284) when a crate item that is not a `Static` is converted into a `StaticDef`. Only items whose `ItemKind` is `Static` are accepted, since a static definition needs a corresponding static DefKind.","triggerScenarios":"Calling `StaticDef::try_from(crate_item)` / `crate_item.try_into()` on a `CrateItem` whose kind is `Fn`, `Const`, `Trait`, etc.","commonSituations":"Filtering crate items into typed handles without first checking `item.kind()`; processing a mixed list of functions and statics through a single conversion path.","solutions":["Check `matches!(item.kind(), ItemKind::Static)` before converting.","Separate items by kind using `crate_statics()` instead of `crate_items()` when you only want statics.","Handle non-static kinds in a separate branch."],"exampleFix":"// before\nlet s: StaticDef = crate_item.try_into()?;\n\n// after\nlet s: StaticDef = if matches!(crate_item.kind(), ItemKind::Static) {\n    crate_item.try_into()?\n} else {\n    continue;\n};","handlingStrategy":"type-guard","validationCode":"// Only attempt the conversion on items known to be statics.\nif !matches!(item.kind(), ItemKind::Static) {\n    return Err(\"not a static item\".into());\n}\nStaticDef::try_from(item)","typeGuard":"fn is_static_item(item: &CrateItem) -> bool {\n    matches!(item.kind(), ItemKind::Static)\n}","tryCatchPattern":null,"preventionTips":["Use crate_statics() to fetch only static items in the first place.","Always narrow on ItemKind before a typed TryFrom conversion."],"tags":["rustc-public","static","crate-item","type-guard"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}