Skip to content

Commit 86c74a4

Browse files
committed
rustc_target: introduce Abi
Improve type safety by using an enum rather than strings.
1 parent a1122a1 commit 86c74a4

File tree

114 files changed

+308
-263
lines changed

Some content is hidden

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

114 files changed

+308
-263
lines changed

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 2 additions & 2 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};
5353

5454
pub use crate::config::*;
5555
use crate::prelude::*;
@@ -186,7 +186,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
186186
let has_reliable_f16_f128 = !(sess.target.arch == Arch::X86_64
187187
&& sess.target.os == "windows"
188188
&& sess.target.env == "gnu"
189-
&& sess.target.abi != "llvm");
189+
&& sess.target.abi != Abi::Llvm);
190190

191191
TargetConfig {
192192
target_features,

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 2 additions & 2 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, HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel,
3333
};
3434
use smallvec::SmallVec;
3535

@@ -337,7 +337,7 @@ pub(crate) unsafe fn create_module<'ll>(
337337
if sess.target.is_like_msvc
338338
|| (sess.target.options.os == "windows"
339339
&& sess.target.options.env == "gnu"
340-
&& sess.target.options.abi == "llvm")
340+
&& sess.target.options.abi == Abi::Llvm)
341341
{
342342
match sess.opts.cg.control_flow_guard {
343343
CFGuard::Disabled => {}

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ 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::{Abi, Arch, MergeFunctions, PanicStrategy, SmallDataThresholdSupport};
1919
use smallvec::{SmallVec, smallvec};
2020

2121
use crate::back::write::create_informational_target_machine;
@@ -353,7 +353,7 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
353353
let target_arch = &sess.target.arch;
354354
let target_os = sess.target.options.os.as_ref();
355355
let target_env = sess.target.options.env.as_ref();
356-
let target_abi = sess.target.options.abi.as_ref();
356+
let target_abi = &sess.target.options.abi;
357357
let target_pointer_width = sess.target.pointer_width;
358358
let version = get_version();
359359
let lt_20_1_1 = version < (20, 1, 1);
@@ -371,7 +371,7 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
371371
// Selection failure <https://github.com/llvm/llvm-project/issues/50374> (fixed in llvm21)
372372
(Arch::S390x, _) if lt_21_0_0 => false,
373373
// 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,
374+
(Arch::X86_64, "windows") if target_env == "gnu" && *target_abi != Abi::Llvm => false,
375375
// Infinite recursion <https://github.com/llvm/llvm-project/issues/97981>
376376
(Arch::CSky, _) => false,
377377
(Arch::Hexagon, _) if lt_21_0_0 => false, // (fixed in llvm21)
@@ -403,7 +403,7 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
403403
// not fail if our compiler-builtins is linked. (fixed in llvm21)
404404
(Arch::X86, _) if lt_21_0_0 => false,
405405
// 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,
406+
(Arch::X86_64, "windows") if target_env == "gnu" && *target_abi != Abi::Llvm => false,
407407
// There are no known problems on other platforms, so the only requirement is that symbols
408408
// are available. `compiler-builtins` provides all symbols required for core `f128`
409409
// support, so this should work for everything else.

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/link.rs

Lines changed: 2 additions & 2 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+
Abi, BinaryFormat, Cc, LinkOutputKind, LinkSelfContainedComponents, LinkSelfContainedDefault,
5050
LinkerFeatures, LinkerFlavor, LinkerFlavorCli, Lld, RelocModel, RelroLevel, SanitizerSet,
5151
SplitDebuginfo,
5252
};
@@ -1819,7 +1819,7 @@ fn self_contained_components(
18191819
LinkSelfContainedDefault::InferredForMusl => sess.crt_static(Some(crate_type)),
18201820
LinkSelfContainedDefault::InferredForMingw => {
18211821
sess.host == sess.target
1822-
&& sess.target.abi != "uwp"
1822+
&& sess.target.abi != Abi::Uwp
18231823
&& detect_self_contained_mingw(sess, linker)
18241824
}
18251825
}

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::middle::exported_symbols::{
1717
use rustc_middle::ty::TyCtxt;
1818
use rustc_session::Session;
1919
use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip};
20-
use rustc_target::spec::{Arch, Cc, LinkOutputKind, LinkerFlavor, Lld};
20+
use rustc_target::spec::{Abi, Arch, Cc, LinkOutputKind, LinkerFlavor, Lld};
2121
use tracing::{debug, warn};
2222

2323
use super::command::Command;
@@ -83,7 +83,7 @@ pub(crate) fn get_linker<'a>(
8383
// To comply with the Windows App Certification Kit,
8484
// MSVC needs to link with the Store versions of the runtime libraries (vcruntime, msvcrt, etc).
8585
let t = &sess.target;
86-
if matches!(flavor, LinkerFlavor::Msvc(..)) && t.abi == "uwp" {
86+
if matches!(flavor, LinkerFlavor::Msvc(..)) && t.abi == Abi::Uwp {
8787
if let Some(ref tool) = msvc_tool {
8888
let original_path = tool.path();
8989
if let Some(root_lib_path) = original_path.ancestors().nth(4) {
@@ -134,7 +134,7 @@ pub(crate) fn get_linker<'a>(
134134

135135
// FIXME: Move `/LIBPATH` addition for uwp targets from the linker construction
136136
// to the linker args construction.
137-
assert!(cmd.get_args().is_empty() || sess.target.abi == "uwp");
137+
assert!(cmd.get_args().is_empty() || sess.target.abi == Abi::Uwp);
138138
match flavor {
139139
LinkerFlavor::Unix(Cc::No) if sess.target.os == "l4re" => {
140140
Box::new(L4Bender::new(cmd, sess)) as Box<dyn Linker>

compiler/rustc_codegen_ssa/src/back/metadata.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_metadata::fs::METADATA_FILENAME;
2020
use rustc_middle::bug;
2121
use rustc_session::Session;
2222
use rustc_span::sym;
23-
use rustc_target::spec::{RelocModel, Target, ef_avr_arch};
23+
use rustc_target::spec::{Abi, RelocModel, Target, ef_avr_arch};
2424
use tracing::debug;
2525

2626
use super::apple;
@@ -379,11 +379,11 @@ pub(super) fn elf_e_flags(architecture: Architecture, sess: &Session) -> u32 {
379379
}
380380
}
381381
Architecture::Csky => {
382-
let e_flags = match sess.target.options.abi.as_ref() {
383-
"abiv2" => elf::EF_CSKY_ABIV2,
384-
_ => elf::EF_CSKY_ABIV1,
385-
};
386-
e_flags
382+
if matches!(sess.target.options.abi, Abi::AbiV2) {
383+
elf::EF_CSKY_ABIV2
384+
} else {
385+
elf::EF_CSKY_ABIV1
386+
}
387387
}
388388
Architecture::PowerPc64 => {
389389
const EF_PPC64_ABI_UNKNOWN: u32 = 0;

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::Target;
10+
use rustc_target::spec::{Abi, Target};
1111

1212
use crate::traits::*;
1313

@@ -171,7 +171,7 @@ pub fn asm_const_to_str<'tcx>(
171171
}
172172

173173
pub fn is_mingw_gnu_toolchain(target: &Target) -> bool {
174-
target.os == "windows" && target.env == "gnu" && target.abi.is_empty()
174+
target.os == "windows" && target.env == "gnu" && target.abi == Abi::Unspecified
175175
}
176176

177177
pub fn i686_decorated_name(

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};
18+
use rustc_target::spec::{Abi, Arch, BinaryFormat, LinkSelfContainedComponents};
1919

2020
use crate::errors;
2121

@@ -67,7 +67,7 @@ pub fn walk_native_lib_search_dirs<R>(
6767
// FIXME: On AIX this also has the side-effect of making the list of library search paths
6868
// non-empty, which is needed or the linker may decide to record the LIBPATH env, if
6969
// defined, as the search path instead of appending the default search paths.
70-
if sess.target.abi == "fortanix"
70+
if sess.target.abi == Abi::Fortanix
7171
|| sess.target.os == "linux"
7272
|| sess.target.os == "fuchsia"
7373
|| sess.target.is_like_aix

compiler/rustc_session/src/config/cfg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub(crate) fn default_configuration(sess: &Session) -> Cfg {
239239
ins_none!(sym::sanitizer_cfi_normalize_integers);
240240
}
241241

242-
ins_str!(sym::target_abi, &sess.target.abi);
242+
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());
245245
ins_str!(sym::target_env, &sess.target.env);
@@ -447,7 +447,7 @@ impl CheckCfg {
447447
};
448448

449449
for target in Target::builtins().chain(iter::once(current_target.clone())) {
450-
values_target_abi.insert(Symbol::intern(&target.options.abi));
450+
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()));
453453
values_target_env.insert(Symbol::intern(&target.options.env));

0 commit comments

Comments
 (0)