Skip to content

Commit 6e4c782

Browse files
committed
RFC3239: Implement cfg(target)
1 parent c51871c commit 6e4c782

File tree

10 files changed

+101
-8
lines changed

10 files changed

+101
-8
lines changed

Diff for: compiler/rustc_error_codes/src/error_codes/E0455.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ Erroneous code example:
1111
To solve this error you can use conditional compilation:
1212

1313
```
14-
#[cfg_attr(target="macos", link(name = "FooCoreServices", kind = "framework"))]
14+
#[cfg_attr(
15+
target_os="macos",
16+
link(name = "FooCoreServices", kind = "framework")
17+
)]
1518
extern "C" {}
1619
```
1720

Diff for: compiler/rustc_feature/src/active.rs

+2
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ declare_features! (
317317
(incomplete, capture_disjoint_fields, "1.49.0", Some(53488), None),
318318
/// Allows the use of `#[cfg(sanitize = "option")]`; set when -Zsanitizer is used.
319319
(active, cfg_sanitize, "1.41.0", Some(39699), None),
320+
/// Allows `cfg(target = "...")`.
321+
(active, cfg_target, "1.62.0", Some(96901), None),
320322
/// Allows `cfg(target_abi = "...")`.
321323
(active, cfg_target_abi, "1.55.0", Some(80970), None),
322324
/// Allows `cfg(target_has_atomic_load_store = "...")`.

Diff for: compiler/rustc_feature/src/builtin_attrs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub type GatedCfg = (Symbol, Symbol, GateFn);
2424
/// `cfg(...)`'s that are feature gated.
2525
const GATED_CFGS: &[GatedCfg] = &[
2626
// (name in cfg, feature, function to check if the feature is enabled)
27+
(sym::target, sym::cfg_target, cfg_fn!(cfg_target)),
2728
(sym::target_abi, sym::cfg_target_abi, cfg_fn!(cfg_target_abi)),
2829
(sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)),
2930
(

Diff for: compiler/rustc_session/src/config.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ pub const fn default_lib_output() -> CrateType {
894894

895895
fn default_configuration(sess: &Session) -> CrateConfig {
896896
// NOTE: This should be kept in sync with `CrateCheckConfig::fill_well_known` below.
897+
let tar = &sess.opts.target_triple.triple();
897898
let end = &sess.target.endian;
898899
let arch = &sess.target.arch;
899900
let wordsz = sess.target.pointer_width.to_string();
@@ -909,8 +910,10 @@ fn default_configuration(sess: &Session) -> CrateConfig {
909910
});
910911

911912
let mut ret = FxHashSet::default();
912-
ret.reserve(7); // the minimum number of insertions
913+
ret.reserve(9); // the minimum number of insertions
914+
913915
// Target bindings.
916+
ret.insert((sym::target, Some(Symbol::intern(tar))));
914917
ret.insert((sym::target_os, Some(Symbol::intern(os))));
915918
for fam in sess.target.families.as_ref() {
916919
ret.insert((sym::target_family, Some(Symbol::intern(fam))));
@@ -1031,6 +1034,7 @@ impl CrateCheckConfig {
10311034
// rustc
10321035
sym::unix,
10331036
sym::windows,
1037+
sym::target,
10341038
sym::target_os,
10351039
sym::target_family,
10361040
sym::target_arch,
@@ -1120,9 +1124,11 @@ impl CrateCheckConfig {
11201124
.extend(atomic_values);
11211125

11221126
// Target specific values
1123-
for target in
1124-
TARGETS.iter().map(|target| Target::expect_builtin(&TargetTriple::from_triple(target)))
1127+
for (name, target) in TARGETS
1128+
.iter()
1129+
.map(|target| (target, Target::expect_builtin(&TargetTriple::from_triple(target))))
11251130
{
1131+
self.values_valid.entry(sym::target).or_default().insert(Symbol::intern(&name));
11261132
self.values_valid
11271133
.entry(sym::target_os)
11281134
.or_default()

Diff for: compiler/rustc_span/src/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ symbols! {
426426
cfg_macro,
427427
cfg_panic,
428428
cfg_sanitize,
429+
cfg_target,
429430
cfg_target_abi,
430431
cfg_target_feature,
431432
cfg_target_has_atomic,
@@ -1374,6 +1375,7 @@ symbols! {
13741375
sym,
13751376
sync,
13761377
t32,
1378+
target,
13771379
target_abi,
13781380
target_arch,
13791381
target_endian,

Diff for: src/test/ui/cfg/cfg-target.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// run-pass
2+
#![feature(cfg_target)]
3+
4+
#[cfg(target = "x86_64-unknown-linux-gnu")]
5+
pub fn main() {
6+
}
7+
8+
#[cfg(not(target = "x86_64-unknown-linux-gnu"))]
9+
pub fn main() {
10+
}

Diff for: src/test/ui/check-cfg/well-known-names.rs

+9
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@
33
// check-pass
44
// compile-flags: --check-cfg=names() -Z unstable-options
55

6+
#![feature(cfg_target)]
7+
68
#[cfg(target_oz = "linux")]
79
//~^ WARNING unexpected `cfg` condition name
810
fn target_os_misspell() {}
911

1012
#[cfg(target_os = "linux")]
1113
fn target_os() {}
1214

15+
#[cfg(targeT = "x86_64-unknown-linux-gnu")]
16+
//~^ WARNING unexpected `cfg` condition name
17+
fn target_misspell() {}
18+
19+
#[cfg(target = "x86_64-unknown-linux-gnu")]
20+
fn target() {}
21+
1322
#[cfg(features = "foo")]
1423
//~^ WARNING unexpected `cfg` condition name
1524
fn feature_misspell() {}

Diff for: src/test/ui/check-cfg/well-known-names.stderr

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: unexpected `cfg` condition name
2-
--> $DIR/well-known-names.rs:6:7
2+
--> $DIR/well-known-names.rs:8:7
33
|
44
LL | #[cfg(target_oz = "linux")]
55
| ---------^^^^^^^^^^
@@ -9,18 +9,26 @@ LL | #[cfg(target_oz = "linux")]
99
= note: `#[warn(unexpected_cfgs)]` on by default
1010

1111
warning: unexpected `cfg` condition name
12-
--> $DIR/well-known-names.rs:13:7
12+
--> $DIR/well-known-names.rs:15:7
13+
|
14+
LL | #[cfg(targeT = "x86_64-unknown-linux-gnu")]
15+
| ------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
| |
17+
| help: did you mean: `target`
18+
19+
warning: unexpected `cfg` condition name
20+
--> $DIR/well-known-names.rs:22:7
1321
|
1422
LL | #[cfg(features = "foo")]
1523
| --------^^^^^^^^
1624
| |
1725
| help: did you mean: `feature`
1826

1927
warning: unexpected `cfg` condition name
20-
--> $DIR/well-known-names.rs:20:7
28+
--> $DIR/well-known-names.rs:29:7
2129
|
2230
LL | #[cfg(uniw)]
2331
| ^^^^ help: did you mean: `unix`
2432

25-
warning: 3 warnings emitted
33+
warning: 4 warnings emitted
2634

Diff for: src/test/ui/feature-gates/feature-gate-cfg-target.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[cfg(target = "x")] //~ ERROR `cfg(target)` is experimental
2+
struct Foo(u64, u64);
3+
4+
#[cfg_attr(target = "x", x)] //~ ERROR `cfg(target)` is experimental
5+
struct Bar(u64, u64);
6+
7+
#[cfg(not(any(all(target = "x"))))] //~ ERROR `cfg(target)` is experimental
8+
fn foo() {}
9+
10+
fn main() {
11+
cfg!(target = "x");
12+
//~^ ERROR `cfg(target)` is experimental and subject to change
13+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0658]: `cfg(target)` is experimental and subject to change
2+
--> $DIR/feature-gate-cfg-target.rs:1:7
3+
|
4+
LL | #[cfg(target = "x")]
5+
| ^^^^^^^^^^^^
6+
|
7+
= note: see issue #96901 <https://github.com/rust-lang/rust/issues/96901> for more information
8+
= help: add `#![feature(cfg_target)]` to the crate attributes to enable
9+
10+
error[E0658]: `cfg(target)` is experimental and subject to change
11+
--> $DIR/feature-gate-cfg-target.rs:4:12
12+
|
13+
LL | #[cfg_attr(target = "x", x)]
14+
| ^^^^^^^^^^^^
15+
|
16+
= note: see issue #96901 <https://github.com/rust-lang/rust/issues/96901> for more information
17+
= help: add `#![feature(cfg_target)]` to the crate attributes to enable
18+
19+
error[E0658]: `cfg(target)` is experimental and subject to change
20+
--> $DIR/feature-gate-cfg-target.rs:7:19
21+
|
22+
LL | #[cfg(not(any(all(target = "x"))))]
23+
| ^^^^^^^^^^^^
24+
|
25+
= note: see issue #96901 <https://github.com/rust-lang/rust/issues/96901> for more information
26+
= help: add `#![feature(cfg_target)]` to the crate attributes to enable
27+
28+
error[E0658]: `cfg(target)` is experimental and subject to change
29+
--> $DIR/feature-gate-cfg-target.rs:11:10
30+
|
31+
LL | cfg!(target = "x");
32+
| ^^^^^^^^^^^^
33+
|
34+
= note: see issue #96901 <https://github.com/rust-lang/rust/issues/96901> for more information
35+
= help: add `#![feature(cfg_target)]` to the crate attributes to enable
36+
37+
error: aborting due to 4 previous errors
38+
39+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)