From 84d6e8aed3af940c53e49e342575e91f502c7bd4 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Tue, 6 Jul 2021 20:54:54 -0700 Subject: [PATCH] Implement cfg(target_abi) (RFC 2992) Add an `abi` field to `TargetOptions`, defaulting to "". Support using `cfg(target_abi = "...")` for conditional compilation on that field. Gated by `feature(cfg_target_abi)`. Add a test for `target_abi`, and a test for the feature gate. Add `target_abi` to tidy as a platform-specific cfg. This does not add an abi to any existing target. --- compiler/rustc_feature/src/active.rs | 3 ++ compiler/rustc_feature/src/builtin_attrs.rs | 1 + compiler/rustc_session/src/config.rs | 4 +- compiler/rustc_span/src/symbol.rs | 2 + compiler/rustc_target/src/spec/mod.rs | 6 +++ src/test/ui/cfg/cfg-target-abi.rs | 10 +++++ .../feature-gate-cfg-target-abi.rs | 11 ++++++ .../feature-gate-cfg-target-abi.stderr | 39 +++++++++++++++++++ src/tools/tidy/src/pal.rs | 1 + 9 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/cfg/cfg-target-abi.rs create mode 100644 src/test/ui/feature-gates/feature-gate-cfg-target-abi.rs create mode 100644 src/test/ui/feature-gates/feature-gate-cfg-target-abi.stderr diff --git a/compiler/rustc_feature/src/active.rs b/compiler/rustc_feature/src/active.rs index f045a75cdc8..03f06466683 100644 --- a/compiler/rustc_feature/src/active.rs +++ b/compiler/rustc_feature/src/active.rs @@ -687,6 +687,9 @@ pub fn set(&self, features: &mut Features, span: Span) { /// Allows qualified paths in struct expressions, struct patterns and tuple struct patterns. (active, more_qualified_paths, "1.54.0", Some(80080), None), + /// Allows `cfg(target_abi = "...")`. + (active, cfg_target_abi, "1.55.0", Some(80970), None), + // ------------------------------------------------------------------------- // feature-group-end: actual feature gates // ------------------------------------------------------------------------- diff --git a/compiler/rustc_feature/src/builtin_attrs.rs b/compiler/rustc_feature/src/builtin_attrs.rs index 259a6328a22..94075656754 100644 --- a/compiler/rustc_feature/src/builtin_attrs.rs +++ b/compiler/rustc_feature/src/builtin_attrs.rs @@ -23,6 +23,7 @@ macro_rules! cfg_fn { /// `cfg(...)`'s that are feature gated. const GATED_CFGS: &[GatedCfg] = &[ // (name in cfg, feature, function to check if the feature is enabled) + (sym::target_abi, sym::cfg_target_abi, cfg_fn!(cfg_target_abi)), (sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)), (sym::target_has_atomic, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)), (sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)), diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 88eaa7fe329..b444f66258a 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -805,6 +805,7 @@ fn default_configuration(sess: &Session) -> CrateConfig { let wordsz = sess.target.pointer_width.to_string(); let os = &sess.target.os; let env = &sess.target.env; + let abi = &sess.target.abi; let vendor = &sess.target.vendor; let min_atomic_width = sess.target.min_atomic_width(); let max_atomic_width = sess.target.max_atomic_width(); @@ -814,7 +815,7 @@ fn default_configuration(sess: &Session) -> CrateConfig { }); let mut ret = FxHashSet::default(); - ret.reserve(6); // the minimum number of insertions + ret.reserve(7); // the minimum number of insertions // Target bindings. ret.insert((sym::target_os, Some(Symbol::intern(os)))); for fam in &sess.target.families { @@ -829,6 +830,7 @@ fn default_configuration(sess: &Session) -> CrateConfig { ret.insert((sym::target_endian, Some(Symbol::intern(end.as_str())))); ret.insert((sym::target_pointer_width, Some(Symbol::intern(&wordsz)))); ret.insert((sym::target_env, Some(Symbol::intern(env)))); + ret.insert((sym::target_abi, Some(Symbol::intern(abi)))); ret.insert((sym::target_vendor, Some(Symbol::intern(vendor)))); if sess.target.has_elf_tls { ret.insert((sym::target_thread_local, None)); diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index be4f12c6d1c..6d14662dc77 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -353,6 +353,7 @@ cfg_eval, cfg_panic, cfg_sanitize, + cfg_target_abi, cfg_target_feature, cfg_target_has_atomic, cfg_target_thread_local, @@ -1199,6 +1200,7 @@ sync, sync_trait, t32, + target_abi, target_arch, target_endian, target_env, diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 0a5f8c17c63..aa7dbeb16f2 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -1025,6 +1025,9 @@ pub struct TargetOptions { pub os: String, /// Environment name to use for conditional compilation (`target_env`). Defaults to "". pub env: String, + /// ABI name to distinguish multiple ABIs on the same OS and architecture. For instance, `"eabi"` + /// or `"eabihf"`. Defaults to "". + pub abi: String, /// Vendor name to use for conditional compilation (`target_vendor`). Defaults to "unknown". pub vendor: String, /// Default linker flavor used if `-C linker-flavor` or `-C linker` are not passed @@ -1342,6 +1345,7 @@ fn default() -> TargetOptions { c_int_width: "32".to_string(), os: "none".to_string(), env: String::new(), + abi: String::new(), vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, linker: option_env!("CFG_DEFAULT_LINKER").map(|s| s.to_string()), @@ -1919,6 +1923,7 @@ macro_rules! key { key!(c_int_width = "target-c-int-width"); key!(os); key!(env); + key!(abi); key!(vendor); key!(linker_flavor, LinkerFlavor)?; key!(linker, optional); @@ -2152,6 +2157,7 @@ macro_rules! target_option_val { target_option_val!(c_int_width, "target-c-int-width"); target_option_val!(os); target_option_val!(env); + target_option_val!(abi); target_option_val!(vendor); target_option_val!(linker_flavor); target_option_val!(linker); diff --git a/src/test/ui/cfg/cfg-target-abi.rs b/src/test/ui/cfg/cfg-target-abi.rs new file mode 100644 index 00000000000..acc570fc843 --- /dev/null +++ b/src/test/ui/cfg/cfg-target-abi.rs @@ -0,0 +1,10 @@ +// run-pass +#![feature(cfg_target_abi)] + +#[cfg(target_abi = "eabihf")] +pub fn main() { +} + +#[cfg(not(target_abi = "eabihf"))] +pub fn main() { +} diff --git a/src/test/ui/feature-gates/feature-gate-cfg-target-abi.rs b/src/test/ui/feature-gates/feature-gate-cfg-target-abi.rs new file mode 100644 index 00000000000..f2651493980 --- /dev/null +++ b/src/test/ui/feature-gates/feature-gate-cfg-target-abi.rs @@ -0,0 +1,11 @@ +#[cfg(target_abi = "x")] //~ ERROR `cfg(target_abi)` is experimental +#[cfg_attr(target_abi = "x", x)] //~ ERROR `cfg(target_abi)` is experimental +struct Foo(u64, u64); + +#[cfg(not(any(all(target_abi = "x"))))] //~ ERROR `cfg(target_abi)` is experimental +fn foo() {} + +fn main() { + cfg!(target_abi = "x"); + //~^ ERROR `cfg(target_abi)` is experimental and subject to change +} diff --git a/src/test/ui/feature-gates/feature-gate-cfg-target-abi.stderr b/src/test/ui/feature-gates/feature-gate-cfg-target-abi.stderr new file mode 100644 index 00000000000..ed8cbcbe4f0 --- /dev/null +++ b/src/test/ui/feature-gates/feature-gate-cfg-target-abi.stderr @@ -0,0 +1,39 @@ +error[E0658]: `cfg(target_abi)` is experimental and subject to change + --> $DIR/feature-gate-cfg-target-abi.rs:2:12 + | +LL | #[cfg_attr(target_abi = "x", x)] + | ^^^^^^^^^^^^^^^^ + | + = note: see issue #80970 for more information + = help: add `#![feature(cfg_target_abi)]` to the crate attributes to enable + +error[E0658]: `cfg(target_abi)` is experimental and subject to change + --> $DIR/feature-gate-cfg-target-abi.rs:1:7 + | +LL | #[cfg(target_abi = "x")] + | ^^^^^^^^^^^^^^^^ + | + = note: see issue #80970 for more information + = help: add `#![feature(cfg_target_abi)]` to the crate attributes to enable + +error[E0658]: `cfg(target_abi)` is experimental and subject to change + --> $DIR/feature-gate-cfg-target-abi.rs:5:19 + | +LL | #[cfg(not(any(all(target_abi = "x"))))] + | ^^^^^^^^^^^^^^^^ + | + = note: see issue #80970 for more information + = help: add `#![feature(cfg_target_abi)]` to the crate attributes to enable + +error[E0658]: `cfg(target_abi)` is experimental and subject to change + --> $DIR/feature-gate-cfg-target-abi.rs:9:10 + | +LL | cfg!(target_abi = "x"); + | ^^^^^^^^^^^^^^^^ + | + = note: see issue #80970 for more information + = help: add `#![feature(cfg_target_abi)]` to the crate attributes to enable + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0658`. diff --git a/src/tools/tidy/src/pal.rs b/src/tools/tidy/src/pal.rs index 31cdc6865a4..24a10018779 100644 --- a/src/tools/tidy/src/pal.rs +++ b/src/tools/tidy/src/pal.rs @@ -122,6 +122,7 @@ fn check_cfgs( let contains_platform_specific_cfg = cfg.contains("target_os") || cfg.contains("target_env") + || cfg.contains("target_abi") || cfg.contains("target_vendor") || cfg.contains("unix") || cfg.contains("windows"); -- 2.44.0