{"id":"4d3f079c58c32b9a","repo":"rust-lang/rust","slug":"cgu-symbol-name-accessed-before-setting","errorCode":null,"errorMessage":"CGU symbol name accessed before setting","messagePattern":"CGU symbol name accessed before setting","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_middle/src/mono.rs","lineNumber":455,"sourceCode":"    pub fn items(&self) -> &FxIndexMap<MonoItem<'tcx>, MonoItemData> {\n        &self.items\n    }\n\n    pub fn items_mut(&mut self) -> &mut FxIndexMap<MonoItem<'tcx>, MonoItemData> {\n        &mut self.items\n    }\n\n    pub fn is_code_coverage_dead_code_cgu(&self) -> bool {\n        self.is_code_coverage_dead_code_cgu\n    }\n\n    /// Marks this CGU as the one used to contain code coverage information for dead code.\n    pub fn make_code_coverage_dead_code_cgu(&mut self) {\n        self.is_code_coverage_dead_code_cgu = true;\n    }\n\n    pub fn symbol_name(&self) -> Symbol {\n        self.symbol_name.expect(\"CGU symbol name accessed before setting\")\n    }\n\n    pub fn set_symbol_name(&mut self, name: Symbol) {\n        self.symbol_name = Some(name);\n    }\n\n    pub fn mangle_name(human_readable_name: &str) -> BaseNString {\n        let mut hasher = StableHasher::new();\n        human_readable_name.hash(&mut hasher);\n        let hash: Hash128 = hasher.finish();\n        hash.as_u128().to_base_fixed_len(CASE_INSENSITIVE)\n    }\n\n    pub fn shorten_name(human_readable_name: &str) -> Cow<'_, str> {\n        // Set a limit a somewhat below the common platform limits for file names.\n        const MAX_CGU_NAME_LENGTH: usize = 200;\n        const TRUNCATED_NAME_PREFIX: &str = \"-trunc-\";\n        if human_readable_name.len() > MAX_CGU_NAME_LENGTH {","sourceCodeStart":437,"sourceCodeEnd":473,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_middle/src/mono.rs#L437-L473","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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.","Reproduce on the latest nightly; many such regressions are fixed within days.","If you are building rustc yourself, re-run `x.py build` cleanly from a known-good commit and bisect the offending PR.","Retry the build with `-C incremental=no` and a clean `target/` to rule out a stale-CGU interaction.","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()`."],"exampleFix":"// before (internal rustc pass, incorrect ordering)\nlet name = cgu.symbol_name();           // panic: name not set yet\ngenerate_symbol_with(&cxt, name);\n\n// after\nif cgu.symbol_name.is_none() {\n    let name = compute_cgu_symbol_name(tcx, &cgu);\n    cgu.set_symbol_name(name);\n}\nlet name = cgu.symbol_name();\ngenerate_symbol_with(&cxt, name);","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"// CGU symbol-name-before-set is an internal rustc bug (ICE).\n// There is no source-level guard; treat it as a transient compiler panic.\nuse std::process::Command;\nfn compile_resilient(crate_dir: &str) -> bool {\n    for attempt in 0..2u8 {\n        let status = Command::new(\"cargo\")\n            .args([\"build\"])\n            .current_dir(crate_dir)\n            .status()\n            .expect(\"failed to spawn cargo\");\n        if status.success() { return true; }\n        // If output mentions the ICE, nuke artifacts and retry once.\n        if attempt == 0 {\n            let _ = Command::new(\"cargo\").args([\"clean\"]).current_dir(crate_dir).status();\n        }\n    }\n    false\n}","preventionTips":["Pin to a known-good nightly/stable toolchain; this is a compiler bug, not user-code.","Report the ICE at https://github.com/rust-lang/rust with RUST_BACKTRACE=full output.","Run a `cargo clean` before retrying so stale CGU metadata is rebuilt.","If reproducible only under incremental compilation, disable it (CARGO_INCREMENTAL=0)."],"tags":["rustc","ice","codegen-unit","symbol-mangling","internal-compiler-error"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}