rust-lang/rust · critical

CGU symbol name accessed before setting

Error message

CGU symbol name accessed before setting

What it means

This panic fires inside `CodegenUnit::symbol_name()` (mono.rs:455), which calls `.expect()` on the `symbol_name: Option<Symbol>` field. The codegen unit's mangled symbol name is computed lazily during the partitioning/naming pass and stored via `set_symbol_name`; calling the getter before the setter has run means the CGU pipeline was driven out of order. It is a pure internal invariant of `rustc_middle::mono`, never reachable through normal user Rust code.

Source

Thrown at compiler/rustc_middle/src/mono.rs:455

    pub fn items(&self) -> &FxIndexMap<MonoItem<'tcx>, MonoItemData> {
        &self.items
    }

    pub fn items_mut(&mut self) -> &mut FxIndexMap<MonoItem<'tcx>, MonoItemData> {
        &mut self.items
    }

    pub fn is_code_coverage_dead_code_cgu(&self) -> bool {
        self.is_code_coverage_dead_code_cgu
    }

    /// Marks this CGU as the one used to contain code coverage information for dead code.
    pub fn make_code_coverage_dead_code_cgu(&mut self) {
        self.is_code_coverage_dead_code_cgu = true;
    }

    pub fn symbol_name(&self) -> Symbol {
        self.symbol_name.expect("CGU symbol name accessed before setting")
    }

    pub fn set_symbol_name(&mut self, name: Symbol) {
        self.symbol_name = Some(name);
    }

    pub fn mangle_name(human_readable_name: &str) -> BaseNString {
        let mut hasher = StableHasher::new();
        human_readable_name.hash(&mut hasher);
        let hash: Hash128 = hasher.finish();
        hash.as_u128().to_base_fixed_len(CASE_INSENSITIVE)
    }

    pub fn shorten_name(human_readable_name: &str) -> Cow<'_, str> {
        // Set a limit a somewhat below the common platform limits for file names.
        const MAX_CGU_NAME_LENGTH: usize = 200;
        const TRUNCATED_NAME_PREFIX: &str = "-trunc-";
        if human_readable_name.len() > MAX_CGU_NAME_LENGTH {

View on GitHub (pinned to 22057b88b0)

Solutions

  1. File an issue at https://github.com/rust-lang/rust with the exact rustc commit (`rustc -vV`) and a minimal reproducer; this is a compiler bug, not user code.
  2. Reproduce on the latest nightly; many such regressions are fixed within days.
  3. If you are building rustc yourself, re-run `x.py build` cleanly from a known-good commit and bisect the offending PR.
  4. Retry the build with `-C incremental=no` and a clean `target/` to rule out a stale-CGU interaction.
  5. If you maintain an out-of-tree rustc tool/driver, audit your pass ordering: ensure `set_symbol_name` runs on every CGU before any codegen step reads `symbol_name()`.

Example fix

// before (internal rustc pass, incorrect ordering)
let name = cgu.symbol_name();           // panic: name not set yet
generate_symbol_with(&cxt, name);

// after
if cgu.symbol_name.is_none() {
    let name = compute_cgu_symbol_name(tcx, &cgu);
    cgu.set_symbol_name(name);
}
let name = cgu.symbol_name();
generate_symbol_with(&cxt, name);
Defensive patterns

Strategy: retry

Try / catch

// CGU symbol-name-before-set is an internal rustc bug (ICE).
// There is no source-level guard; treat it as a transient compiler panic.
use std::process::Command;
fn compile_resilient(crate_dir: &str) -> bool {
    for attempt in 0..2u8 {
        let status = Command::new("cargo")
            .args(["build"])
            .current_dir(crate_dir)
            .status()
            .expect("failed to spawn cargo");
        if status.success() { return true; }
        // If output mentions the ICE, nuke artifacts and retry once.
        if attempt == 0 {
            let _ = Command::new("cargo").args(["clean"]).current_dir(crate_dir).status();
        }
    }
    false
}

Prevention

When it happens

Trigger: Reached only when an internal rustc pass invokes `CodegenUnit::symbol_name()` before the symbol-assignment pass (which calls `set_symbol_name`) has visited that unit—e.g. a custom codegen-unit partitioning hook, an in-tree rustc tool calling the query system out of order, or a regression in the monomorphization-collection pipeline.

Common situations: Developers hit this when building the rustc compiler itself with an experimental/staged toolchain, when running rustc bootstrap with `--stage 1` against mismatched crates, when bisecting a rustc regression, or when a rustc nightly regresses the CGU naming pass. End users compiling ordinary crates essentially never see it.

Related errors


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