{"id":"4161445671f84a55","repo":"rust-lang/rust","slug":"statics-should-not-have-generic-parameters","errorCode":null,"errorMessage":"statics should not have generic parameters","messagePattern":"statics should not have generic parameters","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/rustc_middle/src/mir/interpret/mod.rs","lineNumber":336,"sourceCode":"            GlobalAlloc::Static(did) => {\n                let DefKind::Static { safety: _, mutability, nested } = tcx.def_kind(did) else {\n                    bug!()\n                };\n                if nested {\n                    // Nested statics in a `static` are never interior mutable,\n                    // so just use the declared mutability.\n                    if cfg!(debug_assertions) {\n                        let alloc = tcx.eval_static_initializer(did).unwrap();\n                        assert_eq!(alloc.0.mutability, mutability);\n                    }\n                    mutability\n                } else {\n                    let mutability = match mutability {\n                        Mutability::Not\n                            if !tcx\n                                .type_of(did)\n                                .no_bound_vars()\n                                .expect(\"statics should not have generic parameters\")\n                                .is_freeze(tcx, typing_env) =>\n                        {\n                            Mutability::Mut\n                        }\n                        _ => mutability,\n                    };\n                    mutability\n                }\n            }\n            GlobalAlloc::Memory(alloc) => alloc.inner().mutability,\n            GlobalAlloc::TypeId { .. } | GlobalAlloc::Function { .. } | GlobalAlloc::VTable(..) => {\n                // These are immutable.\n                Mutability::Not\n            }\n        }\n    }\n\n    pub fn size_and_align(","sourceCodeStart":318,"sourceCodeEnd":354,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_middle/src/mir/interpret/mod.rs#L318-L354","documentation":"`.expect(\"statics should not have generic parameters\")` in `GlobalAlloc::mutability` (mod.rs:336) asserts that `tcx.type_of(static_def_id).no_bound_vars()` returns `Some`, i.e. the static's type contains no late/early-bound parameters. Rust's type system forbids generic `static`s, so by the time const-eval queries an allocation's mutability the type must be monomorphic. Reaching the expect means a `static` reached MIR/interpret with a type that still has bound variables — an upstream validation/HIR-lowering bug.","triggerScenarios":"A `static` whose `Ty` has bound `Region`/`Ty`/`Const` vars after `tcx.type_of` substitution, queried via `GlobalAlloc::Static(did).mutability(tcx, typing_env)`. Reproducible when a rustc pass fails to reject a generic static in HIR, when an extern static's type is decoded with lingering bound vars, or when a test/fuzzer hand-constructs a `GlobalAlloc::Static` for an ill-formed DefId.","commonSituations":"Fuzzing rustc with malformed item declarations; regressions in `rustc_hir_analysis` item-kind validation after generic-params refactors; mismatched metadata between a crate and its deps; custom derives that emit `static FOO: T = ...` with `T` referencing an outer generic.","solutions":["Inspect the static's type with `tcx.type_of(did)` and confirm whether it legitimately has bound vars; if so, the bug is in HIR lowering or the item's generics, not here.","Bisect changes to `rustc_resolve`/`rustc_hir_analysis` that affect static item kind validation.","If you wrote the offending code: convert the generic `static` into a `const` or a function returning the value (Rust has no generic statics).","File a rustc ICE with the DefId and the bound-var kind from the panic."],"exampleFix":"// before (ill-formed; reaches interpret with bound vars)\nstatic FOO<T>: T = Default::default();\n\n// after — Rust statics must be monomorphic\nstatic FOO: ConcreteDefault = ConcreteDefault::default();","handlingStrategy":"validation","validationCode":"// The expect fires on tcx.type_of(did).no_bound_vars() == None, i.e. the\n// static's type still mentions generic parameters. Validate the type before\n// asking for its mutability / size.\n//\n// Note: this is the SAME invariant as error 246; the two differs only in\n// which call site (mutability path vs size/align path) trips the expect.\nfn static_type_is_concrete<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> bool {\n    tcx.type_of(did).no_bound_vars().is_some()\n}\n\nif static_type_is_concrete(tcx, did) {\n    let mutability = alloc_mutability(tcx, did); // safe to call\n} else {\n    return Err(\"static has generic parameters; not monomorphic\");\n}","typeGuard":null,"tryCatchPattern":"let m = std::panic::catch_unwind(|| alloc_mutability(tcx, did));\nmatch m {\n    Ok(m) => /* use m */,\n    Err(_) => /* reject the static; it is not a valid monomorphic target */,\n}","preventionTips":["Never declare a static with generic parameters; Rust rejects `static FOO: T = ...` for generic T at typecheck, but proc-macro / macro-generated code can still synthesize such items.","Before querying a static's mutability or layout, confirm the type is monomorphic via no_bound_vars().is_some().","Treat a None from no_bound_vars() on a static as a hard error in your tooling, not as a retry condition.","Audit macro-generated statics (lazy_static, const generics) for accidentally引入ing parameters on the static itself."],"tags":["rustc","mir","const-eval","statics","generics","internal","compiler-ice"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}