diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index 86e20d383b4f9..bcbafab585b40 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -24,9 +24,12 @@ pub(crate) struct ParseTargetMachineConfig<'a>(pub LlvmError<'a>); impl Diagnostic<'_, G> for ParseTargetMachineConfig<'_> { fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> { - let diag: Diag<'_, G> = self.0.into_diag(dcx, level); + // Reuse the formatted primary message from `LlvmError` without emitting it. + let diag: Diag<'_, ()> = self.0.into_diag(dcx, level); let (message, _) = diag.messages.first().expect("`LlvmError` with no message"); - let message = format_diag_message(message, &diag.args); + let message = format_diag_message(message, &diag.args).into_owned(); + diag.cancel(); + Diag::new( dcx, level, diff --git a/src/tools/tidy/src/ui_tests.rs b/src/tools/tidy/src/ui_tests.rs index 50ec411987672..d58adbcb9dd7e 100644 --- a/src/tools/tidy/src/ui_tests.rs +++ b/src/tools/tidy/src/ui_tests.rs @@ -220,6 +220,7 @@ fn check_unexpected_extension(check: &mut RunningCheck, file_path: &Path, ext: & "tests/ui/asm/named-asm-labels.s", // loading an external asm file to test named labels lint "tests/ui/asm/normalize-offsets-for-crlf.s", // loading an external asm file to test CRLF normalization "tests/ui/codegen/mismatched-data-layout.json", // testing mismatched data layout w/ custom targets + "tests/ui/codegen/custom-target-invalid-llvm-target.json", // testing invalid custom targets "tests/ui/check-cfg/my-awesome-platform.json", // testing custom targets with cfgs "tests/ui/argfile/commandline-argfile-badutf8.args", // passing args via a file "tests/ui/argfile/commandline-argfile.args", // passing args via a file diff --git a/tests/ui/codegen/custom-target-invalid-llvm-target.json b/tests/ui/codegen/custom-target-invalid-llvm-target.json new file mode 100644 index 0000000000000..07b20dfc00a3b --- /dev/null +++ b/tests/ui/codegen/custom-target-invalid-llvm-target.json @@ -0,0 +1,6 @@ +{ + "llvm-target": "not-a-real-target", + "data-layout": "e", + "arch": "x86_64", + "target-pointer-width": 64 +} diff --git a/tests/ui/codegen/custom-target-invalid-llvm-target.rs b/tests/ui/codegen/custom-target-invalid-llvm-target.rs new file mode 100644 index 0000000000000..72c80cd7af4f1 --- /dev/null +++ b/tests/ui/codegen/custom-target-invalid-llvm-target.rs @@ -0,0 +1,10 @@ +// Regression test for https://github.com/rust-lang/rust/issues/157401 + +// ignore-tidy-target-specific-tests +//@ check-fail +//@ compile-flags: --target={{src-base}}/codegen/custom-target-invalid-llvm-target.json -Z unstable-options +//@ ignore-backends: gcc + +fn main() {} + +//~? ERROR failed to parse target machine config to target machine diff --git a/tests/ui/codegen/custom-target-invalid-llvm-target.stderr b/tests/ui/codegen/custom-target-invalid-llvm-target.stderr new file mode 100644 index 0000000000000..d5ac437a2b646 --- /dev/null +++ b/tests/ui/codegen/custom-target-invalid-llvm-target.stderr @@ -0,0 +1,2 @@ +error: failed to parse target machine config to target machine: could not create LLVM TargetMachine for triple: not-a-real-target +