From ddd75964009962e6b24096402e1baecbaf5bc395 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Wed, 5 Nov 2025 12:37:09 -0500 Subject: [PATCH] rustc_target: introduce Env Improve type safety by using an enum rather than strings. --- compiler/rustc_codegen_cranelift/src/lib.rs | 4 +- compiler/rustc_codegen_llvm/src/callee.rs | 4 +- compiler/rustc_codegen_llvm/src/context.rs | 6 +-- compiler/rustc_codegen_llvm/src/llvm_util.rs | 12 +++--- compiler/rustc_codegen_ssa/src/back/apple.rs | 38 +++++++++--------- compiler/rustc_codegen_ssa/src/back/link.rs | 20 +++++----- compiler/rustc_codegen_ssa/src/common.rs | 4 +- compiler/rustc_metadata/src/native_libs.rs | 4 +- compiler/rustc_session/src/config/cfg.rs | 4 +- compiler/rustc_target/src/asm/aarch64.rs | 4 +- compiler/rustc_target/src/callconv/powerpc.rs | 4 +- .../rustc_target/src/callconv/powerpc64.rs | 4 +- compiler/rustc_target/src/callconv/s390x.rs | 4 +- compiler/rustc_target/src/callconv/sparc64.rs | 4 +- .../rustc_target/src/spec/base/apple/mod.rs | 26 ++++++------ .../rustc_target/src/spec/base/apple/tests.rs | 4 +- .../rustc_target/src/spec/base/hurd_gnu.rs | 4 +- compiler/rustc_target/src/spec/base/l4re.rs | 4 +- .../rustc_target/src/spec/base/linux_gnu.rs | 4 +- .../rustc_target/src/spec/base/linux_musl.rs | 4 +- .../rustc_target/src/spec/base/linux_ohos.rs | 4 +- .../src/spec/base/linux_uclibc.rs | 4 +- .../rustc_target/src/spec/base/linux_wasm.rs | 6 +-- .../src/spec/base/managarm_mlibc.rs | 4 +- compiler/rustc_target/src/spec/base/redox.rs | 4 +- .../src/spec/base/unikraft_linux_musl.rs | 4 +- .../rustc_target/src/spec/base/vxworks.rs | 4 +- .../rustc_target/src/spec/base/windows_gnu.rs | 6 +-- .../src/spec/base/windows_gnullvm.rs | 5 ++- .../src/spec/base/windows_msvc.rs | 4 +- compiler/rustc_target/src/spec/json.rs | 4 +- compiler/rustc_target/src/spec/mod.rs | 40 +++++++++++++++++-- .../targets/aarch64_unknown_nto_qnx700.rs | 4 +- .../targets/aarch64_unknown_nto_qnx710.rs | 4 +- .../aarch64_unknown_nto_qnx710_iosock.rs | 4 +- .../targets/aarch64_unknown_nto_qnx800.rs | 4 +- .../src/spec/targets/armv6k_nintendo_3ds.rs | 6 +-- .../src/spec/targets/armv7_rtems_eabihf.rs | 6 +-- .../targets/armv7_sony_vita_newlibeabihf.rs | 6 +-- .../src/spec/targets/armv7a_vex_v5.rs | 6 +-- .../src/spec/targets/i686_pc_nto_qnx700.rs | 4 +- .../spec/targets/riscv32imac_esp_espidf.rs | 6 ++- .../spec/targets/riscv32imafc_esp_espidf.rs | 6 ++- .../src/spec/targets/riscv32imc_esp_espidf.rs | 6 ++- .../src/spec/targets/wasm32_wasip1.rs | 5 ++- .../src/spec/targets/wasm32_wasip1_threads.rs | 5 ++- .../src/spec/targets/wasm32_wasip2.rs | 4 +- .../src/spec/targets/wasm32_wasip3.rs | 4 +- .../targets/x86_64_fortanix_unknown_sgx.rs | 6 ++- .../src/spec/targets/x86_64_pc_nto_qnx710.rs | 4 +- .../targets/x86_64_pc_nto_qnx710_iosock.rs | 4 +- .../src/spec/targets/x86_64_pc_nto_qnx800.rs | 4 +- .../src/spec/targets/xtensa_esp32_espidf.rs | 4 +- .../src/spec/targets/xtensa_esp32s2_espidf.rs | 4 +- .../src/spec/targets/xtensa_esp32s3_espidf.rs | 4 +- .../miri/src/shims/windows/foreign_items.rs | 4 +- 56 files changed, 205 insertions(+), 160 deletions(-) diff --git a/compiler/rustc_codegen_cranelift/src/lib.rs b/compiler/rustc_codegen_cranelift/src/lib.rs index 19058dac832f..2175869085a3 100644 --- a/compiler/rustc_codegen_cranelift/src/lib.rs +++ b/compiler/rustc_codegen_cranelift/src/lib.rs @@ -49,7 +49,7 @@ use rustc_middle::dep_graph::{WorkProduct, WorkProductId}; use rustc_session::Session; use rustc_session::config::OutputFilenames; use rustc_span::{Symbol, sym}; -use rustc_target::spec::{Abi, Arch}; +use rustc_target::spec::{Abi, Arch, Env}; pub use crate::config::*; use crate::prelude::*; @@ -185,7 +185,7 @@ impl CodegenBackend for CraneliftCodegenBackend { // won't be available when using a LLVM-built sysroot. let has_reliable_f16_f128 = !(sess.target.arch == Arch::X86_64 && sess.target.os == "windows" - && sess.target.env == "gnu" + && sess.target.env == Env::Gnu && sess.target.abi != Abi::Llvm); TargetConfig { diff --git a/compiler/rustc_codegen_llvm/src/callee.rs b/compiler/rustc_codegen_llvm/src/callee.rs index f0d4c546e986..9215273eed17 100644 --- a/compiler/rustc_codegen_llvm/src/callee.rs +++ b/compiler/rustc_codegen_llvm/src/callee.rs @@ -7,7 +7,7 @@ use rustc_codegen_ssa::common; use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, HasTypingEnv}; use rustc_middle::ty::{self, Instance, TypeVisitableExt}; -use rustc_target::spec::Arch; +use rustc_target::spec::{Arch, Env}; use tracing::debug; use crate::context::CodegenCx; @@ -145,7 +145,7 @@ pub(crate) fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'t if cx.use_dll_storage_attrs && let Some(library) = tcx.native_library(instance_def_id) && library.kind.is_dllimport() - && !matches!(tcx.sess.target.env.as_ref(), "gnu" | "uclibc") + && !matches!(tcx.sess.target.env, Env::Gnu | Env::Uclibc) { llvm::set_dllimport_storage_class(llfn); } diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index 3444ed649f1d..ff29c18c56bb 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -29,7 +29,7 @@ use rustc_span::source_map::Spanned; use rustc_span::{DUMMY_SP, Span, Symbol}; use rustc_symbol_mangling::mangle_internal_symbol; use rustc_target::spec::{ - Abi, Arch, HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel, + Abi, Arch, Env, HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel, }; use smallvec::SmallVec; @@ -336,7 +336,7 @@ pub(crate) unsafe fn create_module<'ll>( // Control Flow Guard is currently only supported by MSVC and LLVM on Windows. if sess.target.is_like_msvc || (sess.target.options.os == "windows" - && sess.target.options.env == "gnu" + && sess.target.options.env == Env::Gnu && sess.target.options.abi == Abi::Llvm) { match sess.opts.cg.control_flow_guard { @@ -710,7 +710,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { }, ); - if self.tcx.sess.target.env == "sim" { + if self.tcx.sess.target.env == Env::Sim { llvm::add_module_flag_u32( self.llmod, llvm::ModuleFlagMergeBehavior::Error, diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index e0884c9c1538..ea0106ce83c7 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -15,7 +15,9 @@ use rustc_fs_util::path_to_c_string; use rustc_middle::bug; use rustc_session::Session; use rustc_session::config::{PrintKind, PrintRequest}; -use rustc_target::spec::{Abi, Arch, MergeFunctions, PanicStrategy, SmallDataThresholdSupport}; +use rustc_target::spec::{ + Abi, Arch, Env, MergeFunctions, PanicStrategy, SmallDataThresholdSupport, +}; use smallvec::{SmallVec, smallvec}; use crate::back::write::create_informational_target_machine; @@ -352,7 +354,7 @@ pub(crate) fn target_config(sess: &Session) -> TargetConfig { fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) { let target_arch = &sess.target.arch; let target_os = sess.target.options.os.as_ref(); - let target_env = sess.target.options.env.as_ref(); + let target_env = &sess.target.options.env; let target_abi = &sess.target.options.abi; let target_pointer_width = sess.target.pointer_width; let version = get_version(); @@ -371,7 +373,7 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) { // Selection failure (fixed in llvm21) (Arch::S390x, _) if lt_21_0_0 => false, // MinGW ABI bugs - (Arch::X86_64, "windows") if target_env == "gnu" && *target_abi != Abi::Llvm => false, + (Arch::X86_64, "windows") if *target_env == Env::Gnu && *target_abi != Abi::Llvm => false, // Infinite recursion (Arch::CSky, _) => false, (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) { // not fail if our compiler-builtins is linked. (fixed in llvm21) (Arch::X86, _) if lt_21_0_0 => false, // MinGW ABI bugs - (Arch::X86_64, "windows") if target_env == "gnu" && *target_abi != Abi::Llvm => false, + (Arch::X86_64, "windows") if *target_env == Env::Gnu && *target_abi != Abi::Llvm => false, // There are no known problems on other platforms, so the only requirement is that symbols // are available. `compiler-builtins` provides all symbols required for core `f128` // support, so this should work for everything else. @@ -424,7 +426,7 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) { // (ld is 80-bit extended precision). // // musl does not implement the symbols required for f128 math at all. - _ if target_env == "musl" => false, + _ if *target_env == Env::Musl => false, (Arch::X86_64, _) => false, (_, "linux") if target_pointer_width == 64 => true, _ => false, diff --git a/compiler/rustc_codegen_ssa/src/back/apple.rs b/compiler/rustc_codegen_ssa/src/back/apple.rs index 3b29ddeadc7f..09dd9170f756 100644 --- a/compiler/rustc_codegen_ssa/src/back/apple.rs +++ b/compiler/rustc_codegen_ssa/src/back/apple.rs @@ -6,7 +6,7 @@ use itertools::Itertools; use rustc_middle::middle::exported_symbols::SymbolExportKind; use rustc_session::Session; pub(super) use rustc_target::spec::apple::OSVersion; -use rustc_target::spec::{Arch, Target}; +use rustc_target::spec::{Arch, Env, Target}; use tracing::debug; use crate::errors::{XcrunError, XcrunSdkPathWarning}; @@ -17,35 +17,35 @@ mod tests; /// The canonical name of the desired SDK for a given target. pub(super) fn sdk_name(target: &Target) -> &'static str { - match (&*target.os, &*target.env) { - ("macos", "") => "MacOSX", - ("ios", "") => "iPhoneOS", - ("ios", "sim") => "iPhoneSimulator", + match (&*target.os, &target.env) { + ("macos", Env::Unspecified) => "MacOSX", + ("ios", Env::Unspecified) => "iPhoneOS", + ("ios", Env::Sim) => "iPhoneSimulator", // Mac Catalyst uses the macOS SDK - ("ios", "macabi") => "MacOSX", - ("tvos", "") => "AppleTVOS", - ("tvos", "sim") => "AppleTVSimulator", - ("visionos", "") => "XROS", - ("visionos", "sim") => "XRSimulator", - ("watchos", "") => "WatchOS", - ("watchos", "sim") => "WatchSimulator", + ("ios", Env::MacAbi) => "MacOSX", + ("tvos", Env::Unspecified) => "AppleTVOS", + ("tvos", Env::Sim) => "AppleTVSimulator", + ("visionos", Env::Unspecified) => "XROS", + ("visionos", Env::Sim) => "XRSimulator", + ("watchos", Env::Unspecified) => "WatchOS", + ("watchos", Env::Sim) => "WatchSimulator", (os, abi) => unreachable!("invalid os '{os}' / abi '{abi}' combination for Apple target"), } } pub(super) fn macho_platform(target: &Target) -> u32 { - match (&*target.os, &*target.env) { + match (&*target.os, &target.env) { ("macos", _) => object::macho::PLATFORM_MACOS, - ("ios", "macabi") => object::macho::PLATFORM_MACCATALYST, - ("ios", "sim") => object::macho::PLATFORM_IOSSIMULATOR, + ("ios", Env::MacAbi) => object::macho::PLATFORM_MACCATALYST, + ("ios", Env::Sim) => object::macho::PLATFORM_IOSSIMULATOR, ("ios", _) => object::macho::PLATFORM_IOS, - ("watchos", "sim") => object::macho::PLATFORM_WATCHOSSIMULATOR, + ("watchos", Env::Sim) => object::macho::PLATFORM_WATCHOSSIMULATOR, ("watchos", _) => object::macho::PLATFORM_WATCHOS, - ("tvos", "sim") => object::macho::PLATFORM_TVOSSIMULATOR, + ("tvos", Env::Sim) => object::macho::PLATFORM_TVOSSIMULATOR, ("tvos", _) => object::macho::PLATFORM_TVOS, - ("visionos", "sim") => object::macho::PLATFORM_XROSSIMULATOR, + ("visionos", Env::Sim) => object::macho::PLATFORM_XROSSIMULATOR, ("visionos", _) => object::macho::PLATFORM_XROS, - _ => unreachable!("tried to get Mach-O platform for non-Apple target"), + (os, env) => unreachable!("invalid os '{os}' / env '{env}' combination for Apple target"), } } diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 33b24aa519bf..d01bcfe97118 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -46,9 +46,9 @@ use rustc_session::{Session, filesearch}; use rustc_span::Symbol; use rustc_target::spec::crt_objects::CrtObjects; use rustc_target::spec::{ - Abi, BinaryFormat, Cc, LinkOutputKind, LinkSelfContainedComponents, LinkSelfContainedDefault, - LinkerFeatures, LinkerFlavor, LinkerFlavorCli, Lld, RelocModel, RelroLevel, SanitizerSet, - SplitDebuginfo, + Abi, BinaryFormat, Cc, Env, LinkOutputKind, LinkSelfContainedComponents, + LinkSelfContainedDefault, LinkerFeatures, LinkerFlavor, LinkerFlavorCli, Lld, RelocModel, + RelroLevel, SanitizerSet, SplitDebuginfo, }; use tracing::{debug, info, warn}; @@ -3071,7 +3071,7 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo // `sess.target.arch` (`target_arch`) is not detailed enough. let llvm_arch = sess.target.llvm_target.split_once('-').expect("LLVM target must have arch").0; let target_os = &*sess.target.os; - let target_env = &*sess.target.env; + let target_env = &sess.target.env; // The architecture name to forward to the linker. // @@ -3123,12 +3123,12 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo // > - xros-simulator // > - driverkit let platform_name = match (target_os, target_env) { - (os, "") => os, - ("ios", "macabi") => "mac-catalyst", - ("ios", "sim") => "ios-simulator", - ("tvos", "sim") => "tvos-simulator", - ("watchos", "sim") => "watchos-simulator", - ("visionos", "sim") => "visionos-simulator", + (os, Env::Unspecified) => os, + ("ios", Env::MacAbi) => "mac-catalyst", + ("ios", Env::Sim) => "ios-simulator", + ("tvos", Env::Sim) => "tvos-simulator", + ("watchos", Env::Sim) => "watchos-simulator", + ("visionos", Env::Sim) => "visionos-simulator", _ => bug!("invalid OS/env combination for Apple target: {target_os}, {target_env}"), }; diff --git a/compiler/rustc_codegen_ssa/src/common.rs b/compiler/rustc_codegen_ssa/src/common.rs index 517f0eb11ad2..b9d23e281169 100644 --- a/compiler/rustc_codegen_ssa/src/common.rs +++ b/compiler/rustc_codegen_ssa/src/common.rs @@ -7,7 +7,7 @@ use rustc_middle::ty::{self, Instance, TyCtxt}; use rustc_middle::{bug, mir, span_bug}; use rustc_session::cstore::{DllCallingConvention, DllImport}; use rustc_span::Span; -use rustc_target::spec::{Abi, Target}; +use rustc_target::spec::{Abi, Env, Target}; use crate::traits::*; @@ -171,7 +171,7 @@ pub fn asm_const_to_str<'tcx>( } pub fn is_mingw_gnu_toolchain(target: &Target) -> bool { - target.os == "windows" && target.env == "gnu" && target.abi == Abi::Unspecified + target.os == "windows" && target.env == Env::Gnu && target.abi == Abi::Unspecified } pub fn i686_decorated_name( diff --git a/compiler/rustc_metadata/src/native_libs.rs b/compiler/rustc_metadata/src/native_libs.rs index b5e97b4d5848..f7cbcf43a7e7 100644 --- a/compiler/rustc_metadata/src/native_libs.rs +++ b/compiler/rustc_metadata/src/native_libs.rs @@ -15,7 +15,7 @@ use rustc_session::cstore::{DllCallingConvention, DllImport, ForeignModule, Nati use rustc_session::search_paths::PathKind; use rustc_span::Symbol; use rustc_span::def_id::{DefId, LOCAL_CRATE}; -use rustc_target::spec::{Abi, Arch, BinaryFormat, LinkSelfContainedComponents}; +use rustc_target::spec::{Abi, Arch, BinaryFormat, Env, LinkSelfContainedComponents}; use crate::errors; @@ -79,7 +79,7 @@ pub fn walk_native_lib_search_dirs( // Mac Catalyst uses the macOS SDK, but to link to iOS-specific frameworks // we must have the support library stubs in the library search path (#121430). if let Some(sdk_root) = apple_sdk_root - && sess.target.env == "macabi" + && sess.target.env == Env::MacAbi { f(&sdk_root.join("System/iOSSupport/usr/lib"), false)?; f(&sdk_root.join("System/iOSSupport/System/Library/Frameworks"), true)?; diff --git a/compiler/rustc_session/src/config/cfg.rs b/compiler/rustc_session/src/config/cfg.rs index 98dfe4dcd460..a73508c78a24 100644 --- a/compiler/rustc_session/src/config/cfg.rs +++ b/compiler/rustc_session/src/config/cfg.rs @@ -242,7 +242,7 @@ pub(crate) fn default_configuration(sess: &Session) -> Cfg { ins_sym!(sym::target_abi, sess.target.abi.desc_symbol()); ins_sym!(sym::target_arch, sess.target.arch.desc_symbol()); ins_str!(sym::target_endian, sess.target.endian.as_str()); - ins_str!(sym::target_env, &sess.target.env); + ins_sym!(sym::target_env, sess.target.env.desc_symbol()); for family in sess.target.families.as_ref() { ins_str!(sym::target_family, family); @@ -450,7 +450,7 @@ impl CheckCfg { values_target_abi.insert(target.options.abi.desc_symbol()); values_target_arch.insert(target.arch.desc_symbol()); values_target_endian.insert(Symbol::intern(target.options.endian.as_str())); - values_target_env.insert(Symbol::intern(&target.options.env)); + values_target_env.insert(target.options.env.desc_symbol()); values_target_family.extend( target.options.families.iter().map(|family| Symbol::intern(family)), ); diff --git a/compiler/rustc_target/src/asm/aarch64.rs b/compiler/rustc_target/src/asm/aarch64.rs index 43a8d9ca119d..0dcf5c74e63b 100644 --- a/compiler/rustc_target/src/asm/aarch64.rs +++ b/compiler/rustc_target/src/asm/aarch64.rs @@ -4,7 +4,7 @@ use rustc_data_structures::fx::FxIndexSet; use rustc_span::{Symbol, sym}; use super::{InlineAsmArch, InlineAsmType, ModifierInfo}; -use crate::spec::{RelocModel, Target}; +use crate::spec::{Env, RelocModel, Target}; def_reg_class! { AArch64 AArch64InlineAsmRegClass { @@ -77,7 +77,7 @@ pub(crate) fn target_reserves_x18(target: &Target, target_features: &FxIndexSet< // Note that +reserve-x18 is currently not set for the above targets. target.os == "android" || target.os == "fuchsia" - || target.env == "ohos" + || target.env == Env::Ohos || target.is_like_darwin || target.is_like_windows || target_features.contains(&sym::reserve_x18) diff --git a/compiler/rustc_target/src/callconv/powerpc.rs b/compiler/rustc_target/src/callconv/powerpc.rs index 67066672eca3..34711d2843c5 100644 --- a/compiler/rustc_target/src/callconv/powerpc.rs +++ b/compiler/rustc_target/src/callconv/powerpc.rs @@ -1,7 +1,7 @@ use rustc_abi::TyAbiInterface; use crate::callconv::{ArgAbi, FnAbi}; -use crate::spec::HasTargetSpec; +use crate::spec::{Env, HasTargetSpec}; fn classify_ret(ret: &mut ArgAbi<'_, Ty>) { if ret.layout.is_aggregate() { @@ -18,7 +18,7 @@ where if arg.is_ignore() { // powerpc-unknown-linux-{gnu,musl,uclibc} doesn't ignore ZSTs. if cx.target_spec().os == "linux" - && matches!(&*cx.target_spec().env, "gnu" | "musl" | "uclibc") + && matches!(cx.target_spec().env, Env::Gnu | Env::Musl | Env::Uclibc) && arg.layout.is_zst() { arg.make_indirect_from_ignore(); diff --git a/compiler/rustc_target/src/callconv/powerpc64.rs b/compiler/rustc_target/src/callconv/powerpc64.rs index 380b280fbc64..536af1d9b9cd 100644 --- a/compiler/rustc_target/src/callconv/powerpc64.rs +++ b/compiler/rustc_target/src/callconv/powerpc64.rs @@ -5,7 +5,7 @@ use rustc_abi::{Endian, HasDataLayout, TyAbiInterface}; use crate::callconv::{Align, ArgAbi, FnAbi, Reg, RegKind, Uniform}; -use crate::spec::HasTargetSpec; +use crate::spec::{Env, HasTargetSpec}; #[derive(Debug, Clone, Copy, PartialEq)] enum ABI { @@ -106,7 +106,7 @@ where Ty: TyAbiInterface<'a, C> + Copy, C: HasDataLayout + HasTargetSpec, { - let abi = if cx.target_spec().env == "musl" || cx.target_spec().os == "freebsd" { + let abi = if cx.target_spec().env == Env::Musl || cx.target_spec().os == "freebsd" { ELFv2 } else if cx.target_spec().os == "aix" { AIX diff --git a/compiler/rustc_target/src/callconv/s390x.rs b/compiler/rustc_target/src/callconv/s390x.rs index c2f2b47690ca..a9ebd60b56f2 100644 --- a/compiler/rustc_target/src/callconv/s390x.rs +++ b/compiler/rustc_target/src/callconv/s390x.rs @@ -4,7 +4,7 @@ use rustc_abi::{BackendRepr, HasDataLayout, TyAbiInterface}; use crate::callconv::{ArgAbi, FnAbi, Reg, RegKind}; -use crate::spec::HasTargetSpec; +use crate::spec::{Env, HasTargetSpec}; fn classify_ret(ret: &mut ArgAbi<'_, Ty>) { let size = ret.layout.size; @@ -30,7 +30,7 @@ where if arg.is_ignore() { // s390x-unknown-linux-{gnu,musl,uclibc} doesn't ignore ZSTs. if cx.target_spec().os == "linux" - && matches!(&*cx.target_spec().env, "gnu" | "musl" | "uclibc") + && matches!(cx.target_spec().env, Env::Gnu | Env::Musl | Env::Uclibc) && arg.layout.is_zst() { arg.make_indirect_from_ignore(); diff --git a/compiler/rustc_target/src/callconv/sparc64.rs b/compiler/rustc_target/src/callconv/sparc64.rs index 911eaaf08f82..d4e8b8a086f6 100644 --- a/compiler/rustc_target/src/callconv/sparc64.rs +++ b/compiler/rustc_target/src/callconv/sparc64.rs @@ -6,7 +6,7 @@ use rustc_abi::{ }; use crate::callconv::{ArgAbi, ArgAttribute, CastTarget, FnAbi, Uniform}; -use crate::spec::HasTargetSpec; +use crate::spec::{Env, HasTargetSpec}; #[derive(Clone, Debug)] struct Sdata { @@ -224,7 +224,7 @@ where if arg.is_ignore() { // sparc64-unknown-linux-{gnu,musl,uclibc} doesn't ignore ZSTs. if cx.target_spec().os == "linux" - && matches!(&*cx.target_spec().env, "gnu" | "musl" | "uclibc") + && matches!(cx.target_spec().env, Env::Gnu | Env::Musl | Env::Uclibc) && arg.layout.is_zst() { arg.make_indirect_from_ignore(); diff --git a/compiler/rustc_target/src/spec/base/apple/mod.rs b/compiler/rustc_target/src/spec/base/apple/mod.rs index 6e73e0002828..45ac54c68f8a 100644 --- a/compiler/rustc_target/src/spec/base/apple/mod.rs +++ b/compiler/rustc_target/src/spec/base/apple/mod.rs @@ -4,7 +4,7 @@ use std::num::ParseIntError; use std::str::FromStr; use crate::spec::{ - Abi, BinaryFormat, Cc, DebuginfoKind, FloatAbi, FramePointer, LinkerFlavor, Lld, RustcAbi, + Abi, BinaryFormat, Cc, DebuginfoKind, Env, FloatAbi, FramePointer, LinkerFlavor, Lld, RustcAbi, SplitDebuginfo, StackProbeType, StaticCow, Target, TargetOptions, cvs, }; @@ -94,11 +94,11 @@ pub(crate) enum TargetEnv { } impl TargetEnv { - fn target_env(self) -> &'static str { + fn target_env(self) -> Env { match self { - Self::Normal => "", - Self::MacCatalyst => "macabi", - Self::Simulator => "sim", + Self::Normal => Env::Unspecified, + Self::MacCatalyst => Env::MacAbi, + Self::Simulator => Env::Sim, } } @@ -121,7 +121,7 @@ pub(crate) fn base( let mut opts = TargetOptions { llvm_floatabi: Some(FloatAbi::Hard), os: os.into(), - env: env.target_env().into(), + env: env.target_env(), // NOTE: We originally set `cfg(target_abi = "macabi")` / `cfg(target_abi = "sim")`, // before it was discovered that those are actually environments: // https://github.com/rust-lang/rust/issues/133331 @@ -319,18 +319,18 @@ impl OSVersion { /// This matches what LLVM does, see in part: /// pub fn minimum_deployment_target(target: &Target) -> Self { - let (major, minor, patch) = match (&*target.os, &target.arch, &*target.env) { + let (major, minor, patch) = match (&*target.os, &target.arch, &target.env) { ("macos", crate::spec::Arch::AArch64, _) => (11, 0, 0), - ("ios", crate::spec::Arch::AArch64, "macabi") => (14, 0, 0), - ("ios", crate::spec::Arch::AArch64, "sim") => (14, 0, 0), + ("ios", crate::spec::Arch::AArch64, Env::MacAbi) => (14, 0, 0), + ("ios", crate::spec::Arch::AArch64, Env::Sim) => (14, 0, 0), ("ios", _, _) if target.llvm_target.starts_with("arm64e") => (14, 0, 0), // Mac Catalyst defaults to 13.1 in Clang. - ("ios", _, "macabi") => (13, 1, 0), - ("tvos", crate::spec::Arch::AArch64, "sim") => (14, 0, 0), - ("watchos", crate::spec::Arch::AArch64, "sim") => (7, 0, 0), + ("ios", _, Env::MacAbi) => (13, 1, 0), + ("tvos", crate::spec::Arch::AArch64, Env::Sim) => (14, 0, 0), + ("watchos", crate::spec::Arch::AArch64, Env::Sim) => (7, 0, 0), // True Aarch64 on watchOS (instead of their Aarch64 Ilp32 called `arm64_32`) has been // available since Xcode 14, but it's only actually used more recently in watchOS 26. - ("watchos", crate::spec::Arch::AArch64, "") + ("watchos", crate::spec::Arch::AArch64, Env::Unspecified) if !target.llvm_target.starts_with("arm64_32") => { (26, 0, 0) diff --git a/compiler/rustc_target/src/spec/base/apple/tests.rs b/compiler/rustc_target/src/spec/base/apple/tests.rs index ea30613796ac..fd3fd6da9116 100644 --- a/compiler/rustc_target/src/spec/base/apple/tests.rs +++ b/compiler/rustc_target/src/spec/base/apple/tests.rs @@ -1,10 +1,10 @@ use super::OSVersion; -use crate::spec::Abi; use crate::spec::targets::{ aarch64_apple_darwin, aarch64_apple_ios_sim, aarch64_apple_visionos_sim, aarch64_apple_watchos_sim, i686_apple_darwin, x86_64_apple_darwin, x86_64_apple_ios, x86_64_apple_tvos, x86_64_apple_watchos_sim, }; +use crate::spec::{Abi, Env}; #[test] fn simulator_targets_set_env() { @@ -19,7 +19,7 @@ fn simulator_targets_set_env() { ]; for target in &all_sim_targets { - assert_eq!(target.env, "sim"); + assert_eq!(target.env, Env::Sim); // Ensure backwards compat assert_eq!(target.abi, Abi::Sim); } diff --git a/compiler/rustc_target/src/spec/base/hurd_gnu.rs b/compiler/rustc_target/src/spec/base/hurd_gnu.rs index d33372b41b90..cc1696b3ab04 100644 --- a/compiler/rustc_target/src/spec/base/hurd_gnu.rs +++ b/compiler/rustc_target/src/spec/base/hurd_gnu.rs @@ -1,5 +1,5 @@ -use crate::spec::{TargetOptions, base}; +use crate::spec::{Env, TargetOptions, base}; pub(crate) fn opts() -> TargetOptions { - TargetOptions { env: "gnu".into(), ..base::hurd::opts() } + TargetOptions { env: Env::Gnu, ..base::hurd::opts() } } diff --git a/compiler/rustc_target/src/spec/base/l4re.rs b/compiler/rustc_target/src/spec/base/l4re.rs index 072a6a1001bd..126865549ea6 100644 --- a/compiler/rustc_target/src/spec/base/l4re.rs +++ b/compiler/rustc_target/src/spec/base/l4re.rs @@ -1,9 +1,9 @@ -use crate::spec::{Cc, LinkerFlavor, PanicStrategy, RelocModel, TargetOptions, cvs}; +use crate::spec::{Cc, Env, LinkerFlavor, PanicStrategy, RelocModel, TargetOptions, cvs}; pub(crate) fn opts() -> TargetOptions { TargetOptions { os: "l4re".into(), - env: "uclibc".into(), + env: Env::Uclibc, linker_flavor: LinkerFlavor::Unix(Cc::No), panic_strategy: PanicStrategy::Abort, linker: Some("l4-bender".into()), diff --git a/compiler/rustc_target/src/spec/base/linux_gnu.rs b/compiler/rustc_target/src/spec/base/linux_gnu.rs index 2fcd8c61a9c6..7a907c802df2 100644 --- a/compiler/rustc_target/src/spec/base/linux_gnu.rs +++ b/compiler/rustc_target/src/spec/base/linux_gnu.rs @@ -1,7 +1,7 @@ -use crate::spec::{Cc, LinkerFlavor, Lld, TargetOptions, base}; +use crate::spec::{Cc, Env, LinkerFlavor, Lld, TargetOptions, base}; pub(crate) fn opts() -> TargetOptions { - let mut base = TargetOptions { env: "gnu".into(), ..base::linux::opts() }; + let mut base = TargetOptions { env: Env::Gnu, ..base::linux::opts() }; // When we're asked to use the `rust-lld` linker by default, set the appropriate lld-using // linker flavor, and self-contained linker component. diff --git a/compiler/rustc_target/src/spec/base/linux_musl.rs b/compiler/rustc_target/src/spec/base/linux_musl.rs index 1bef602404e5..6d3124b559b1 100644 --- a/compiler/rustc_target/src/spec/base/linux_musl.rs +++ b/compiler/rustc_target/src/spec/base/linux_musl.rs @@ -1,8 +1,8 @@ -use crate::spec::{LinkSelfContainedDefault, TargetOptions, base, crt_objects}; +use crate::spec::{Env, LinkSelfContainedDefault, TargetOptions, base, crt_objects}; pub(crate) fn opts() -> TargetOptions { TargetOptions { - env: "musl".into(), + env: Env::Musl, pre_link_objects_self_contained: crt_objects::pre_musl_self_contained(), post_link_objects_self_contained: crt_objects::post_musl_self_contained(), link_self_contained: LinkSelfContainedDefault::InferredForMusl, diff --git a/compiler/rustc_target/src/spec/base/linux_ohos.rs b/compiler/rustc_target/src/spec/base/linux_ohos.rs index 1b7f1e196664..dee78fb325c1 100644 --- a/compiler/rustc_target/src/spec/base/linux_ohos.rs +++ b/compiler/rustc_target/src/spec/base/linux_ohos.rs @@ -1,8 +1,8 @@ -use crate::spec::{TargetOptions, TlsModel, base}; +use crate::spec::{Env, TargetOptions, TlsModel, base}; pub(crate) fn opts() -> TargetOptions { TargetOptions { - env: "ohos".into(), + env: Env::Ohos, crt_static_default: false, tls_model: TlsModel::Emulated, has_thread_local: false, diff --git a/compiler/rustc_target/src/spec/base/linux_uclibc.rs b/compiler/rustc_target/src/spec/base/linux_uclibc.rs index 40801b76dca7..c65a3b9139f1 100644 --- a/compiler/rustc_target/src/spec/base/linux_uclibc.rs +++ b/compiler/rustc_target/src/spec/base/linux_uclibc.rs @@ -1,5 +1,5 @@ -use crate::spec::{TargetOptions, base}; +use crate::spec::{Env, TargetOptions, base}; pub(crate) fn opts() -> TargetOptions { - TargetOptions { env: "uclibc".into(), ..base::linux::opts() } + TargetOptions { env: Env::Uclibc, ..base::linux::opts() } } diff --git a/compiler/rustc_target/src/spec/base/linux_wasm.rs b/compiler/rustc_target/src/spec/base/linux_wasm.rs index a8c137c22a97..c13b130b3691 100644 --- a/compiler/rustc_target/src/spec/base/linux_wasm.rs +++ b/compiler/rustc_target/src/spec/base/linux_wasm.rs @@ -2,8 +2,8 @@ //! aspects from their respective base targets use crate::spec::{ - Cc, LinkSelfContainedDefault, LinkerFlavor, PanicStrategy, RelocModel, TargetOptions, TlsModel, - add_link_args, crt_objects, cvs, + Cc, Env, LinkSelfContainedDefault, LinkerFlavor, PanicStrategy, RelocModel, TargetOptions, + TlsModel, add_link_args, crt_objects, cvs, }; pub(crate) fn opts() -> TargetOptions { @@ -58,7 +58,7 @@ pub(crate) fn opts() -> TargetOptions { is_like_wasm: true, families: cvs!["wasm", "unix"], os: "linux".into(), - env: "musl".into(), + env: Env::Musl, // we allow dynamic linking, but only cdylibs. Basically we allow a // final library artifact that exports some symbols (a wasm module) but diff --git a/compiler/rustc_target/src/spec/base/managarm_mlibc.rs b/compiler/rustc_target/src/spec/base/managarm_mlibc.rs index da3856b212d9..3eea65550399 100644 --- a/compiler/rustc_target/src/spec/base/managarm_mlibc.rs +++ b/compiler/rustc_target/src/spec/base/managarm_mlibc.rs @@ -1,9 +1,9 @@ -use crate::spec::{RelroLevel, TargetOptions, cvs}; +use crate::spec::{Env, RelroLevel, TargetOptions, cvs}; pub(crate) fn opts() -> TargetOptions { TargetOptions { os: "managarm".into(), - env: "mlibc".into(), + env: Env::Mlibc, dynamic_linking: true, executables: true, families: cvs!["unix"], diff --git a/compiler/rustc_target/src/spec/base/redox.rs b/compiler/rustc_target/src/spec/base/redox.rs index e9b47f3fa3fd..8a394df2dff8 100644 --- a/compiler/rustc_target/src/spec/base/redox.rs +++ b/compiler/rustc_target/src/spec/base/redox.rs @@ -1,9 +1,9 @@ -use crate::spec::{Cc, LinkerFlavor, Lld, RelroLevel, TargetOptions, cvs}; +use crate::spec::{Cc, Env, LinkerFlavor, Lld, RelroLevel, TargetOptions, cvs}; pub(crate) fn opts() -> TargetOptions { TargetOptions { os: "redox".into(), - env: "relibc".into(), + env: Env::Relibc, dynamic_linking: true, families: cvs!["unix"], has_rpath: true, diff --git a/compiler/rustc_target/src/spec/base/unikraft_linux_musl.rs b/compiler/rustc_target/src/spec/base/unikraft_linux_musl.rs index 319a6182303b..154633090d7b 100644 --- a/compiler/rustc_target/src/spec/base/unikraft_linux_musl.rs +++ b/compiler/rustc_target/src/spec/base/unikraft_linux_musl.rs @@ -1,9 +1,9 @@ -use crate::spec::{PanicStrategy, RelocModel, TargetOptions, cvs}; +use crate::spec::{Env, PanicStrategy, RelocModel, TargetOptions, cvs}; pub(crate) fn opts() -> TargetOptions { TargetOptions { os: "linux".into(), - env: "musl".into(), + env: Env::Musl, vendor: "unikraft".into(), linker: Some("kraftld".into()), relocation_model: RelocModel::Static, diff --git a/compiler/rustc_target/src/spec/base/vxworks.rs b/compiler/rustc_target/src/spec/base/vxworks.rs index bb0d00f4a4c0..04e1d4b9a826 100644 --- a/compiler/rustc_target/src/spec/base/vxworks.rs +++ b/compiler/rustc_target/src/spec/base/vxworks.rs @@ -1,9 +1,9 @@ -use crate::spec::{TargetOptions, cvs}; +use crate::spec::{Env, TargetOptions, cvs}; pub(crate) fn opts() -> TargetOptions { TargetOptions { os: "vxworks".into(), - env: "gnu".into(), + env: Env::Gnu, vendor: "wrs".into(), linker: Some("wr-c++".into()), exe_suffix: ".vxe".into(), diff --git a/compiler/rustc_target/src/spec/base/windows_gnu.rs b/compiler/rustc_target/src/spec/base/windows_gnu.rs index 2867428e42f7..d8e660c7cda2 100644 --- a/compiler/rustc_target/src/spec/base/windows_gnu.rs +++ b/compiler/rustc_target/src/spec/base/windows_gnu.rs @@ -1,8 +1,8 @@ use std::borrow::Cow; use crate::spec::{ - BinaryFormat, Cc, DebuginfoKind, LinkSelfContainedDefault, LinkerFlavor, Lld, SplitDebuginfo, - TargetOptions, add_link_args, crt_objects, cvs, + BinaryFormat, Cc, DebuginfoKind, Env, LinkSelfContainedDefault, LinkerFlavor, Lld, + SplitDebuginfo, TargetOptions, add_link_args, crt_objects, cvs, }; pub(crate) fn opts() -> TargetOptions { @@ -78,7 +78,7 @@ pub(crate) fn opts() -> TargetOptions { TargetOptions { os: "windows".into(), - env: "gnu".into(), + env: Env::Gnu, vendor: "pc".into(), // FIXME(#13846) this should be enabled for windows function_sections: false, diff --git a/compiler/rustc_target/src/spec/base/windows_gnullvm.rs b/compiler/rustc_target/src/spec/base/windows_gnullvm.rs index 6dbe504a7405..41b8a0122fb0 100644 --- a/compiler/rustc_target/src/spec/base/windows_gnullvm.rs +++ b/compiler/rustc_target/src/spec/base/windows_gnullvm.rs @@ -1,7 +1,8 @@ use std::borrow::Cow; use crate::spec::{ - Abi, BinaryFormat, Cc, DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions, cvs, + Abi, BinaryFormat, Cc, DebuginfoKind, Env, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions, + cvs, }; pub(crate) fn opts() -> TargetOptions { @@ -21,7 +22,7 @@ pub(crate) fn opts() -> TargetOptions { TargetOptions { os: "windows".into(), - env: "gnu".into(), + env: Env::Gnu, vendor: "pc".into(), abi: Abi::Llvm, linker: Some("clang".into()), diff --git a/compiler/rustc_target/src/spec/base/windows_msvc.rs b/compiler/rustc_target/src/spec/base/windows_msvc.rs index 066785a1ae41..d2c143613891 100644 --- a/compiler/rustc_target/src/spec/base/windows_msvc.rs +++ b/compiler/rustc_target/src/spec/base/windows_msvc.rs @@ -1,11 +1,11 @@ -use crate::spec::{TargetOptions, base, cvs}; +use crate::spec::{Env, TargetOptions, base, cvs}; pub(crate) fn opts() -> TargetOptions { let base = base::msvc::opts(); TargetOptions { os: "windows".into(), - env: "msvc".into(), + env: Env::Msvc, vendor: "pc".into(), dynamic_linking: true, dll_prefix: "".into(), diff --git a/compiler/rustc_target/src/spec/json.rs b/compiler/rustc_target/src/spec/json.rs index abcae134bc15..7924460b78f2 100644 --- a/compiler/rustc_target/src/spec/json.rs +++ b/compiler/rustc_target/src/spec/json.rs @@ -5,7 +5,7 @@ use rustc_abi::{Align, AlignFromBytesError}; use super::crt_objects::CrtObjects; use super::{ - Abi, Arch, BinaryFormat, CodeModel, DebuginfoKind, FloatAbi, FramePointer, LinkArgsCli, + Abi, Arch, BinaryFormat, CodeModel, DebuginfoKind, Env, FloatAbi, FramePointer, LinkArgsCli, LinkSelfContainedComponents, LinkSelfContainedDefault, LinkerFlavorCli, LldFlavor, MergeFunctions, PanicStrategy, RelocModel, RelroLevel, RustcAbi, SanitizerSet, SmallDataThresholdSupport, SplitDebuginfo, StackProbeType, StaticCow, SymbolVisibility, Target, @@ -506,7 +506,7 @@ struct TargetSpecJson { c_int_width: Option, c_enum_min_bits: Option, os: Option>, - env: Option>, + env: Option, abi: Option, vendor: Option>, linker: Option>, diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index a8539669678c..d72ce7e14b05 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -1926,6 +1926,38 @@ impl Arch { } } +crate::target_spec_enum! { + pub enum Env { + Gnu = "gnu", + MacAbi = "macabi", + Mlibc = "mlibc", + Msvc = "msvc", + Musl = "musl", + Newlib = "newlib", + Nto70 = "nto70", + Nto71 = "nto71", + Nto71IoSock = "nto71_iosock", + Nto80 = "nto80", + Ohos = "ohos", + Relibc = "relibc", + Sgx = "sgx", + Sim = "sim", + P1 = "p1", + P2 = "p2", + P3 = "p3", + Uclibc = "uclibc", + V5 = "v5", + Unspecified = "", + } + other_variant = Other; +} + +impl Env { + pub fn desc_symbol(&self) -> Symbol { + Symbol::intern(self.desc()) + } +} + crate::target_spec_enum! { pub enum Abi { Abi64 = "abi64", @@ -2081,8 +2113,8 @@ pub struct TargetOptions { /// A couple of targets having `std` also use "unknown" as an `os` value, /// but they are exceptions. pub os: StaticCow, - /// Environment name to use for conditional compilation (`target_env`). Defaults to "". - pub env: StaticCow, + /// Environment name to use for conditional compilation (`target_env`). Defaults to [`Env::Unspecified`]. + pub env: Env, /// ABI name to distinguish multiple ABIs on the same OS and architecture. For instance, `"eabi"` /// or `"eabihf"`. Defaults to [`Abi::Unspecified`]. /// This field is *not* forwarded directly to LLVM; its primary purpose is `cfg(target_abi)`. @@ -2591,7 +2623,7 @@ impl Default for TargetOptions { endian: Endian::Little, c_int_width: 32, os: "none".into(), - env: "".into(), + env: Env::Unspecified, abi: Abi::Unspecified, vendor: "unknown".into(), linker: option_env!("CFG_DEFAULT_LINKER").map(|s| s.into()), @@ -3148,7 +3180,7 @@ impl Target { fn can_use_os_unknown(&self) -> bool { self.llvm_target == "wasm32-unknown-unknown" || self.llvm_target == "wasm64-unknown-unknown" - || (self.env == "sgx" && self.vendor == "fortanix") + || (self.env == Env::Sgx && self.vendor == "fortanix") } /// Load a built-in target diff --git a/compiler/rustc_target/src/spec/targets/aarch64_unknown_nto_qnx700.rs b/compiler/rustc_target/src/spec/targets/aarch64_unknown_nto_qnx700.rs index b26e6f19e1ab..e1f29f832f0b 100644 --- a/compiler/rustc_target/src/spec/targets/aarch64_unknown_nto_qnx700.rs +++ b/compiler/rustc_target/src/spec/targets/aarch64_unknown_nto_qnx700.rs @@ -1,11 +1,11 @@ -use crate::spec::Target; use crate::spec::base::nto_qnx; +use crate::spec::{Env, Target}; pub(crate) fn target() -> Target { let mut target = nto_qnx::aarch64(); target.metadata.description = Some("ARM64 QNX Neutrino 7.0 RTOS".into()); target.options.pre_link_args = nto_qnx::pre_link_args(nto_qnx::ApiVariant::Default, nto_qnx::Arch::Aarch64); - target.options.env = "nto70".into(); + target.options.env = Env::Nto70; target } diff --git a/compiler/rustc_target/src/spec/targets/aarch64_unknown_nto_qnx710.rs b/compiler/rustc_target/src/spec/targets/aarch64_unknown_nto_qnx710.rs index 3a78952c36c4..1baa56630d3a 100644 --- a/compiler/rustc_target/src/spec/targets/aarch64_unknown_nto_qnx710.rs +++ b/compiler/rustc_target/src/spec/targets/aarch64_unknown_nto_qnx710.rs @@ -1,5 +1,5 @@ -use crate::spec::Target; use crate::spec::base::nto_qnx; +use crate::spec::{Env, Target}; pub(crate) fn target() -> Target { let mut target = nto_qnx::aarch64(); @@ -7,6 +7,6 @@ pub(crate) fn target() -> Target { Some("ARM64 QNX Neutrino 7.1 RTOS with io-pkt network stack".into()); target.options.pre_link_args = nto_qnx::pre_link_args(nto_qnx::ApiVariant::Default, nto_qnx::Arch::Aarch64); - target.options.env = "nto71".into(); + target.options.env = Env::Nto71; target } diff --git a/compiler/rustc_target/src/spec/targets/aarch64_unknown_nto_qnx710_iosock.rs b/compiler/rustc_target/src/spec/targets/aarch64_unknown_nto_qnx710_iosock.rs index 4964f4078f5c..80ae93247a3f 100644 --- a/compiler/rustc_target/src/spec/targets/aarch64_unknown_nto_qnx710_iosock.rs +++ b/compiler/rustc_target/src/spec/targets/aarch64_unknown_nto_qnx710_iosock.rs @@ -1,5 +1,5 @@ -use crate::spec::Target; use crate::spec::base::nto_qnx; +use crate::spec::{Env, Target}; pub(crate) fn target() -> Target { let mut target = nto_qnx::aarch64(); @@ -7,6 +7,6 @@ pub(crate) fn target() -> Target { Some("ARM64 QNX Neutrino 7.1 RTOS with io-sock network stack".into()); target.options.pre_link_args = nto_qnx::pre_link_args(nto_qnx::ApiVariant::IoSock, nto_qnx::Arch::Aarch64); - target.options.env = "nto71_iosock".into(); + target.options.env = Env::Nto71IoSock; target } diff --git a/compiler/rustc_target/src/spec/targets/aarch64_unknown_nto_qnx800.rs b/compiler/rustc_target/src/spec/targets/aarch64_unknown_nto_qnx800.rs index 5b820681efe9..5d265087c4a2 100644 --- a/compiler/rustc_target/src/spec/targets/aarch64_unknown_nto_qnx800.rs +++ b/compiler/rustc_target/src/spec/targets/aarch64_unknown_nto_qnx800.rs @@ -1,11 +1,11 @@ -use crate::spec::Target; use crate::spec::base::nto_qnx; +use crate::spec::{Env, Target}; pub(crate) fn target() -> Target { let mut target = nto_qnx::aarch64(); target.metadata.description = Some("ARM64 QNX Neutrino 8.0 RTOS".into()); target.options.pre_link_args = nto_qnx::pre_link_args(nto_qnx::ApiVariant::Default, nto_qnx::Arch::Aarch64); - target.options.env = "nto80".into(); + target.options.env = Env::Nto80; target } diff --git a/compiler/rustc_target/src/spec/targets/armv6k_nintendo_3ds.rs b/compiler/rustc_target/src/spec/targets/armv6k_nintendo_3ds.rs index 380d3bac521c..78d4fe94acec 100644 --- a/compiler/rustc_target/src/spec/targets/armv6k_nintendo_3ds.rs +++ b/compiler/rustc_target/src/spec/targets/armv6k_nintendo_3ds.rs @@ -1,6 +1,6 @@ use crate::spec::{ - Abi, Arch, Cc, FloatAbi, LinkerFlavor, Lld, RelocModel, Target, TargetMetadata, TargetOptions, - cvs, + Abi, Arch, Cc, Env, FloatAbi, LinkerFlavor, Lld, RelocModel, Target, TargetMetadata, + TargetOptions, cvs, }; /// A base target for Nintendo 3DS devices using the devkitARM toolchain. @@ -26,7 +26,7 @@ pub(crate) fn target() -> Target { options: TargetOptions { os: "horizon".into(), - env: "newlib".into(), + env: Env::Newlib, vendor: "nintendo".into(), cpu: "mpcore".into(), abi: Abi::EabiHf, diff --git a/compiler/rustc_target/src/spec/targets/armv7_rtems_eabihf.rs b/compiler/rustc_target/src/spec/targets/armv7_rtems_eabihf.rs index 27fdfda552cc..73195b18499f 100644 --- a/compiler/rustc_target/src/spec/targets/armv7_rtems_eabihf.rs +++ b/compiler/rustc_target/src/spec/targets/armv7_rtems_eabihf.rs @@ -1,6 +1,6 @@ use crate::spec::{ - Abi, Arch, Cc, FloatAbi, LinkerFlavor, Lld, PanicStrategy, RelocModel, Target, TargetMetadata, - TargetOptions, cvs, + Abi, Arch, Cc, Env, FloatAbi, LinkerFlavor, Lld, PanicStrategy, RelocModel, Target, + TargetMetadata, TargetOptions, cvs, }; pub(crate) fn target() -> Target { @@ -32,7 +32,7 @@ pub(crate) fn target() -> Target { c_enum_min_bits: Some(8), eh_frame_header: false, no_default_libraries: false, - env: "newlib".into(), + env: Env::Newlib, ..Default::default() }, } diff --git a/compiler/rustc_target/src/spec/targets/armv7_sony_vita_newlibeabihf.rs b/compiler/rustc_target/src/spec/targets/armv7_sony_vita_newlibeabihf.rs index c4d3f52d1e6c..481ab5ff644f 100644 --- a/compiler/rustc_target/src/spec/targets/armv7_sony_vita_newlibeabihf.rs +++ b/compiler/rustc_target/src/spec/targets/armv7_sony_vita_newlibeabihf.rs @@ -1,8 +1,8 @@ use rustc_abi::Endian; use crate::spec::{ - Abi, Arch, Cc, FloatAbi, LinkerFlavor, Lld, RelocModel, Target, TargetMetadata, TargetOptions, - cvs, + Abi, Arch, Cc, Env, FloatAbi, LinkerFlavor, Lld, RelocModel, Target, TargetMetadata, + TargetOptions, cvs, }; /// A base target for PlayStation Vita devices using the VITASDK toolchain (using newlib). @@ -32,7 +32,7 @@ pub(crate) fn target() -> Target { os: "vita".into(), endian: Endian::Little, c_int_width: 32, - env: "newlib".into(), + env: Env::Newlib, vendor: "sony".into(), abi: Abi::EabiHf, llvm_floatabi: Some(FloatAbi::Hard), diff --git a/compiler/rustc_target/src/spec/targets/armv7a_vex_v5.rs b/compiler/rustc_target/src/spec/targets/armv7a_vex_v5.rs index 938227316863..c1fbbc33b878 100644 --- a/compiler/rustc_target/src/spec/targets/armv7a_vex_v5.rs +++ b/compiler/rustc_target/src/spec/targets/armv7a_vex_v5.rs @@ -1,6 +1,6 @@ use crate::spec::{ - Abi, Arch, Cc, FloatAbi, LinkerFlavor, Lld, PanicStrategy, RelocModel, Target, TargetMetadata, - TargetOptions, + Abi, Arch, Cc, Env, FloatAbi, LinkerFlavor, Lld, PanicStrategy, RelocModel, Target, + TargetMetadata, TargetOptions, }; const LINKER_SCRIPT: &str = include_str!("./armv7a_vex_v5_linker_script.ld"); @@ -8,7 +8,7 @@ const LINKER_SCRIPT: &str = include_str!("./armv7a_vex_v5_linker_script.ld"); pub(crate) fn target() -> Target { let opts = TargetOptions { vendor: "vex".into(), - env: "v5".into(), + env: Env::V5, os: "vexos".into(), cpu: "cortex-a9".into(), abi: Abi::EabiHf, diff --git a/compiler/rustc_target/src/spec/targets/i686_pc_nto_qnx700.rs b/compiler/rustc_target/src/spec/targets/i686_pc_nto_qnx700.rs index af7f3b6b8563..7184a54961e2 100644 --- a/compiler/rustc_target/src/spec/targets/i686_pc_nto_qnx700.rs +++ b/compiler/rustc_target/src/spec/targets/i686_pc_nto_qnx700.rs @@ -1,5 +1,5 @@ use crate::spec::base::nto_qnx; -use crate::spec::{Arch, RustcAbi, StackProbeType, Target, TargetOptions, base}; +use crate::spec::{Arch, Env, RustcAbi, StackProbeType, Target, TargetOptions, base}; pub(crate) fn target() -> Target { let mut meta = nto_qnx::meta(); @@ -21,7 +21,7 @@ pub(crate) fn target() -> Target { nto_qnx::ApiVariant::Default, nto_qnx::Arch::I586, ), - env: "nto70".into(), + env: Env::Nto70, vendor: "pc".into(), stack_probes: StackProbeType::Inline, ..base::nto_qnx::opts() diff --git a/compiler/rustc_target/src/spec/targets/riscv32imac_esp_espidf.rs b/compiler/rustc_target/src/spec/targets/riscv32imac_esp_espidf.rs index 6b3e5a47ad87..81bd2a64632b 100644 --- a/compiler/rustc_target/src/spec/targets/riscv32imac_esp_espidf.rs +++ b/compiler/rustc_target/src/spec/targets/riscv32imac_esp_espidf.rs @@ -1,4 +1,6 @@ -use crate::spec::{Arch, PanicStrategy, RelocModel, Target, TargetMetadata, TargetOptions, cvs}; +use crate::spec::{ + Arch, Env, PanicStrategy, RelocModel, Target, TargetMetadata, TargetOptions, cvs, +}; pub(crate) fn target() -> Target { Target { @@ -16,7 +18,7 @@ pub(crate) fn target() -> Target { options: TargetOptions { families: cvs!["unix"], os: "espidf".into(), - env: "newlib".into(), + env: Env::Newlib, vendor: "espressif".into(), linker: Some("riscv32-esp-elf-gcc".into()), cpu: "generic-rv32".into(), diff --git a/compiler/rustc_target/src/spec/targets/riscv32imafc_esp_espidf.rs b/compiler/rustc_target/src/spec/targets/riscv32imafc_esp_espidf.rs index 196a3de95cfb..198afeb20533 100644 --- a/compiler/rustc_target/src/spec/targets/riscv32imafc_esp_espidf.rs +++ b/compiler/rustc_target/src/spec/targets/riscv32imafc_esp_espidf.rs @@ -1,4 +1,6 @@ -use crate::spec::{Arch, PanicStrategy, RelocModel, Target, TargetMetadata, TargetOptions, cvs}; +use crate::spec::{ + Arch, Env, PanicStrategy, RelocModel, Target, TargetMetadata, TargetOptions, cvs, +}; pub(crate) fn target() -> Target { Target { @@ -16,7 +18,7 @@ pub(crate) fn target() -> Target { options: TargetOptions { families: cvs!["unix"], os: "espidf".into(), - env: "newlib".into(), + env: Env::Newlib, vendor: "espressif".into(), linker: Some("riscv32-esp-elf-gcc".into()), cpu: "generic-rv32".into(), diff --git a/compiler/rustc_target/src/spec/targets/riscv32imc_esp_espidf.rs b/compiler/rustc_target/src/spec/targets/riscv32imc_esp_espidf.rs index 66c64667d0dc..8534bb6093bd 100644 --- a/compiler/rustc_target/src/spec/targets/riscv32imc_esp_espidf.rs +++ b/compiler/rustc_target/src/spec/targets/riscv32imc_esp_espidf.rs @@ -1,4 +1,6 @@ -use crate::spec::{Arch, PanicStrategy, RelocModel, Target, TargetMetadata, TargetOptions, cvs}; +use crate::spec::{ + Arch, Env, PanicStrategy, RelocModel, Target, TargetMetadata, TargetOptions, cvs, +}; pub(crate) fn target() -> Target { Target { @@ -16,7 +18,7 @@ pub(crate) fn target() -> Target { options: TargetOptions { families: cvs!["unix"], os: "espidf".into(), - env: "newlib".into(), + env: Env::Newlib, vendor: "espressif".into(), linker: Some("riscv32-esp-elf-gcc".into()), cpu: "generic-rv32".into(), diff --git a/compiler/rustc_target/src/spec/targets/wasm32_wasip1.rs b/compiler/rustc_target/src/spec/targets/wasm32_wasip1.rs index a7c196c4530f..65ca593730ea 100644 --- a/compiler/rustc_target/src/spec/targets/wasm32_wasip1.rs +++ b/compiler/rustc_target/src/spec/targets/wasm32_wasip1.rs @@ -11,14 +11,15 @@ //! introduced. use crate::spec::{ - Arch, Cc, LinkSelfContainedDefault, LinkerFlavor, Target, TargetMetadata, base, crt_objects, + Arch, Cc, Env, LinkSelfContainedDefault, LinkerFlavor, Target, TargetMetadata, base, + crt_objects, }; pub(crate) fn target() -> Target { let mut options = base::wasm::options(); options.os = "wasi".into(); - options.env = "p1".into(); + options.env = Env::P1; options.add_pre_link_args(LinkerFlavor::WasmLld(Cc::Yes), &["--target=wasm32-wasip1"]); options.pre_link_objects_self_contained = crt_objects::pre_wasi_self_contained(); diff --git a/compiler/rustc_target/src/spec/targets/wasm32_wasip1_threads.rs b/compiler/rustc_target/src/spec/targets/wasm32_wasip1_threads.rs index d4953cc9d498..f1b91be4f155 100644 --- a/compiler/rustc_target/src/spec/targets/wasm32_wasip1_threads.rs +++ b/compiler/rustc_target/src/spec/targets/wasm32_wasip1_threads.rs @@ -8,14 +8,15 @@ //! Historically this target was known as `wasm32-wasi-preview1-threads`. use crate::spec::{ - Arch, Cc, LinkSelfContainedDefault, LinkerFlavor, Target, TargetMetadata, base, crt_objects, + Arch, Cc, Env, LinkSelfContainedDefault, LinkerFlavor, Target, TargetMetadata, base, + crt_objects, }; pub(crate) fn target() -> Target { let mut options = base::wasm::options(); options.os = "wasi".into(); - options.env = "p1".into(); + options.env = Env::P1; options.add_pre_link_args( LinkerFlavor::WasmLld(Cc::No), diff --git a/compiler/rustc_target/src/spec/targets/wasm32_wasip2.rs b/compiler/rustc_target/src/spec/targets/wasm32_wasip2.rs index 717d49004a17..67a12c032b6d 100644 --- a/compiler/rustc_target/src/spec/targets/wasm32_wasip2.rs +++ b/compiler/rustc_target/src/spec/targets/wasm32_wasip2.rs @@ -17,14 +17,14 @@ //! . use crate::spec::{ - Arch, LinkSelfContainedDefault, RelocModel, Target, TargetMetadata, base, crt_objects, + Arch, Env, LinkSelfContainedDefault, RelocModel, Target, TargetMetadata, base, crt_objects, }; pub(crate) fn target() -> Target { let mut options = base::wasm::options(); options.os = "wasi".into(); - options.env = "p2".into(); + options.env = Env::P2; options.linker = Some("wasm-component-ld".into()); options.pre_link_objects_self_contained = crt_objects::pre_wasi_self_contained(); diff --git a/compiler/rustc_target/src/spec/targets/wasm32_wasip3.rs b/compiler/rustc_target/src/spec/targets/wasm32_wasip3.rs index e3d5e6542c26..d417f3d48a4b 100644 --- a/compiler/rustc_target/src/spec/targets/wasm32_wasip3.rs +++ b/compiler/rustc_target/src/spec/targets/wasm32_wasip3.rs @@ -8,13 +8,13 @@ //! all component-model-level imports anyway. Over time the imports of the //! standard library will change to WASIp3. -use crate::spec::Target; +use crate::spec::{Env, Target}; pub(crate) fn target() -> Target { // As of now WASIp3 is a lightly edited wasip2 target, so start with that // and this may grow over time as more features are supported. let mut target = super::wasm32_wasip2::target(); target.llvm_target = "wasm32-wasip3".into(); - target.options.env = "p3".into(); + target.options.env = Env::P3; target } diff --git a/compiler/rustc_target/src/spec/targets/x86_64_fortanix_unknown_sgx.rs b/compiler/rustc_target/src/spec/targets/x86_64_fortanix_unknown_sgx.rs index 9b5b42905d5e..85c5e0a5f5ee 100644 --- a/compiler/rustc_target/src/spec/targets/x86_64_fortanix_unknown_sgx.rs +++ b/compiler/rustc_target/src/spec/targets/x86_64_fortanix_unknown_sgx.rs @@ -1,6 +1,8 @@ use std::borrow::Cow; -use crate::spec::{Abi, Arch, Cc, LinkerFlavor, Lld, Target, TargetMetadata, TargetOptions, cvs}; +use crate::spec::{ + Abi, Arch, Cc, Env, LinkerFlavor, Lld, Target, TargetMetadata, TargetOptions, cvs, +}; pub(crate) fn target() -> Target { let pre_link_args = TargetOptions::link_args( @@ -56,7 +58,7 @@ pub(crate) fn target() -> Target { ]; let opts = TargetOptions { os: "unknown".into(), - env: "sgx".into(), + env: Env::Sgx, vendor: "fortanix".into(), abi: Abi::Fortanix, linker_flavor: LinkerFlavor::Gnu(Cc::No, Lld::Yes), diff --git a/compiler/rustc_target/src/spec/targets/x86_64_pc_nto_qnx710.rs b/compiler/rustc_target/src/spec/targets/x86_64_pc_nto_qnx710.rs index 248aa91862c9..916001548509 100644 --- a/compiler/rustc_target/src/spec/targets/x86_64_pc_nto_qnx710.rs +++ b/compiler/rustc_target/src/spec/targets/x86_64_pc_nto_qnx710.rs @@ -1,5 +1,5 @@ -use crate::spec::Target; use crate::spec::base::nto_qnx; +use crate::spec::{Env, Target}; pub(crate) fn target() -> Target { let mut target = nto_qnx::x86_64(); @@ -7,6 +7,6 @@ pub(crate) fn target() -> Target { Some("x86 64-bit QNX Neutrino 7.1 RTOS with io-pkt network stack".into()); target.options.pre_link_args = nto_qnx::pre_link_args(nto_qnx::ApiVariant::Default, nto_qnx::Arch::X86_64); - target.options.env = "nto71".into(); + target.options.env = Env::Nto71; target } diff --git a/compiler/rustc_target/src/spec/targets/x86_64_pc_nto_qnx710_iosock.rs b/compiler/rustc_target/src/spec/targets/x86_64_pc_nto_qnx710_iosock.rs index 8f4c4924a295..1e97ae6b7a08 100644 --- a/compiler/rustc_target/src/spec/targets/x86_64_pc_nto_qnx710_iosock.rs +++ b/compiler/rustc_target/src/spec/targets/x86_64_pc_nto_qnx710_iosock.rs @@ -1,5 +1,5 @@ -use crate::spec::Target; use crate::spec::base::nto_qnx; +use crate::spec::{Env, Target}; pub(crate) fn target() -> Target { let mut target = nto_qnx::x86_64(); @@ -7,6 +7,6 @@ pub(crate) fn target() -> Target { Some("x86 64-bit QNX Neutrino 7.1 RTOS with io-sock network stack".into()); target.options.pre_link_args = nto_qnx::pre_link_args(nto_qnx::ApiVariant::IoSock, nto_qnx::Arch::X86_64); - target.options.env = "nto71_iosock".into(); + target.options.env = Env::Nto71IoSock; target } diff --git a/compiler/rustc_target/src/spec/targets/x86_64_pc_nto_qnx800.rs b/compiler/rustc_target/src/spec/targets/x86_64_pc_nto_qnx800.rs index d91a94a2ba55..bd98df621db1 100644 --- a/compiler/rustc_target/src/spec/targets/x86_64_pc_nto_qnx800.rs +++ b/compiler/rustc_target/src/spec/targets/x86_64_pc_nto_qnx800.rs @@ -1,11 +1,11 @@ -use crate::spec::Target; use crate::spec::base::nto_qnx; +use crate::spec::{Env, Target}; pub(crate) fn target() -> Target { let mut target = nto_qnx::x86_64(); target.metadata.description = Some("x86 64-bit QNX Neutrino 8.0 RTOS".into()); target.options.pre_link_args = nto_qnx::pre_link_args(nto_qnx::ApiVariant::Default, nto_qnx::Arch::X86_64); - target.options.env = "nto80".into(); + target.options.env = Env::Nto80; target } diff --git a/compiler/rustc_target/src/spec/targets/xtensa_esp32_espidf.rs b/compiler/rustc_target/src/spec/targets/xtensa_esp32_espidf.rs index d909271e218e..4199c410dc7e 100644 --- a/compiler/rustc_target/src/spec/targets/xtensa_esp32_espidf.rs +++ b/compiler/rustc_target/src/spec/targets/xtensa_esp32_espidf.rs @@ -1,7 +1,7 @@ use rustc_abi::Endian; use crate::spec::base::xtensa; -use crate::spec::{Arch, Target, TargetMetadata, TargetOptions, cvs}; +use crate::spec::{Arch, Env, Target, TargetMetadata, TargetOptions, cvs}; pub(crate) fn target() -> Target { Target { @@ -16,7 +16,7 @@ pub(crate) fn target() -> Target { c_int_width: 32, families: cvs!["unix"], os: "espidf".into(), - env: "newlib".into(), + env: Env::Newlib, vendor: "espressif".into(), executables: true, diff --git a/compiler/rustc_target/src/spec/targets/xtensa_esp32s2_espidf.rs b/compiler/rustc_target/src/spec/targets/xtensa_esp32s2_espidf.rs index b1ed0ffe264e..cca6a124ec63 100644 --- a/compiler/rustc_target/src/spec/targets/xtensa_esp32s2_espidf.rs +++ b/compiler/rustc_target/src/spec/targets/xtensa_esp32s2_espidf.rs @@ -1,7 +1,7 @@ use rustc_abi::Endian; use crate::spec::base::xtensa; -use crate::spec::{Arch, Target, TargetMetadata, TargetOptions, cvs}; +use crate::spec::{Arch, Env, Target, TargetMetadata, TargetOptions, cvs}; pub(crate) fn target() -> Target { Target { @@ -16,7 +16,7 @@ pub(crate) fn target() -> Target { c_int_width: 32, families: cvs!["unix"], os: "espidf".into(), - env: "newlib".into(), + env: Env::Newlib, vendor: "espressif".into(), executables: true, diff --git a/compiler/rustc_target/src/spec/targets/xtensa_esp32s3_espidf.rs b/compiler/rustc_target/src/spec/targets/xtensa_esp32s3_espidf.rs index 8e1fee4ad657..7038222b9571 100644 --- a/compiler/rustc_target/src/spec/targets/xtensa_esp32s3_espidf.rs +++ b/compiler/rustc_target/src/spec/targets/xtensa_esp32s3_espidf.rs @@ -1,7 +1,7 @@ use rustc_abi::Endian; use crate::spec::base::xtensa; -use crate::spec::{Arch, Target, TargetMetadata, TargetOptions, cvs}; +use crate::spec::{Arch, Env, Target, TargetMetadata, TargetOptions, cvs}; pub(crate) fn target() -> Target { Target { @@ -16,7 +16,7 @@ pub(crate) fn target() -> Target { c_int_width: 32, families: cvs!["unix"], os: "espidf".into(), - env: "newlib".into(), + env: Env::Newlib, vendor: "espressif".into(), executables: true, diff --git a/src/tools/miri/src/shims/windows/foreign_items.rs b/src/tools/miri/src/shims/windows/foreign_items.rs index 21c9022737bc..c824147ad4be 100644 --- a/src/tools/miri/src/shims/windows/foreign_items.rs +++ b/src/tools/miri/src/shims/windows/foreign_items.rs @@ -6,7 +6,7 @@ use rustc_abi::{Align, CanonAbi, Size, X86Call}; use rustc_middle::ty::Ty; use rustc_span::Symbol; use rustc_target::callconv::FnAbi; -use rustc_target::spec::Arch; +use rustc_target::spec::{Arch, Env}; use self::shims::windows::handle::{Handle, PseudoHandle}; use crate::shims::os_str::bytes_to_os_str; @@ -826,7 +826,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { // It was originally specified as part of the Itanium C++ ABI: // https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html#base-throw. // MinGW implements _Unwind_RaiseException on top of SEH exceptions. - if this.tcx.sess.target.env != "gnu" { + if this.tcx.sess.target.env != Env::Gnu { throw_unsup_format!( "`_Unwind_RaiseException` is not supported on non-MinGW Windows", );