{"id":"fb06e0371fbf4ca7","repo":"rust-lang/rust","slug":"non-utf8-diagnostic","errorCode":null,"errorMessage":"non-UTF8 diagnostic","messagePattern":"non-UTF8 diagnostic","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/rustc_codegen_llvm/src/back/write.rs","lineNumber":481,"sourceCode":"                line: opt.line,\n                column: opt.column,\n                pass_name: &opt.pass_name,\n                kind: match opt.kind {\n                    OptimizationRemark => \"success\",\n                    OptimizationMissed | OptimizationFailure => \"missed\",\n                    OptimizationAnalysis\n                    | OptimizationAnalysisFPCommute\n                    | OptimizationAnalysisAliasing => \"analysis\",\n                    OptimizationRemarkOther => \"other\",\n                },\n                message: &opt.message,\n            });\n        }\n        llvm::diagnostic::PGO(diagnostic_ref) | llvm::diagnostic::Linker(diagnostic_ref) => {\n            let message = llvm::build_string(|s| unsafe {\n                llvm::LLVMRustWriteDiagnosticInfoToString(diagnostic_ref, s)\n            })\n            .expect(\"non-UTF8 diagnostic\");\n            dcx.emit_warn(FromLlvmDiag { message });\n        }\n        llvm::diagnostic::Unsupported(diagnostic_ref) => {\n            let message = llvm::build_string(|s| unsafe {\n                llvm::LLVMRustWriteDiagnosticInfoToString(diagnostic_ref, s)\n            })\n            .expect(\"non-UTF8 diagnostic\");\n            dcx.emit_err(FromLlvmDiag { message });\n        }\n        llvm::diagnostic::UnknownDiagnostic(..) => {}\n    }\n}\n\nfn get_pgo_gen_path(config: &ModuleConfig) -> Option<CString> {\n    match config.pgo_gen {\n        SwitchWithOptPath::Enabled(ref opt_dir_path) => {\n            let path = if let Some(dir_path) = opt_dir_path {\n                dir_path.join(\"default_%m.profraw\")","sourceCodeStart":463,"sourceCodeEnd":499,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_codegen_llvm/src/back/write.rs#L463-L499","documentation":"Thrown by `.expect(\"non-UTF8 diagnostic\")` on `llvm::build_string(...)` for the `PGO`/`Linker` diagnostic branch in `diagnostic_handler` (write.rs:481). When LLVM emits a Profile-Guided-Optimization or linker diagnostic, rustc asks LLVM to render it to a string; if that rendered string is not valid UTF-8 this expect panics. The diagnostic is later emitted as a warning (`emit_warn`), but reaching the expect means the message bytes were malformed.","triggerScenarios":"Triggered during codegen/linking with PGO (`-C profile-use`/`-C profile-generate`) or when LLVM produces a linker diagnostic, AND the bytes LLVM writes via `LLVMRustWriteDiagnosticInfoToString` are not valid UTF-8 — e.g. a linker error containing locale-specific/non-UTF-8 characters, or memory corruption of the diagnostic buffer.","commonSituations":"Linker (`ld`/`lld`) emitting a localized or garbage message under PGO; using a non-UTF-8 locale (`LANG`/`LC_ALL`) that influences linker output; corrupt PGO profile data causing LLVM to emit a malformed diagnostic; custom/botched LLVM build. Rare with stock toolchains and C locales.","solutions":["Force a UTF-8/C locale for the build: `LC_ALL=C LANG=C cargo build`.","If using PGO, regenerate the profile data (`-C profile-generate`) and confirm the `.profdata` is valid (`llvm-profdata show`).","Switch to the official rustup toolchain to rule out a custom-LLVM diagnostic bug.","Change linker (e.g. `rustflags = [\"-C\", \"linker=clang\", \"-C\", \"link-arg=-fuse-ld=lld\"]`) to see if the malformed diagnostic came from a specific linker.","Report to rustc with the linker/LLVM version and the exact flags if reproducible."],"exampleFix":"# before: localized linker diagnostic produces non-UTF-8 bytes\ncargo build --release  # panic: non-UTF8 diagnostic\n\n# after: force C locale and a UTF-8-safe linker\nexport LC_ALL=C LANG=C\ncargo build --release","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"let res = std::panic::catch_unwind(|| run_codegen_with_pgo_or_linker_diag());\nif res.is_err() {\n    // PGO/Linker diagnostic string was non-UTF-8; fall back to a build without that diagnostic path.\n    run_codegen_silent();\n}","preventionTips":["Use a UTF-8-capable linker (lld) and ensure its diagnostic output is UTF-8.","Set LANG/LC_ALL to a UTF-8 locale so linker error text is decoded correctly.","Avoid object/source paths containing non-UTF-8 bytes when PGO (-Cprofile-generate) is on.","Keep linker and rustc from the same platform target; mismatched linkers emit localized non-UTF-8 messages."],"tags":["rustc","llvm","linker","pgo","utf8"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}