Skip to content

Commit

Permalink
Add support for boolean literals in target cfgs
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Dec 22, 2024
1 parent 22a0cdf commit b33f175
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 104 deletions.
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
88 changes: 6 additions & 82 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,21 +639,8 @@ 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
Expand Down Expand Up @@ -687,23 +675,10 @@ fn cfg_booleans() {
.build();

p.cargo("check")
// FIXME: `b` should be compiled
.masquerade_as_nightly_cargo(&["cfg-boolean-literals feature"])
.with_stderr_data(str![[r#"
[WARNING] [[ROOT]/foo/Cargo.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)`
[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)`
[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
Expand Down Expand Up @@ -735,13 +710,6 @@ fn cfg_booleans_config() {

p.cargo("check")
.with_stderr_data(str![[r#"
[WARNING] [.cargo/config.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)`
[CHECKING] a v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
Expand Down Expand Up @@ -772,13 +740,6 @@ fn cfg_booleans_not() {

p.cargo("check")
.with_stderr_data(str![[r#"
[WARNING] [[ROOT]/foo/Cargo.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] a v0.0.1 ([ROOT]/foo)
Expand Down Expand Up @@ -810,30 +771,9 @@ fn cfg_booleans_combinators() {
.build();

p.cargo("check")
// FIXME: `b` should be compiled
.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] [[ROOT]/foo/Cargo.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)`
[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)`
[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
Expand Down Expand Up @@ -868,29 +808,13 @@ fn cfg_booleans_rustflags_no_effect() {
.build();

p.cargo("check")
// FIXME: only `b` should be compiled, the rustflags don't take effect
.with_stderr_data(str![[r#"
[WARNING] [[ROOT]/foo/Cargo.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)`
[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)`
[LOCKING] 2 packages to latest compatible versions
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
[CHECKING] c v0.0.1 ([ROOT]/foo/c)
[CHECKING] a v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.env("RUSTFLAGS", "--cfg true --cfg false")
.env("RUSTFLAGS", "--cfg false")
.run();
}

0 comments on commit b33f175

Please sign in to comment.