Skip to content

Commit 3cf8e1a

Browse files
authored
Unrolled build for #148531
Rollup merge of #148531 - tamird:vendor-enum, r=nnethercote rustc_target: introduce 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 11339a0 + 98a534e commit 3cf8e1a

File tree

249 files changed

+1012
-830
lines changed

Some content is hidden

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

249 files changed

+1012
-830
lines changed

compiler/rustc_codegen_cranelift/src/lib.rs

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

5454
pub use crate::config::*;
5555
use crate::prelude::*;
@@ -163,15 +163,15 @@ impl CodegenBackend for CraneliftCodegenBackend {
163163
fn target_config(&self, sess: &Session) -> TargetConfig {
164164
// FIXME return the actually used target features. this is necessary for #[cfg(target_feature)]
165165
let target_features = match sess.target.arch {
166-
Arch::X86_64 if sess.target.os != "none" => {
166+
Arch::X86_64 if sess.target.os != Os::None => {
167167
// x86_64 mandates SSE2 support and rustc requires the x87 feature to be enabled
168168
vec![sym::fxsr, sym::sse, sym::sse2, Symbol::intern("x87")]
169169
}
170-
Arch::AArch64 => match &*sess.target.os {
171-
"none" => vec![],
170+
Arch::AArch64 => match &sess.target.os {
171+
Os::None => vec![],
172172
// On macOS the aes, sha2 and sha3 features are enabled by default and ring
173173
// fails to compile on macOS when they are not present.
174-
"macos" => vec![sym::neon, sym::aes, sym::sha2, sym::sha3],
174+
Os::MacOs => vec![sym::neon, sym::aes, sym::sha2, sym::sha3],
175175
// AArch64 mandates Neon support
176176
_ => vec![sym::neon],
177177
},
@@ -184,9 +184,9 @@ impl CodegenBackend for CraneliftCodegenBackend {
184184
// targets due to GCC using a different ABI than LLVM. Therefore `f16` and `f128`
185185
// won't be available when using a LLVM-built sysroot.
186186
let has_reliable_f16_f128 = !(sess.target.arch == Arch::X86_64
187-
&& sess.target.os == "windows"
188-
&& sess.target.env == "gnu"
189-
&& sess.target.abi != "llvm");
187+
&& sess.target.os == Os::Windows
188+
&& sess.target.env == Env::Gnu
189+
&& sess.target.abi != Abi::Llvm);
190190

191191
TargetConfig {
192192
target_features,

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

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ use rustc_session::{Session, filesearch};
4646
use rustc_span::Symbol;
4747
use rustc_target::spec::crt_objects::CrtObjects;
4848
use rustc_target::spec::{
49-
BinaryFormat, Cc, LinkOutputKind, LinkSelfContainedComponents, LinkSelfContainedDefault,
50-
LinkerFeatures, LinkerFlavor, LinkerFlavorCli, Lld, RelocModel, RelroLevel, SanitizerSet,
51-
SplitDebuginfo,
49+
Abi, BinaryFormat, Cc, Env, LinkOutputKind, LinkSelfContainedComponents,
50+
LinkSelfContainedDefault, LinkerFeatures, LinkerFlavor, LinkerFlavorCli, Lld, Os, RelocModel,
51+
RelroLevel, SanitizerSet, SplitDebuginfo,
5252
};
5353
use tracing::{debug, info, warn};
5454

@@ -1407,11 +1407,9 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
14071407
Some(LinkerFlavorCli::Llbc) => Some(LinkerFlavor::Llbc),
14081408
Some(LinkerFlavorCli::Ptx) => Some(LinkerFlavor::Ptx),
14091409
// The linker flavors that corresponds to targets needs logic that keeps the base LinkerFlavor
1410-
_ => sess
1411-
.opts
1412-
.cg
1413-
.linker_flavor
1414-
.map(|flavor| sess.target.linker_flavor.with_cli_hints(flavor)),
1410+
linker_flavor => {
1411+
linker_flavor.map(|flavor| sess.target.linker_flavor.with_cli_hints(flavor))
1412+
}
14151413
};
14161414
if let Some(ret) = infer_from(sess, sess.opts.cg.linker.clone(), linker_flavor, features) {
14171415
return ret;
@@ -1819,7 +1817,7 @@ fn self_contained_components(
18191817
LinkSelfContainedDefault::InferredForMusl => sess.crt_static(Some(crate_type)),
18201818
LinkSelfContainedDefault::InferredForMingw => {
18211819
sess.host == sess.target
1822-
&& sess.target.abi != "uwp"
1820+
&& sess.target.abi != Abi::Uwp
18231821
&& detect_self_contained_mingw(sess, linker)
18241822
}
18251823
}
@@ -1845,7 +1843,7 @@ fn add_pre_link_objects(
18451843
let empty = Default::default();
18461844
let objects = if self_contained {
18471845
&opts.pre_link_objects_self_contained
1848-
} else if !(sess.target.os == "fuchsia" && matches!(flavor, LinkerFlavor::Gnu(Cc::Yes, _))) {
1846+
} else if !(sess.target.os == Os::Fuchsia && matches!(flavor, LinkerFlavor::Gnu(Cc::Yes, _))) {
18491847
&opts.pre_link_objects
18501848
} else {
18511849
&empty
@@ -2496,7 +2494,7 @@ fn add_order_independent_options(
24962494

24972495
let apple_sdk_root = add_apple_sdk(cmd, sess, flavor);
24982496

2499-
if sess.target.os == "fuchsia"
2497+
if sess.target.os == Os::Fuchsia
25002498
&& crate_type == CrateType::Executable
25012499
&& !matches!(flavor, LinkerFlavor::Gnu(Cc::Yes, _))
25022500
{
@@ -2515,7 +2513,7 @@ fn add_order_independent_options(
25152513
cmd.no_crt_objects();
25162514
}
25172515

2518-
if sess.target.os == "emscripten" {
2516+
if sess.target.os == Os::Emscripten {
25192517
cmd.cc_arg(if sess.opts.unstable_opts.emscripten_wasm_eh {
25202518
"-fwasm-exceptions"
25212519
} else if sess.panic_strategy().unwinds() {
@@ -3070,8 +3068,8 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
30703068

30713069
// `sess.target.arch` (`target_arch`) is not detailed enough.
30723070
let llvm_arch = sess.target.llvm_target.split_once('-').expect("LLVM target must have arch").0;
3073-
let target_os = &*sess.target.os;
3074-
let target_env = &*sess.target.env;
3071+
let target_os = &sess.target.os;
3072+
let target_env = &sess.target.env;
30753073

30763074
// The architecture name to forward to the linker.
30773075
//
@@ -3123,12 +3121,12 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
31233121
// > - xros-simulator
31243122
// > - driverkit
31253123
let platform_name = match (target_os, target_env) {
3126-
(os, "") => os,
3127-
("ios", "macabi") => "mac-catalyst",
3128-
("ios", "sim") => "ios-simulator",
3129-
("tvos", "sim") => "tvos-simulator",
3130-
("watchos", "sim") => "watchos-simulator",
3131-
("visionos", "sim") => "visionos-simulator",
3124+
(os, Env::Unspecified) => os.desc(),
3125+
(Os::IOs, Env::MacAbi) => "mac-catalyst",
3126+
(Os::IOs, Env::Sim) => "ios-simulator",
3127+
(Os::TvOs, Env::Sim) => "tvos-simulator",
3128+
(Os::WatchOs, Env::Sim) => "watchos-simulator",
3129+
(Os::VisionOs, Env::Sim) => "visionos-simulator",
31323130
_ => bug!("invalid OS/env combination for Apple target: {target_os}, {target_env}"),
31333131
};
31343132

@@ -3192,7 +3190,7 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
31923190
// fairly safely use `-target`. See also the following, where it is
31933191
// made explicit that the recommendation by LLVM developers is to use
31943192
// `-target`: <https://github.com/llvm/llvm-project/issues/88271>
3195-
if target_os == "macos" {
3193+
if *target_os == Os::MacOs {
31963194
// `-arch` communicates the architecture.
31973195
//
31983196
// CC forwards the `-arch` to the linker, so we use the same value

0 commit comments

Comments
 (0)