Skip to content

Commit 1151ddb

Browse files
committed
rustc_target: introduce Env
Improve type safety by using an enum rather than strings.
1 parent 20ab7b3 commit 1151ddb

Some content is hidden

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

56 files changed

+202
-159
lines changed

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 2 additions & 2 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::{ABI, Arch};
50+
use rustc_target::spec::{ABI, Arch, Env};
5151

5252
pub use crate::config::*;
5353
use crate::prelude::*;
@@ -215,7 +215,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
215215
// available when using a LLVM-built sysroot.
216216
Arch::X86_64
217217
if sess.target.os == "windows"
218-
&& sess.target.env == "gnu"
218+
&& sess.target.env == Env::Gnu
219219
&& sess.target.abi != ABI::Llvm =>
220220
{
221221
false

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: 3 additions & 3 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-
ABI, Arch, HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel,
32+
ABI, Arch, Env, HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel,
3333
};
3434
use smallvec::SmallVec;
3535

@@ -336,7 +336,7 @@ pub(crate) unsafe fn create_module<'ll>(
336336
// Control Flow Guard is currently only supported by MSVC and LLVM on Windows.
337337
if sess.target.is_like_msvc
338338
|| (sess.target.options.os == "windows"
339-
&& sess.target.options.env == "gnu"
339+
&& sess.target.options.env == Env::Gnu
340340
&& sess.target.options.abi == ABI::Llvm)
341341
{
342342
match sess.opts.cg.control_flow_guard {
@@ -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,

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 7 additions & 5 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::{ABI, Arch, MergeFunctions, PanicStrategy, SmallDataThresholdSupport};
18+
use rustc_target::spec::{
19+
ABI, Arch, Env, MergeFunctions, PanicStrategy, SmallDataThresholdSupport,
20+
};
1921
use smallvec::{SmallVec, smallvec};
2022

2123
use crate::back::write::create_informational_target_machine;
@@ -352,7 +354,7 @@ pub(crate) fn target_config(sess: &Session) -> TargetConfig {
352354
fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
353355
let target_arch = &sess.target.arch;
354356
let target_os = sess.target.options.os.as_ref();
355-
let target_env = sess.target.options.env.as_ref();
357+
let target_env = &sess.target.options.env;
356358
let target_abi = &sess.target.options.abi;
357359
let target_pointer_width = sess.target.pointer_width;
358360
let version = get_version();
@@ -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 != ABI::Llvm => false,
376+
(Arch::X86_64, "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 != ABI::Llvm => false,
408+
(Arch::X86_64, "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,7 +426,7 @@ 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,
429431
(_, "linux") if target_pointer_width == 64 => true,
430432
_ => false,

compiler/rustc_codegen_ssa/src/back/apple.rs

Lines changed: 19 additions & 19 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, 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+
("macos", Env::Unspecified) => "MacOSX",
22+
("ios", Env::Unspecified) => "iPhoneOS",
23+
("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+
("ios", Env::MacAbi) => "MacOSX",
26+
("tvos", Env::Unspecified) => "AppleTVOS",
27+
("tvos", Env::Sim) => "AppleTVSimulator",
28+
("visionos", Env::Unspecified) => "XROS",
29+
("visionos", Env::Sim) => "XRSimulator",
30+
("watchos", Env::Unspecified) => "WatchOS",
31+
("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) {
37+
match (&*target.os, &target.env) {
3838
("macos", _) => object::macho::PLATFORM_MACOS,
39-
("ios", "macabi") => object::macho::PLATFORM_MACCATALYST,
40-
("ios", "sim") => object::macho::PLATFORM_IOSSIMULATOR,
39+
("ios", Env::MacAbi) => object::macho::PLATFORM_MACCATALYST,
40+
("ios", Env::Sim) => object::macho::PLATFORM_IOSSIMULATOR,
4141
("ios", _) => object::macho::PLATFORM_IOS,
42-
("watchos", "sim") => object::macho::PLATFORM_WATCHOSSIMULATOR,
42+
("watchos", Env::Sim) => object::macho::PLATFORM_WATCHOSSIMULATOR,
4343
("watchos", _) => object::macho::PLATFORM_WATCHOS,
44-
("tvos", "sim") => object::macho::PLATFORM_TVOSSIMULATOR,
44+
("tvos", Env::Sim) => object::macho::PLATFORM_TVOSSIMULATOR,
4545
("tvos", _) => object::macho::PLATFORM_TVOS,
46-
("visionos", "sim") => object::macho::PLATFORM_XROSSIMULATOR,
46+
("visionos", Env::Sim) => object::macho::PLATFORM_XROSSIMULATOR,
4747
("visionos", _) => object::macho::PLATFORM_XROS,
48-
_ => unreachable!("tried to get Mach-O platform for non-Apple target"),
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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ 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,
49+
BinaryFormat, Cc, Env, LinkOutputKind, LinkSelfContainedComponents, LinkSelfContainedDefault,
5050
LinkerFeatures, LinkerFlavor, LinkerFlavorCli, Lld, RelocModel, RelroLevel, SanitizerSet,
5151
SplitDebuginfo, Vendor,
5252
};
@@ -3072,7 +3072,7 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
30723072
// `sess.target.arch` (`target_arch`) is not detailed enough.
30733073
let llvm_arch = sess.target.llvm_target.split_once('-').expect("LLVM target must have arch").0;
30743074
let target_os = &*sess.target.os;
3075-
let target_env = &*sess.target.env;
3075+
let target_env = &sess.target.env;
30763076

30773077
// The architecture name to forward to the linker.
30783078
//
@@ -3124,12 +3124,12 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
31243124
// > - xros-simulator
31253125
// > - driverkit
31263126
let platform_name = match (target_os, target_env) {
3127-
(os, "") => os,
3128-
("ios", "macabi") => "mac-catalyst",
3129-
("ios", "sim") => "ios-simulator",
3130-
("tvos", "sim") => "tvos-simulator",
3131-
("watchos", "sim") => "watchos-simulator",
3132-
("visionos", "sim") => "visionos-simulator",
3127+
(os, Env::Unspecified) => os,
3128+
("ios", Env::MacAbi) => "mac-catalyst",
3129+
("ios", Env::Sim) => "ios-simulator",
3130+
("tvos", Env::Sim) => "tvos-simulator",
3131+
("watchos", Env::Sim) => "watchos-simulator",
3132+
("visionos", Env::Sim) => "visionos-simulator",
31333133
_ => bug!("invalid OS/env combination for Apple target: {target_os}, {target_env}"),
31343134
};
31353135

compiler/rustc_codegen_ssa/src/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::ty::{self, Instance, TyCtxt};
77
use rustc_middle::{bug, mir, span_bug};
88
use rustc_session::cstore::{DllCallingConvention, DllImport};
99
use rustc_span::Span;
10-
use rustc_target::spec::{ABI, Target, Vendor};
10+
use rustc_target::spec::{ABI, Env, Target, Vendor};
1111

1212
use crate::traits::*;
1313

@@ -173,7 +173,7 @@ pub fn asm_const_to_str<'tcx>(
173173
pub fn is_mingw_gnu_toolchain(target: &Target) -> bool {
174174
target.vendor == Vendor::Pc
175175
&& target.os == "windows"
176-
&& target.env == "gnu"
176+
&& target.env == Env::Gnu
177177
&& target.abi == ABI::Unspecified
178178
}
179179

compiler/rustc_metadata/src/native_libs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_session::cstore::{DllCallingConvention, DllImport, ForeignModule, Nati
1515
use rustc_session::search_paths::PathKind;
1616
use rustc_span::Symbol;
1717
use rustc_span::def_id::{DefId, LOCAL_CRATE};
18-
use rustc_target::spec::{Arch, BinaryFormat, LinkSelfContainedComponents, Vendor};
18+
use rustc_target::spec::{Arch, BinaryFormat, Env, LinkSelfContainedComponents, Vendor};
1919

2020
use crate::errors;
2121

@@ -79,7 +79,7 @@ pub fn walk_native_lib_search_dirs<R>(
7979
// Mac Catalyst uses the macOS SDK, but to link to iOS-specific frameworks
8080
// we must have the support library stubs in the library search path (#121430).
8181
if let Some(sdk_root) = apple_sdk_root
82-
&& sess.target.env == "macabi"
82+
&& sess.target.env == Env::MacAbi
8383
{
8484
f(&sdk_root.join("System/iOSSupport/usr/lib"), false)?;
8585
f(&sdk_root.join("System/iOSSupport/System/Library/Frameworks"), true)?;

compiler/rustc_session/src/config/cfg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ pub(crate) fn default_configuration(sess: &Session) -> Cfg {
242242
ins_sym!(sym::target_abi, sess.target.abi.desc_symbol());
243243
ins_sym!(sym::target_arch, sess.target.arch.desc_symbol());
244244
ins_str!(sym::target_endian, sess.target.endian.as_str());
245-
ins_str!(sym::target_env, &sess.target.env);
245+
ins_sym!(sym::target_env, sess.target.env.desc_symbol());
246246

247247
for family in sess.target.families.as_ref() {
248248
ins_str!(sym::target_family, family);
@@ -450,7 +450,7 @@ impl CheckCfg {
450450
values_target_abi.insert(target.options.abi.desc_symbol());
451451
values_target_arch.insert(target.arch.desc_symbol());
452452
values_target_endian.insert(Symbol::intern(target.options.endian.as_str()));
453-
values_target_env.insert(Symbol::intern(&target.options.env));
453+
values_target_env.insert(target.options.env.desc_symbol());
454454
values_target_family.extend(
455455
target.options.families.iter().map(|family| Symbol::intern(family)),
456456
);

compiler/rustc_target/src/asm/aarch64.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_data_structures::fx::FxIndexSet;
44
use rustc_span::{Symbol, sym};
55

66
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
7-
use crate::spec::{RelocModel, Target};
7+
use crate::spec::{Env, RelocModel, Target};
88

99
def_reg_class! {
1010
AArch64 AArch64InlineAsmRegClass {
@@ -77,7 +77,7 @@ pub(crate) fn target_reserves_x18(target: &Target, target_features: &FxIndexSet<
7777
// Note that +reserve-x18 is currently not set for the above targets.
7878
target.os == "android"
7979
|| target.os == "fuchsia"
80-
|| target.env == "ohos"
80+
|| target.env == Env::Ohos
8181
|| target.is_like_darwin
8282
|| target.is_like_windows
8383
|| target_features.contains(&sym::reserve_x18)

0 commit comments

Comments
 (0)