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

Implement RFC3695: Allow boolean literals as cfg predicates #14649

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ cargo-credential = { version = "0.4.2", path = "credential/cargo-credential" }
cargo-credential-libsecret = { version = "0.4.12", path = "credential/cargo-credential-libsecret" }
cargo-credential-macos-keychain = { version = "0.4.12", path = "credential/cargo-credential-macos-keychain" }
cargo-credential-wincred = { version = "0.4.12", path = "credential/cargo-credential-wincred" }
cargo-platform = { path = "crates/cargo-platform", version = "0.2.0" }
cargo-platform = { path = "crates/cargo-platform", version = "0.3.0" }
cargo-test-macro = { version = "0.4.1", path = "crates/cargo-test-macro" }
cargo-test-support = { version = "0.7.1", path = "crates/cargo-test-support" }
cargo-util = { version = "0.2.19", path = "crates/cargo-util" }
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-platform/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-platform"
version = "0.2.0"
version = "0.3.0"
edition.workspace = true
license.workspace = true
rust-version.workspace = true
Expand Down
12 changes: 11 additions & 1 deletion crates/cargo-platform/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub enum CfgExpr {
All(Vec<CfgExpr>),
Any(Vec<CfgExpr>),
Value(Cfg),
True,
False,
}

/// A cfg value.
Expand Down Expand Up @@ -147,6 +149,8 @@ impl CfgExpr {
CfgExpr::All(ref e) => e.iter().all(|e| e.matches(cfg)),
CfgExpr::Any(ref e) => e.iter().any(|e| e.matches(cfg)),
CfgExpr::Value(ref e) => cfg.contains(e),
CfgExpr::True => true,
CfgExpr::False => false,
}
}
}
Expand Down Expand Up @@ -174,6 +178,8 @@ impl fmt::Display for CfgExpr {
CfgExpr::All(ref e) => write!(f, "all({})", CommaSep(e)),
CfgExpr::Any(ref e) => write!(f, "any({})", CommaSep(e)),
CfgExpr::Value(ref e) => write!(f, "{}", e),
CfgExpr::True => write!(f, "true"),
CfgExpr::False => write!(f, "false"),
}
}
}
Expand Down Expand Up @@ -229,7 +235,11 @@ impl<'a> Parser<'a> {
self.eat(&Token::RightParen)?;
Ok(CfgExpr::Not(Box::new(e)))
}
Some(Ok(..)) => self.cfg().map(CfgExpr::Value),
Some(Ok(..)) => self.cfg().map(|v| match v {
Cfg::Name(n) if n == "true" => CfgExpr::True,
Cfg::Name(n) if n == "false" => CfgExpr::False,
v => CfgExpr::Value(v),
}),
Some(Err(..)) => Err(self.t.next().unwrap().err().unwrap()),
None => Err(ParseError::new(
self.t.orig,
Expand Down
31 changes: 10 additions & 21 deletions crates/cargo-platform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl Platform {
))
},
}
CfgExpr::True | CfgExpr::False => {},
}
}

Expand All @@ -115,30 +116,18 @@ impl Platform {
check_cfg_expr(e, warnings, path);
}
}
CfgExpr::True | CfgExpr::False => {}
CfgExpr::Value(ref e) => match e {
Cfg::Name(name) | Cfg::KeyPair(name, _) => {
if !name.raw && KEYWORDS.contains(&name.as_str()) {
if name.as_str() == "true" || name.as_str() == "false" {
warnings.push(format!(
"[{}] future-incompatibility: the meaning of `cfg({e})` will change in the future\n \
| Cargo is erroneously allowing `cfg(true)` and `cfg(false)`, but both forms are interpreted as false unless manually overridden with `--cfg`.\n \
| In the future these will be built-in defines that will have the corresponding true/false value.\n \
| It is recommended to avoid using these configs until they are properly supported.\n \
| See </~https://github.com/rust-lang/rust/issues/131204> for more information.\n \
|\n \
| help: use raw-idents instead: `cfg(r#{name})`",
path.display()
));
} else {
warnings.push(format!(
"[{}] future-incompatibility: `cfg({e})` is deprecated as `{name}` is a keyword \
and not an identifier and should not have have been accepted in this position.\n \
| this was previously accepted by Cargo but is being phased out; it will become a hard error in a future release!\n \
|\n \
| help: use raw-idents instead: `cfg(r#{name})`",
path.display()
));
}
warnings.push(format!(
"[{}] future-incompatibility: `cfg({e})` is deprecated as `{name}` is a keyword \
and not an identifier and should not have have been accepted in this position.\n \
| this was previously accepted by Cargo but is being phased out; it will become a hard error in a future release!\n \
|\n \
| help: use raw-idents instead: `cfg(r#{name})`",
path.display()
));
}
}
},
Expand Down
7 changes: 7 additions & 0 deletions crates/cargo-platform/tests/test_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ macro_rules! e {
(any($($t:tt),*)) => (CfgExpr::Any(vec![$(e!($t)),*]));
(all($($t:tt),*)) => (CfgExpr::All(vec![$(e!($t)),*]));
(not($($t:tt)*)) => (CfgExpr::Not(Box::new(e!($($t)*))));
(true) => (CfgExpr::True);
(false) => (CfgExpr::False);
(($($t:tt)*)) => (e!($($t)*));
($($t:tt)*) => (CfgExpr::Value(c!($($t)*)));
}
Expand Down Expand Up @@ -122,6 +124,9 @@ fn cfg_expr() {
good(" foo=\"3\" ", e!(foo = "3"));
good("foo = \"3 e\"", e!(foo = "3 e"));

good("true", e!(true));
good("false", e!(false));

good("all()", e!(all()));
good("all(a)", e!(all(a)));
good("all(a, b)", e!(all(a, b)));
Expand Down Expand Up @@ -249,6 +254,8 @@ fn check_cfg_attributes() {
ok("windows");
ok("any(not(unix), windows)");
ok("foo");
ok("true");
ok("false");

ok("target_arch = \"abc\"");
ok("target_feature = \"abc\"");
Expand Down
187 changes: 173 additions & 14 deletions tests/testsuite/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ fn cfg_raw_idents() {
p.cargo("check")
.with_stderr_data(str![[r#"
[LOCKING] 1 package to latest compatible version
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
[CHECKING] foo v0.1.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

Expand Down Expand Up @@ -638,24 +639,182 @@ fn cfg_keywords() {

p.cargo("check")
.with_stderr_data(str![[r#"
[WARNING] [[ROOT]/foo/Cargo.toml] future-incompatibility: the meaning of `cfg(true)` will change in the future
| Cargo is erroneously allowing `cfg(true)` and `cfg(false)`, but both forms are interpreted as false unless manually overridden with `--cfg`.
| In the future these will be built-in defines that will have the corresponding true/false value.
| It is recommended to avoid using these configs until they are properly supported.
| See </~https://github.com/rust-lang/rust/issues/131204> for more information.
|
| [HELP] use raw-idents instead: `cfg(r#true)`
[WARNING] [.cargo/config.toml] future-incompatibility: the meaning of `cfg(false)` will change in the future
| Cargo is erroneously allowing `cfg(true)` and `cfg(false)`, but both forms are interpreted as false unless manually overridden with `--cfg`.
| In the future these will be built-in defines that will have the corresponding true/false value.
| It is recommended to avoid using these configs until they are properly supported.
| See </~https://github.com/rust-lang/rust/issues/131204> for more information.
|
| [HELP] use raw-idents instead: `cfg(r#false)`
[LOCKING] 1 package to latest compatible version
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
[CHECKING] foo v0.1.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();
}

#[cargo_test]
fn cfg_booleans() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "a"
version = "0.0.1"
edition = "2015"
authors = []

[target.'cfg(true)'.dependencies]
b = { path = 'b' }

[target.'cfg(false)'.dependencies]
c = { path = 'c' }
"#,
)
.file("src/lib.rs", "")
.file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
.file("b/src/lib.rs", "")
.file("c/Cargo.toml", &basic_manifest("c", "0.0.1"))
.file("c/src/lib.rs", "")
.build();

p.cargo("check")
.masquerade_as_nightly_cargo(&["cfg-boolean-literals feature"])
.with_stderr_data(str![[r#"
[LOCKING] 2 packages to latest compatible versions
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
[CHECKING] a v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();
}

#[cargo_test]
fn cfg_booleans_config() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "a"
version = "0.0.1"
edition = "2015"
"#,
)
.file("src/lib.rs", "")
.file(
".cargo/config.toml",
r#"
[target.'cfg(true)']
rustflags = []
"#,
)
.build();

p.cargo("check")
.with_stderr_data(str![[r#"
[CHECKING] a v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();
}

#[cargo_test]
fn cfg_booleans_not() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "a"
version = "0.0.1"
edition = "2015"
authors = []

[target.'cfg(not(false))'.dependencies]
b = { path = 'b' }
"#,
)
.file("src/lib.rs", "")
.file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
.file("b/src/lib.rs", "")
.build();

p.cargo("check")
.with_stderr_data(str![[r#"
[LOCKING] 1 package to latest compatible version
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
[CHECKING] a v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();
}

#[cargo_test]
fn cfg_booleans_combinators() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "a"
version = "0.0.1"
edition = "2015"
authors = []

[target.'cfg(all(any(true), not(false), true))'.dependencies]
b = { path = 'b' }
"#,
)
.file("src/lib.rs", "")
.file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
.file("b/src/lib.rs", "")
.build();

p.cargo("check")
.with_stderr_data(str![[r#"
[LOCKING] 1 package to latest compatible version
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
[CHECKING] a v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();
}

#[cargo_test]
fn cfg_booleans_rustflags_no_effect() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "a"
version = "0.0.1"
edition = "2015"
authors = []

[target.'cfg(true)'.dependencies]
b = { path = 'b' }

[target.'cfg(false)'.dependencies]
c = { path = 'c' }
"#,
)
.file("src/lib.rs", "")
.file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
.file("b/src/lib.rs", "")
.file("c/Cargo.toml", &basic_manifest("c", "0.0.1"))
.file("c/src/lib.rs", "")
.build();

p.cargo("check")
.with_stderr_data(str![[r#"
[LOCKING] 2 packages to latest compatible versions
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
[CHECKING] a v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.env("RUSTFLAGS", "--cfg false")
.run();
}
Loading