rust-lang/rust · error
statics should not have generic parameters
Error message
statics should not have generic parameters
What it means
`.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.
Source
Thrown at compiler/rustc_middle/src/mir/interpret/mod.rs:336
GlobalAlloc::Static(did) => {
let DefKind::Static { safety: _, mutability, nested } = tcx.def_kind(did) else {
bug!()
};
if nested {
// Nested statics in a `static` are never interior mutable,
// so just use the declared mutability.
if cfg!(debug_assertions) {
let alloc = tcx.eval_static_initializer(did).unwrap();
assert_eq!(alloc.0.mutability, mutability);
}
mutability
} else {
let mutability = match mutability {
Mutability::Not
if !tcx
.type_of(did)
.no_bound_vars()
.expect("statics should not have generic parameters")
.is_freeze(tcx, typing_env) =>
{
Mutability::Mut
}
_ => mutability,
};
mutability
}
}
GlobalAlloc::Memory(alloc) => alloc.inner().mutability,
GlobalAlloc::TypeId { .. } | GlobalAlloc::Function { .. } | GlobalAlloc::VTable(..) => {
// These are immutable.
Mutability::Not
}
}
}
pub fn size_and_align(View on GitHub (pinned to 22057b88b0)
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.
Example fix
// before (ill-formed; reaches interpret with bound vars) static FOO<T>: T = Default::default(); // after — Rust statics must be monomorphic static FOO: ConcreteDefault = ConcreteDefault::default();
Defensive patterns
Strategy: validation
Validate before calling
// The expect fires on tcx.type_of(did).no_bound_vars() == None, i.e. the
// static's type still mentions generic parameters. Validate the type before
// asking for its mutability / size.
//
// Note: this is the SAME invariant as error 246; the two differs only in
// which call site (mutability path vs size/align path) trips the expect.
fn static_type_is_concrete<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> bool {
tcx.type_of(did).no_bound_vars().is_some()
}
if static_type_is_concrete(tcx, did) {
let mutability = alloc_mutability(tcx, did); // safe to call
} else {
return Err("static has generic parameters; not monomorphic");
} Try / catch
let m = std::panic::catch_unwind(|| alloc_mutability(tcx, did));
match m {
Ok(m) => /* use m */,
Err(_) => /* reject the static; it is not a valid monomorphic target */,
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- there must be provenance somewhere here
- invalid level/lint_id combination
- this deprecation is always in effect; {since:?}
- range should be nonempty
- an interpreter error got improperly discarded; use `discard_
AI-assisted analysis of rust-lang/rust@22057b88b0 (2026-08-03).
Data as JSON: /data/errors/4161445671f84a55.json.
Report an issue: GitHub.