rust-lang/rust · error

`static_addr_of_mut` did not add the global to `self.global_

Error message

`static_addr_of_mut` did not add the global to `self.global_lvalues`

What it means

In `consts.rs:73-78`, when the `master` feature is enabled, `codegen_static_initializer`-adjacent code calls `self.global_lvalues.borrow().get(&global_value).expect(...)` to mark a const global readonly. `static_addr_of_mut` (consts.rs:192-216) is *supposed* to insert the rvalue→global mapping at line 214. The expect fires when that insertion did not happen for the returned rvalue — i.e. the address returned by `global.get_address(None)` is not the key found in `global_lvalues`. This is a defensive invariant check that only compiles with `--cfg feature="master"`.

Source

Thrown at compiler/rustc_codegen_gcc/src/consts.rs:77

    fn static_addr_of(&self, alloc: ConstAllocation<'_>, kind: Option<&str>) -> RValue<'gcc> {
        let cv = const_alloc_to_gcc(self, alloc);
        let align = alloc.inner().align;

        if let Some(variable) = self.const_globals.borrow().get(&cv) {
            if let Some(global_variable) = self.global_lvalues.borrow().get(variable) {
                let alignment = align.bits() as i32;
                if alignment > global_variable.get_alignment() {
                    global_variable.set_alignment(alignment);
                }
            }
            return *variable;
        }
        let global_value = self.static_addr_of_mut(cv, align, kind);
        #[cfg(feature = "master")]
        self.global_lvalues
            .borrow()
            .get(&global_value)
            .expect("`static_addr_of_mut` did not add the global to `self.global_lvalues`")
            .global_set_readonly();
        self.const_globals.borrow_mut().insert(cv, global_value);
        global_value
    }

    fn codegen_static(&mut self, def_id: DefId) {
        let attrs = self.tcx.codegen_fn_attrs(def_id);

        let Ok((value, alloc)) = codegen_static_initializer(self, def_id) else {
            // Error has already been reported
            return;
        };
        let alloc = alloc.inner();

        // boolean SSA values are i1, but they have to be stored in i8 slots,
        // otherwise some LLVM optimization passes don't work as expected
        let val_llty = self.val_ty(value);
        if val_llty == self.type_i1() {

View on GitHub (pinned to 22057b88b0)

Solutions

  1. Confirm you are on a libgccjit version compatible with this rustc_codegen_gcc checkout; `get_address` returning unequal identities across calls is the usual culprit — re-pin libgccjit.
  2. If you have a custom `static_addr_of_mut`, ensure it inserts the *exact* rvalue it returns into `self.global_lvalues` (as the upstream does at consts.rs:214).
  3. Cache the rvalue once: call `global.get_address(None)` a single time and reuse it for both the insert and the return value, avoiding identity drift.
  4. If unavoidable, relax the expect to best-effort (mark readonly only when present) rather than panicking, but treat that as a stopgap and report the invariant break upstream.

Example fix

// before (consts.rs:72-78)
let global_value = self.static_addr_of_mut(cv, align, kind);
#[cfg(feature = "master")]
self.global_lvalues
    .borrow()
    .get(&global_value)
    .expect("`static_addr_of_mut` did not add the global to `self.global_lvalues`")
    .global_set_readonly();

// after (static_addr_of_mut caches the address once — consts.rs:211-215)
let rvalue = global.get_address(None);
self.global_lvalues.borrow_mut().insert(rvalue, global);
rvalue  // returned value is guaranteed to be the inserted key
Defensive patterns

Strategy: validation

Validate before calling

// consts.rs:77 -> invariant: static_addr_of_mut must register the global
// in self.global_lvalues. This path only runs under the `master` gccjit
// feature (const_globals/readonly promotion). Sidestep by disabling master
// or by not promoting a mutable backing value to a readonly const global.
#[cfg(not(feature = "master"))]
fn compile_with_gcc_backend(cx: &mut CodegenCx) { /* non-master path: assertion gated out */ }

Prevention

When it happens

Trigger: Compiling with the `master` feature (which enables libgccjit master-branch APIs used in tests/build_system/test.rs) AND a code path where `static_addr_of_mut`'s returned `rvalue` (from `global.get_address(None)`) is not the same key inserted into `global_lvalues`, or where the insert was bypassed/overridden by a custom path. Possible if `get_address` returns a fresh/distinct rvalue identity each call, or if a subclass/replacement of `static_addr_of_mut` skips the insert.

Common situations: Seen only on builds with `feature="master"` against a newer libgccjit where `get_address`/global identity semantics changed; local forks that wrap or intercept `static_addr_of_mut`; races from multiple codegen units if `global_lvalues` is unexpectedly shared. On a clean upstream build this should never fire.

Related errors


AI-assisted analysis of rust-lang/rust@22057b88b0 (2026-08-03). Data as JSON: /data/errors/0569bfdaaad7332f.json. Report an issue: GitHub.