{"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":"exception","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/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_public/src/mir/mono.rs#L266-L302","documentation":"Thrown by `TryFrom<CrateItem> for StaticDef` in compiler/rustc_public/src/mir/mono.rs:284 when the crate item is not a static variable. `StaticDef` is only meaningful for `ItemKind::Static`, so passing a function, const, trait, or module item fails. rustc_public enforces the kind match because a static has a fixed address and an initializer that other item kinds do not have.","triggerScenarios":"Calling `StaticDef::try_from(crate_item)` or `StaticDef::try_from(instance)` (via the `Instance -> CrateItem -> StaticDef` chain) on an item whose `kind()` is not `ItemKind::Static`. Blindly converting every item in a crate to a `StaticDef`.","commonSituations":"Analyzers that scan for statics and forget to filter by item kind. Using a DefId-by-path lookup that resolves to a `const` rather than a `static` (a common confusion since both look similar). Refactors that changed a `static` to a `const`.","solutions":["Check `item.kind()` equals `ItemKind::Static` before calling `StaticDef::try_from`.","If resolving by DefId/path, confirm the definition is a `static` (not a `const` or `fn`) at the call site.","On the instance path, remember the conversion goes `Instance -> CrateItem -> StaticDef`; ensure the originating instance is a static before attempting it."],"exampleFix":"// before\nlet def = StaticDef::try_from(item)?;\n// after\nlet def = match item.kind() {\n    ItemKind::Static => StaticDef::try_from(item)?,\n    _ => continue,\n};","handlingStrategy":"type-guard","validationCode":"// Before StaticDef::try_from(item):\nif !matches!(item.kind(), ItemKind::Static) {\n    return Err(format!(\"item {:?} is {:?}, not a static\", item, item.kind()));\n}\nlet static_def = StaticDef::try_from(item)?;","typeGuard":"// True only for `static` items.\nfn is_static_item(item: &CrateItem) -> bool {\n    matches!(item.kind(), ItemKind::Static)\n}","tryCatchPattern":"match StaticDef::try_from(item) {\n    Ok(s) => s,\n    Err(e) if e.to_string().contains(\"Expected a static item\") => {\n        // Item is Fn/Const/Ctor; route it to the right handler by kind.\n        return route_by_kind(item);\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Always dispatch on `CrateItem::kind()` before narrowing; `ItemKind` has four variants (Fn, Static, Const, Ctor) and each maps to a distinct typed wrapper.","When collecting statics, filter with `items.into_iter().filter(|i| matches!(i.kind(), ItemKind::Static))` rather than converting blindly and catching errors.","Do not assume a `CrateItem` obtained from `Crate::fn_defs()` is a `Static`; use a static-specific enumerator if the API provides one."],"tags":["rust","mir","static","conversion"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}