Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC3239: Implement cfg(target) - Part 1 #96909

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/rustc_error_codes/src/error_codes/E0455.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" {}
```

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "...")`.
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
(
Expand Down
12 changes: 9 additions & 3 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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))));
Expand Down Expand Up @@ -1031,6 +1034,7 @@ impl CrateCheckConfig {
// rustc
sym::unix,
sym::windows,
sym::target,
sym::target_os,
sym::target_family,
sym::target_arch,
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ symbols! {
cfg_macro,
cfg_panic,
cfg_sanitize,
cfg_target,
cfg_target_abi,
cfg_target_feature,
cfg_target_has_atomic,
Expand Down Expand Up @@ -1374,6 +1375,7 @@ symbols! {
sym,
sync,
t32,
target,
target_abi,
target_arch,
target_endian,
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/cfg/cfg-target.rs
Original file line number Diff line number Diff line change
@@ -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() {
}
9 changes: 9 additions & 0 deletions src/test/ui/check-cfg/well-known-names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
// 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() {}

#[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() {}
Expand Down
16 changes: 12 additions & 4 deletions src/test/ui/check-cfg/well-known-names.stderr
Original file line number Diff line number Diff line change
@@ -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")]
| ---------^^^^^^^^^^
Expand All @@ -9,18 +9,26 @@ 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")]
| --------^^^^^^^^
| |
| 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

13 changes: 13 additions & 0 deletions src/test/ui/feature-gates/feature-gate-cfg-target.rs
Original file line number Diff line number Diff line change
@@ -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
}
39 changes: 39 additions & 0 deletions src/test/ui/feature-gates/feature-gate-cfg-target.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/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`.