Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b780fa9

Browse files
committed
Use an error struct instead of a panic
1 parent 518becf commit b780fa9

File tree

10 files changed

+47
-5
lines changed

10 files changed

+47
-5
lines changed

compiler/rustc_codegen_llvm/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ codegen_llvm_error_creating_import_library =
1818
codegen_llvm_error_writing_def_file =
1919
Error writing .DEF file: {$error}
2020
21+
codegen_llvm_fixed_x18_invalid_arch = the `-Zfixed-x18` flag is not supported on the `{$arch}` architecture
22+
2123
codegen_llvm_from_llvm_diag = {$message}
2224
2325
codegen_llvm_from_llvm_optimization_diag = {$filename}:{$line}:{$column} {$pass_name} ({$kind}): {$message}

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,9 @@ pub struct MismatchedDataLayout<'a> {
254254
pub(crate) struct InvalidTargetFeaturePrefix<'a> {
255255
pub feature: &'a str,
256256
}
257+
258+
#[derive(Diagnostic)]
259+
#[diag(codegen_llvm_fixed_x18_invalid_arch)]
260+
pub(crate) struct FixedX18InvalidArch<'a> {
261+
pub arch: &'a str,
262+
}

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::back::write::create_informational_target_machine;
22
use crate::errors::{
3-
InvalidTargetFeaturePrefix, PossibleFeature, TargetFeatureDisableOrEnable,
3+
FixedX18InvalidArch, InvalidTargetFeaturePrefix, PossibleFeature, TargetFeatureDisableOrEnable,
44
UnknownCTargetFeature, UnknownCTargetFeaturePrefix, UnstableCTargetFeature,
55
};
66
use crate::llvm;
@@ -618,11 +618,10 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<Str
618618
// -Zfixed-x18
619619
if sess.opts.unstable_opts.fixed_x18 {
620620
if sess.target.arch != "aarch64" {
621-
// TODO: What's the correct way to return a error here?
622-
panic!("-Zfixed-x18 only allowed on aarch64");
621+
sess.dcx().emit_fatal(FixedX18InvalidArch { arch: &sess.target.arch });
622+
} else {
623+
features.push("+reserve-x18".into());
623624
}
624-
625-
features.push("+reserve-x18".into());
626625
}
627626

628627
if diagnostics && let Some(f) = check_tied_features(sess, &featsmap) {

tests/ui/abi/fixed_x18.aarch64.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: the `-Zfixed-x18` flag is not supported on the `x86_64` architecture
2+

tests/ui/abi/fixed_x18.arm.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: the `-Zfixed-x18` flag is not supported on the `arm` architecture
2+

tests/ui/abi/fixed_x18.i686.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: the `-Zfixed-x18` flag is not supported on the `x86` architecture
2+

tests/ui/abi/fixed_x18.riscv32.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: the `-Zfixed-x18` flag is not supported on the `riscv32` architecture
2+

tests/ui/abi/fixed_x18.riscv64.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: the `-Zfixed-x18` flag is not supported on the `riscv64` architecture
2+

tests/ui/abi/fixed_x18.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This tests that -Zfixed-x18 causes a compilation failure on targets other than aarch64.
2+
// Behavior on aarch64 is tested by tests/codegen/fixed-x18.rs.
3+
//
4+
//@ revisions: x64 i686 aarch64 arm riscv32 riscv64
5+
//
6+
//@ compile-flags: -Zfixed-x18
7+
//@ [x64] needs-llvm-components: x86
8+
//@ [x64] compile-flags: --target=x86_64-unknown-linux-gnu --crate-type=rlib
9+
//@ [i686] needs-llvm-components: x86
10+
//@ [i686] compile-flags: --target=i686-unknown-linux-gnu --crate-type=rlib
11+
//@ [arm] needs-llvm-components: arm
12+
//@ [arm] compile-flags: --target=armv7-unknown-linux-gnueabihf --crate-type=rlib
13+
//@ [riscv32] needs-llvm-components: riscv
14+
//@ [riscv32] compile-flags: --target=riscv32i-unknown-none-elf --crate-type=rlib
15+
//@ [riscv64] needs-llvm-components: riscv
16+
//@ [riscv64] compile-flags: --target=riscv64gc-unknown-none-elf --crate-type=rlib
17+
18+
#![crate_type = "lib"]
19+
#![feature(no_core, lang_items)]
20+
#![no_core]
21+
22+
#[lang = "sized"]
23+
trait Sized {}

tests/ui/abi/fixed_x18.x64.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: the `-Zfixed-x18` flag is not supported on the `x86_64` architecture
2+

0 commit comments

Comments
 (0)