Skip to content

Commit 4b786dd

Browse files
Rollup merge of #157470 - TaKO8Ki:fix-target-machine-config-diag-ice, r=JonathanBrouwer
Avoid ICE when emitting TargetMachine config errors Fixes #157401 `ParseTargetMachineConfig::into_diag` constructed an inner `LlvmError` diagnostic to reuse its formatted message, but did not emit or cancel that temporary diagnostic. That a root cause of the ICE.
2 parents 0d6c93f + e2cc7e6 commit 4b786dd

5 files changed

Lines changed: 24 additions & 2 deletions

File tree

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ pub(crate) struct ParseTargetMachineConfig<'a>(pub LlvmError<'a>);
2424

2525
impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> {
2626
fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
27-
let diag: Diag<'_, G> = self.0.into_diag(dcx, level);
27+
// Reuse the formatted primary message from `LlvmError` without emitting it.
28+
let diag: Diag<'_, ()> = self.0.into_diag(dcx, level);
2829
let (message, _) = diag.messages.first().expect("`LlvmError` with no message");
29-
let message = format_diag_message(message, &diag.args);
30+
let message = format_diag_message(message, &diag.args).into_owned();
31+
diag.cancel();
32+
3033
Diag::new(
3134
dcx,
3235
level,

src/tools/tidy/src/ui_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ fn check_unexpected_extension(check: &mut RunningCheck, file_path: &Path, ext: &
220220
"tests/ui/asm/named-asm-labels.s", // loading an external asm file to test named labels lint
221221
"tests/ui/asm/normalize-offsets-for-crlf.s", // loading an external asm file to test CRLF normalization
222222
"tests/ui/codegen/mismatched-data-layout.json", // testing mismatched data layout w/ custom targets
223+
"tests/ui/codegen/custom-target-invalid-llvm-target.json", // testing invalid custom targets
223224
"tests/ui/check-cfg/my-awesome-platform.json", // testing custom targets with cfgs
224225
"tests/ui/argfile/commandline-argfile-badutf8.args", // passing args via a file
225226
"tests/ui/argfile/commandline-argfile.args", // passing args via a file
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"llvm-target": "not-a-real-target",
3+
"data-layout": "e",
4+
"arch": "x86_64",
5+
"target-pointer-width": 64
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Regression test for https://github.com/rust-lang/rust/issues/157401
2+
3+
// ignore-tidy-target-specific-tests
4+
//@ check-fail
5+
//@ compile-flags: --target={{src-base}}/codegen/custom-target-invalid-llvm-target.json -Z unstable-options
6+
//@ ignore-backends: gcc
7+
8+
fn main() {}
9+
10+
//~? ERROR failed to parse target machine config to target machine
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: failed to parse target machine config to target machine: could not create LLVM TargetMachine for triple: not-a-real-target
2+

0 commit comments

Comments
 (0)