diff --git a/compiler/rustc_error_codes/src/error_codes/E0455.md b/compiler/rustc_error_codes/src/error_codes/E0455.md index 84689b3ece610..39ed1f21e195c 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0455.md +++ b/compiler/rustc_error_codes/src/error_codes/E0455.md @@ -11,7 +11,10 @@ Erroneous code example: To solve this error you can use conditional compilation: ``` -#[cfg_attr(target="macos", link(name = "FooCoreServices", kind = "framework"))] +#[cfg_attr( + target_os="macos", + link(name = "FooCoreServices", kind = "framework") +)] extern "C" {} ``` diff --git a/compiler/rustc_feature/src/active.rs b/compiler/rustc_feature/src/active.rs index 520769d308e66..45fc0899c946a 100644 --- a/compiler/rustc_feature/src/active.rs +++ b/compiler/rustc_feature/src/active.rs @@ -317,6 +317,8 @@ declare_features! ( (incomplete, capture_disjoint_fields, "1.49.0", Some(53488), None), /// Allows the use of `#[cfg(sanitize = "option")]`; set when -Zsanitizer is used. (active, cfg_sanitize, "1.41.0", Some(39699), None), + /// Allows `cfg(target = "...")`. + (active, cfg_target, "1.62.0", Some(96901), None), /// Allows `cfg(target_abi = "...")`. (active, cfg_target_abi, "1.55.0", Some(80970), None), /// Allows `cfg(target_has_atomic_load_store = "...")`. diff --git a/compiler/rustc_feature/src/builtin_attrs.rs b/compiler/rustc_feature/src/builtin_attrs.rs index c5f42aa7af724..15b30fd894b86 100644 --- a/compiler/rustc_feature/src/builtin_attrs.rs +++ b/compiler/rustc_feature/src/builtin_attrs.rs @@ -24,6 +24,7 @@ pub type GatedCfg = (Symbol, Symbol, GateFn); /// `cfg(...)`'s that are feature gated. const GATED_CFGS: &[GatedCfg] = &[ // (name in cfg, feature, function to check if the feature is enabled) + (sym::target, sym::cfg_target, cfg_fn!(cfg_target)), (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)), ( diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 530c1a06f8f47..daa13ad5b73f1 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -894,6 +894,7 @@ pub const fn default_lib_output() -> CrateType { fn default_configuration(sess: &Session) -> CrateConfig { // NOTE: This should be kept in sync with `CrateCheckConfig::fill_well_known` below. + let tar = &sess.opts.target_triple.triple(); let end = &sess.target.endian; let arch = &sess.target.arch; let wordsz = sess.target.pointer_width.to_string(); @@ -909,8 +910,10 @@ fn default_configuration(sess: &Session) -> CrateConfig { }); let mut ret = FxHashSet::default(); - ret.reserve(7); // the minimum number of insertions + ret.reserve(9); // the minimum number of insertions + // Target bindings. + ret.insert((sym::target, Some(Symbol::intern(tar)))); ret.insert((sym::target_os, Some(Symbol::intern(os)))); for fam in sess.target.families.as_ref() { ret.insert((sym::target_family, Some(Symbol::intern(fam)))); @@ -1031,6 +1034,7 @@ impl CrateCheckConfig { // rustc sym::unix, sym::windows, + sym::target, sym::target_os, sym::target_family, sym::target_arch, @@ -1120,9 +1124,11 @@ impl CrateCheckConfig { .extend(atomic_values); // Target specific values - for target in - TARGETS.iter().map(|target| Target::expect_builtin(&TargetTriple::from_triple(target))) + for (name, target) in TARGETS + .iter() + .map(|target| (target, Target::expect_builtin(&TargetTriple::from_triple(target)))) { + self.values_valid.entry(sym::target).or_default().insert(Symbol::intern(&name)); self.values_valid .entry(sym::target_os) .or_default() diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index d6885bebc320e..788477386325d 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -426,6 +426,7 @@ symbols! { cfg_macro, cfg_panic, cfg_sanitize, + cfg_target, cfg_target_abi, cfg_target_feature, cfg_target_has_atomic, @@ -1374,6 +1375,7 @@ symbols! { sym, sync, t32, + target, target_abi, target_arch, target_endian, diff --git a/src/test/ui/cfg/cfg-target.rs b/src/test/ui/cfg/cfg-target.rs new file mode 100644 index 0000000000000..cc42c6738b173 --- /dev/null +++ b/src/test/ui/cfg/cfg-target.rs @@ -0,0 +1,10 @@ +// run-pass +#![feature(cfg_target)] + +#[cfg(target = "x86_64-unknown-linux-gnu")] +pub fn main() { +} + +#[cfg(not(target = "x86_64-unknown-linux-gnu"))] +pub fn main() { +} diff --git a/src/test/ui/check-cfg/well-known-names.rs b/src/test/ui/check-cfg/well-known-names.rs index e57fb69a1e05f..db21120645d97 100644 --- a/src/test/ui/check-cfg/well-known-names.rs +++ b/src/test/ui/check-cfg/well-known-names.rs @@ -3,6 +3,8 @@ // check-pass // compile-flags: --check-cfg=names() -Z unstable-options +#![feature(cfg_target)] + #[cfg(target_oz = "linux")] //~^ WARNING unexpected `cfg` condition name fn target_os_misspell() {} @@ -10,6 +12,13 @@ fn target_os_misspell() {} #[cfg(target_os = "linux")] fn target_os() {} +#[cfg(targeT = "x86_64-unknown-linux-gnu")] +//~^ WARNING unexpected `cfg` condition name +fn target_misspell() {} + +#[cfg(target = "x86_64-unknown-linux-gnu")] +fn target() {} + #[cfg(features = "foo")] //~^ WARNING unexpected `cfg` condition name fn feature_misspell() {} diff --git a/src/test/ui/check-cfg/well-known-names.stderr b/src/test/ui/check-cfg/well-known-names.stderr index bdbe4d29d30fe..5b43b5b5ebed9 100644 --- a/src/test/ui/check-cfg/well-known-names.stderr +++ b/src/test/ui/check-cfg/well-known-names.stderr @@ -1,5 +1,5 @@ warning: unexpected `cfg` condition name - --> $DIR/well-known-names.rs:6:7 + --> $DIR/well-known-names.rs:8:7 | LL | #[cfg(target_oz = "linux")] | ---------^^^^^^^^^^ @@ -9,7 +9,15 @@ LL | #[cfg(target_oz = "linux")] = note: `#[warn(unexpected_cfgs)]` on by default warning: unexpected `cfg` condition name - --> $DIR/well-known-names.rs:13:7 + --> $DIR/well-known-names.rs:15:7 + | +LL | #[cfg(targeT = "x86_64-unknown-linux-gnu")] + | ------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | help: did you mean: `target` + +warning: unexpected `cfg` condition name + --> $DIR/well-known-names.rs:22:7 | LL | #[cfg(features = "foo")] | --------^^^^^^^^ @@ -17,10 +25,10 @@ LL | #[cfg(features = "foo")] | help: did you mean: `feature` warning: unexpected `cfg` condition name - --> $DIR/well-known-names.rs:20:7 + --> $DIR/well-known-names.rs:29:7 | LL | #[cfg(uniw)] | ^^^^ help: did you mean: `unix` -warning: 3 warnings emitted +warning: 4 warnings emitted diff --git a/src/test/ui/feature-gates/feature-gate-cfg-target.rs b/src/test/ui/feature-gates/feature-gate-cfg-target.rs new file mode 100644 index 0000000000000..ad4ae9d337032 --- /dev/null +++ b/src/test/ui/feature-gates/feature-gate-cfg-target.rs @@ -0,0 +1,13 @@ +#[cfg(target = "x")] //~ ERROR `cfg(target)` is experimental +struct Foo(u64, u64); + +#[cfg_attr(target = "x", x)] //~ ERROR `cfg(target)` is experimental +struct Bar(u64, u64); + +#[cfg(not(any(all(target = "x"))))] //~ ERROR `cfg(target)` is experimental +fn foo() {} + +fn main() { + cfg!(target = "x"); + //~^ ERROR `cfg(target)` is experimental and subject to change +} diff --git a/src/test/ui/feature-gates/feature-gate-cfg-target.stderr b/src/test/ui/feature-gates/feature-gate-cfg-target.stderr new file mode 100644 index 0000000000000..a33ae31a030c7 --- /dev/null +++ b/src/test/ui/feature-gates/feature-gate-cfg-target.stderr @@ -0,0 +1,39 @@ +error[E0658]: `cfg(target)` is experimental and subject to change + --> $DIR/feature-gate-cfg-target.rs:1:7 + | +LL | #[cfg(target = "x")] + | ^^^^^^^^^^^^ + | + = note: see issue #96901 for more information + = help: add `#![feature(cfg_target)]` to the crate attributes to enable + +error[E0658]: `cfg(target)` is experimental and subject to change + --> $DIR/feature-gate-cfg-target.rs:4:12 + | +LL | #[cfg_attr(target = "x", x)] + | ^^^^^^^^^^^^ + | + = note: see issue #96901 for more information + = help: add `#![feature(cfg_target)]` to the crate attributes to enable + +error[E0658]: `cfg(target)` is experimental and subject to change + --> $DIR/feature-gate-cfg-target.rs:7:19 + | +LL | #[cfg(not(any(all(target = "x"))))] + | ^^^^^^^^^^^^ + | + = note: see issue #96901 for more information + = help: add `#![feature(cfg_target)]` to the crate attributes to enable + +error[E0658]: `cfg(target)` is experimental and subject to change + --> $DIR/feature-gate-cfg-target.rs:11:10 + | +LL | cfg!(target = "x"); + | ^^^^^^^^^^^^ + | + = note: see issue #96901 for more information + = help: add `#![feature(cfg_target)]` to the crate attributes to enable + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0658`.