Skip to content

Commit e6765ba

Browse files
authored
Rollup merge of rust-lang#148531 - tamird:vendor-enum, r=nnethercote
rustc_target: introduce Vendor, Abi, Env, Os Improve type safety by using an enum rather than strings. I'm not really sure this is better since only a few vendors have special semantics. r? ``@nnethercote``
2 parents 8f3e35c + cd47df7 commit e6765ba

File tree

262 files changed

+1132
-904
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

262 files changed

+1132
-904
lines changed

compiler/rustc_codegen_cranelift/src/abi/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
2020
use rustc_session::Session;
2121
use rustc_span::source_map::Spanned;
2222
use rustc_target::callconv::{FnAbi, PassMode};
23-
use rustc_target::spec::Arch;
23+
use rustc_target::spec::{Arch, Vendor};
2424
use smallvec::SmallVec;
2525

2626
use self::pass_mode::*;
@@ -788,8 +788,8 @@ pub(crate) fn codegen_drop<'tcx>(
788788
pub(crate) fn lib_call_arg_param(tcx: TyCtxt<'_>, ty: Type, is_signed: bool) -> AbiParam {
789789
let param = AbiParam::new(ty);
790790
if ty.is_int() && u64::from(ty.bits()) < tcx.data_layout.pointer_size().bits() {
791-
match (&tcx.sess.target.arch, tcx.sess.target.vendor.as_ref()) {
792-
(Arch::X86_64, _) | (Arch::AArch64, "apple") => match (ty, is_signed) {
791+
match (&tcx.sess.target.arch, &tcx.sess.target.vendor) {
792+
(Arch::X86_64, _) | (Arch::AArch64, Vendor::Apple) => match (ty, is_signed) {
793793
(types::I8 | types::I16, true) => param.sext(),
794794
(types::I8 | types::I16, false) => param.uext(),
795795
_ => param,

compiler/rustc_codegen_cranelift/src/codegen_f16_f128.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use rustc_target::spec::Arch;
1+
use rustc_target::spec::{Arch, Vendor};
22

33
use crate::prelude::*;
44

55
pub(crate) fn f16_to_f32(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value {
66
let (value, arg_ty) =
7-
if fx.tcx.sess.target.vendor == "apple" && fx.tcx.sess.target.arch == Arch::X86_64 {
7+
if fx.tcx.sess.target.vendor == Vendor::Apple && fx.tcx.sess.target.arch == Arch::X86_64 {
88
(
99
fx.bcx.ins().bitcast(types::I16, MemFlags::new(), value),
1010
lib_call_arg_param(fx.tcx, types::I16, false),
@@ -21,12 +21,12 @@ fn f16_to_f64(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value {
2121
}
2222

2323
pub(crate) fn f32_to_f16(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value {
24-
let ret_ty = if fx.tcx.sess.target.vendor == "apple" && fx.tcx.sess.target.arch == Arch::X86_64
25-
{
26-
types::I16
27-
} else {
28-
types::F16
29-
};
24+
let ret_ty =
25+
if fx.tcx.sess.target.vendor == Vendor::Apple && fx.tcx.sess.target.arch == Arch::X86_64 {
26+
types::I16
27+
} else {
28+
types::F16
29+
};
3030
let ret = fx.lib_call(
3131
"__truncsfhf2",
3232
vec![AbiParam::new(types::F32)],
@@ -37,12 +37,12 @@ pub(crate) fn f32_to_f16(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value
3737
}
3838

3939
fn f64_to_f16(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value {
40-
let ret_ty = if fx.tcx.sess.target.vendor == "apple" && fx.tcx.sess.target.arch == Arch::X86_64
41-
{
42-
types::I16
43-
} else {
44-
types::F16
45-
};
40+
let ret_ty =
41+
if fx.tcx.sess.target.vendor == Vendor::Apple && fx.tcx.sess.target.arch == Arch::X86_64 {
42+
types::I16
43+
} else {
44+
types::F16
45+
};
4646
let ret = fx.lib_call(
4747
"__truncdfhf2",
4848
vec![AbiParam::new(types::F64)],

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
4747
use rustc_session::Session;
4848
use rustc_session::config::OutputFilenames;
4949
use rustc_span::{Symbol, sym};
50-
use rustc_target::spec::Arch;
50+
use rustc_target::spec::{Abi, Arch, Env, Os};
5151

5252
pub use crate::config::*;
5353
use crate::prelude::*;
@@ -185,15 +185,15 @@ impl CodegenBackend for CraneliftCodegenBackend {
185185
fn target_config(&self, sess: &Session) -> TargetConfig {
186186
// FIXME return the actually used target features. this is necessary for #[cfg(target_feature)]
187187
let target_features = match sess.target.arch {
188-
Arch::X86_64 if sess.target.os != "none" => {
188+
Arch::X86_64 if sess.target.os != Os::None => {
189189
// x86_64 mandates SSE2 support and rustc requires the x87 feature to be enabled
190190
vec![sym::fxsr, sym::sse, sym::sse2, Symbol::intern("x87")]
191191
}
192-
Arch::AArch64 => match &*sess.target.os {
193-
"none" => vec![],
192+
Arch::AArch64 => match &sess.target.os {
193+
Os::None => vec![],
194194
// On macOS the aes, sha2 and sha3 features are enabled by default and ring
195195
// fails to compile on macOS when they are not present.
196-
"macos" => vec![sym::neon, sym::aes, sym::sha2, sym::sha3],
196+
Os::MacOs => vec![sym::neon, sym::aes, sym::sha2, sym::sha3],
197197
// AArch64 mandates Neon support
198198
_ => vec![sym::neon],
199199
},
@@ -214,9 +214,9 @@ impl CodegenBackend for CraneliftCodegenBackend {
214214
// targets due to GCC using a different ABI than LLVM. Therefore `f16` won't be
215215
// available when using a LLVM-built sysroot.
216216
Arch::X86_64
217-
if sess.target.os == "windows"
218-
&& sess.target.env == "gnu"
219-
&& sess.target.abi != "llvm" =>
217+
if sess.target.os == Os::Windows
218+
&& sess.target.env == Env::Gnu
219+
&& sess.target.abi != Abi::Llvm =>
220220
{
221221
false
222222
}

compiler/rustc_codegen_llvm/src/callee.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use rustc_codegen_ssa::common;
88
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, HasTypingEnv};
99
use rustc_middle::ty::{self, Instance, TypeVisitableExt};
10-
use rustc_target::spec::Arch;
10+
use rustc_target::spec::{Arch, Env};
1111
use tracing::debug;
1212

1313
use crate::context::CodegenCx;
@@ -145,7 +145,7 @@ pub(crate) fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'t
145145
if cx.use_dll_storage_attrs
146146
&& let Some(library) = tcx.native_library(instance_def_id)
147147
&& library.kind.is_dllimport()
148-
&& !matches!(tcx.sess.target.env.as_ref(), "gnu" | "uclibc")
148+
&& !matches!(tcx.sess.target.env, Env::Gnu | Env::Uclibc)
149149
{
150150
llvm::set_dllimport_storage_class(llfn);
151151
}

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_span::source_map::Spanned;
2929
use rustc_span::{DUMMY_SP, Span, Symbol};
3030
use rustc_symbol_mangling::mangle_internal_symbol;
3131
use rustc_target::spec::{
32-
Arch, HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel,
32+
Abi, Arch, Env, HasTargetSpec, Os, RelocModel, SmallDataThresholdSupport, Target, TlsModel,
3333
};
3434
use smallvec::SmallVec;
3535

@@ -335,9 +335,9 @@ pub(crate) unsafe fn create_module<'ll>(
335335

336336
// Control Flow Guard is currently only supported by MSVC and LLVM on Windows.
337337
if sess.target.is_like_msvc
338-
|| (sess.target.options.os == "windows"
339-
&& sess.target.options.env == "gnu"
340-
&& sess.target.options.abi == "llvm")
338+
|| (sess.target.options.os == Os::Windows
339+
&& sess.target.options.env == Env::Gnu
340+
&& sess.target.options.abi == Abi::Llvm)
341341
{
342342
match sess.opts.cg.control_flow_guard {
343343
CFGuard::Disabled => {}
@@ -669,7 +669,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
669669
/// This corresponds to the `-fobjc-abi-version=` flag in Clang / GCC.
670670
pub(crate) fn objc_abi_version(&self) -> u32 {
671671
assert!(self.tcx.sess.target.is_like_darwin);
672-
if self.tcx.sess.target.arch == Arch::X86 && self.tcx.sess.target.os == "macos" {
672+
if self.tcx.sess.target.arch == Arch::X86 && self.tcx.sess.target.os == Os::MacOs {
673673
// 32-bit x86 macOS uses ABI version 1 (a.k.a. the "fragile ABI").
674674
1
675675
} else {
@@ -710,7 +710,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
710710
},
711711
);
712712

713-
if self.tcx.sess.target.env == "sim" {
713+
if self.tcx.sess.target.env == Env::Sim {
714714
llvm::add_module_flag_u32(
715715
self.llmod,
716716
llvm::ModuleFlagMergeBehavior::Error,
@@ -963,7 +963,7 @@ impl<'ll> CodegenCx<'ll, '_> {
963963
return eh_catch_typeinfo;
964964
}
965965
let tcx = self.tcx;
966-
assert!(self.sess().target.os == "emscripten");
966+
assert!(self.sess().target.os == Os::Emscripten);
967967
let eh_catch_typeinfo = match tcx.lang_items().eh_catch_typeinfo() {
968968
Some(def_id) => self.get_static(def_id),
969969
_ => {

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_middle::{bug, span_bug};
1818
use rustc_span::{Span, Symbol, sym};
1919
use rustc_symbol_mangling::{mangle_internal_symbol, symbol_name_for_instance_in_crate};
2020
use rustc_target::callconv::PassMode;
21+
use rustc_target::spec::Os;
2122
use tracing::debug;
2223

2324
use crate::abi::FnAbiLlvmExt;
@@ -681,7 +682,7 @@ fn catch_unwind_intrinsic<'ll, 'tcx>(
681682
codegen_msvc_try(bx, try_func, data, catch_func, dest);
682683
} else if wants_wasm_eh(bx.sess()) {
683684
codegen_wasm_try(bx, try_func, data, catch_func, dest);
684-
} else if bx.sess().target.os == "emscripten" {
685+
} else if bx.sess().target.os == Os::Emscripten {
685686
codegen_emcc_try(bx, try_func, data, catch_func, dest);
686687
} else {
687688
codegen_gnu_try(bx, try_func, data, catch_func, dest);

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use rustc_fs_util::path_to_c_string;
1515
use rustc_middle::bug;
1616
use rustc_session::Session;
1717
use rustc_session::config::{PrintKind, PrintRequest};
18-
use rustc_target::spec::{Arch, MergeFunctions, PanicStrategy, SmallDataThresholdSupport};
18+
use rustc_target::spec::{
19+
Abi, Arch, Env, MergeFunctions, Os, PanicStrategy, SmallDataThresholdSupport,
20+
};
1921
use smallvec::{SmallVec, smallvec};
2022

2123
use crate::back::write::create_informational_target_machine;
@@ -104,7 +106,7 @@ unsafe fn configure_llvm(sess: &Session) {
104106
add("-wasm-enable-eh", false);
105107
}
106108

107-
if sess.target.os == "emscripten"
109+
if sess.target.os == Os::Emscripten
108110
&& !sess.opts.unstable_opts.emscripten_wasm_eh
109111
&& sess.panic_strategy().unwinds()
110112
{
@@ -351,9 +353,9 @@ pub(crate) fn target_config(sess: &Session) -> TargetConfig {
351353
/// Determine whether or not experimental float types are reliable based on known bugs.
352354
fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
353355
let target_arch = &sess.target.arch;
354-
let target_os = sess.target.options.os.as_ref();
355-
let target_env = sess.target.options.env.as_ref();
356-
let target_abi = sess.target.options.abi.as_ref();
356+
let target_os = &sess.target.options.os;
357+
let target_env = &sess.target.options.env;
358+
let target_abi = &sess.target.options.abi;
357359
let target_pointer_width = sess.target.pointer_width;
358360
let version = get_version();
359361
let lt_20_1_1 = version < (20, 1, 1);
@@ -371,7 +373,7 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
371373
// Selection failure <https://github.com/llvm/llvm-project/issues/50374> (fixed in llvm21)
372374
(Arch::S390x, _) if lt_21_0_0 => false,
373375
// MinGW ABI bugs <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115054>
374-
(Arch::X86_64, "windows") if target_env == "gnu" && target_abi != "llvm" => false,
376+
(Arch::X86_64, Os::Windows) if *target_env == Env::Gnu && *target_abi != Abi::Llvm => false,
375377
// Infinite recursion <https://github.com/llvm/llvm-project/issues/97981>
376378
(Arch::CSky, _) => false,
377379
(Arch::Hexagon, _) if lt_21_0_0 => false, // (fixed in llvm21)
@@ -403,7 +405,7 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
403405
// not fail if our compiler-builtins is linked. (fixed in llvm21)
404406
(Arch::X86, _) if lt_21_0_0 => false,
405407
// MinGW ABI bugs <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115054>
406-
(Arch::X86_64, "windows") if target_env == "gnu" && target_abi != "llvm" => false,
408+
(Arch::X86_64, Os::Windows) if *target_env == Env::Gnu && *target_abi != Abi::Llvm => false,
407409
// There are no known problems on other platforms, so the only requirement is that symbols
408410
// are available. `compiler-builtins` provides all symbols required for core `f128`
409411
// support, so this should work for everything else.
@@ -424,9 +426,9 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
424426
// (ld is 80-bit extended precision).
425427
//
426428
// musl does not implement the symbols required for f128 math at all.
427-
_ if target_env == "musl" => false,
429+
_ if *target_env == Env::Musl => false,
428430
(Arch::X86_64, _) => false,
429-
(_, "linux") if target_pointer_width == 64 => true,
431+
(_, Os::Linux) if target_pointer_width == 64 => true,
430432
_ => false,
431433
} && cfg.has_reliable_f128;
432434
}

compiler/rustc_codegen_llvm/src/va_arg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_codegen_ssa::traits::{
77
};
88
use rustc_middle::ty::Ty;
99
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
10-
use rustc_target::spec::Arch;
10+
use rustc_target::spec::{Abi, Arch};
1111

1212
use crate::builder::Builder;
1313
use crate::llvm::{Type, Value};
@@ -270,7 +270,7 @@ fn emit_powerpc_va_arg<'ll, 'tcx>(
270270

271271
// Rust does not currently support any powerpc softfloat targets.
272272
let target = &bx.cx.tcx.sess.target;
273-
let is_soft_float_abi = target.abi == "softfloat";
273+
let is_soft_float_abi = target.abi == Abi::SoftFloat;
274274
assert!(!is_soft_float_abi);
275275

276276
// All instances of VaArgSafe are passed directly.

compiler/rustc_codegen_ssa/src/back/apple.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use itertools::Itertools;
66
use rustc_middle::middle::exported_symbols::SymbolExportKind;
77
use rustc_session::Session;
88
pub(super) use rustc_target::spec::apple::OSVersion;
9-
use rustc_target::spec::{Arch, Target};
9+
use rustc_target::spec::{Arch, Env, Os, Target};
1010
use tracing::debug;
1111

1212
use crate::errors::{XcrunError, XcrunSdkPathWarning};
@@ -17,35 +17,35 @@ mod tests;
1717

1818
/// The canonical name of the desired SDK for a given target.
1919
pub(super) fn sdk_name(target: &Target) -> &'static str {
20-
match (&*target.os, &*target.env) {
21-
("macos", "") => "MacOSX",
22-
("ios", "") => "iPhoneOS",
23-
("ios", "sim") => "iPhoneSimulator",
20+
match (&target.os, &target.env) {
21+
(Os::MacOs, Env::Unspecified) => "MacOSX",
22+
(Os::IOs, Env::Unspecified) => "iPhoneOS",
23+
(Os::IOs, Env::Sim) => "iPhoneSimulator",
2424
// Mac Catalyst uses the macOS SDK
25-
("ios", "macabi") => "MacOSX",
26-
("tvos", "") => "AppleTVOS",
27-
("tvos", "sim") => "AppleTVSimulator",
28-
("visionos", "") => "XROS",
29-
("visionos", "sim") => "XRSimulator",
30-
("watchos", "") => "WatchOS",
31-
("watchos", "sim") => "WatchSimulator",
25+
(Os::IOs, Env::MacAbi) => "MacOSX",
26+
(Os::TvOs, Env::Unspecified) => "AppleTVOS",
27+
(Os::TvOs, Env::Sim) => "AppleTVSimulator",
28+
(Os::VisionOs, Env::Unspecified) => "XROS",
29+
(Os::VisionOs, Env::Sim) => "XRSimulator",
30+
(Os::WatchOs, Env::Unspecified) => "WatchOS",
31+
(Os::WatchOs, Env::Sim) => "WatchSimulator",
3232
(os, abi) => unreachable!("invalid os '{os}' / abi '{abi}' combination for Apple target"),
3333
}
3434
}
3535

3636
pub(super) fn macho_platform(target: &Target) -> u32 {
37-
match (&*target.os, &*target.env) {
38-
("macos", _) => object::macho::PLATFORM_MACOS,
39-
("ios", "macabi") => object::macho::PLATFORM_MACCATALYST,
40-
("ios", "sim") => object::macho::PLATFORM_IOSSIMULATOR,
41-
("ios", _) => object::macho::PLATFORM_IOS,
42-
("watchos", "sim") => object::macho::PLATFORM_WATCHOSSIMULATOR,
43-
("watchos", _) => object::macho::PLATFORM_WATCHOS,
44-
("tvos", "sim") => object::macho::PLATFORM_TVOSSIMULATOR,
45-
("tvos", _) => object::macho::PLATFORM_TVOS,
46-
("visionos", "sim") => object::macho::PLATFORM_XROSSIMULATOR,
47-
("visionos", _) => object::macho::PLATFORM_XROS,
48-
_ => unreachable!("tried to get Mach-O platform for non-Apple target"),
37+
match (&target.os, &target.env) {
38+
(Os::MacOs, _) => object::macho::PLATFORM_MACOS,
39+
(Os::IOs, Env::MacAbi) => object::macho::PLATFORM_MACCATALYST,
40+
(Os::IOs, Env::Sim) => object::macho::PLATFORM_IOSSIMULATOR,
41+
(Os::IOs, _) => object::macho::PLATFORM_IOS,
42+
(Os::WatchOs, Env::Sim) => object::macho::PLATFORM_WATCHOSSIMULATOR,
43+
(Os::WatchOs, _) => object::macho::PLATFORM_WATCHOS,
44+
(Os::TvOs, Env::Sim) => object::macho::PLATFORM_TVOSSIMULATOR,
45+
(Os::TvOs, _) => object::macho::PLATFORM_TVOS,
46+
(Os::VisionOs, Env::Sim) => object::macho::PLATFORM_XROSSIMULATOR,
47+
(Os::VisionOs, _) => object::macho::PLATFORM_XROS,
48+
(os, env) => unreachable!("invalid os '{os}' / env '{env}' combination for Apple target"),
4949
}
5050
}
5151

0 commit comments

Comments
 (0)