Skip to content

Commit 52d7027

Browse files
committed
Add support for boolean literals in target cfgs
1 parent 8360c91 commit 52d7027

File tree

4 files changed

+34
-104
lines changed

4 files changed

+34
-104
lines changed

Diff for: crates/cargo-platform/src/cfg.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ pub enum CfgExpr {
1111
All(Vec<CfgExpr>),
1212
Any(Vec<CfgExpr>),
1313
Value(Cfg),
14+
True,
15+
False,
1416
}
1517

1618
/// A cfg value.
@@ -147,6 +149,8 @@ impl CfgExpr {
147149
CfgExpr::All(ref e) => e.iter().all(|e| e.matches(cfg)),
148150
CfgExpr::Any(ref e) => e.iter().any(|e| e.matches(cfg)),
149151
CfgExpr::Value(ref e) => cfg.contains(e),
152+
CfgExpr::True => true,
153+
CfgExpr::False => false,
150154
}
151155
}
152156
}
@@ -174,6 +178,8 @@ impl fmt::Display for CfgExpr {
174178
CfgExpr::All(ref e) => write!(f, "all({})", CommaSep(e)),
175179
CfgExpr::Any(ref e) => write!(f, "any({})", CommaSep(e)),
176180
CfgExpr::Value(ref e) => write!(f, "{}", e),
181+
CfgExpr::True => write!(f, "true"),
182+
CfgExpr::False => write!(f, "false"),
177183
}
178184
}
179185
}
@@ -229,7 +235,11 @@ impl<'a> Parser<'a> {
229235
self.eat(&Token::RightParen)?;
230236
Ok(CfgExpr::Not(Box::new(e)))
231237
}
232-
Some(Ok(..)) => self.cfg().map(CfgExpr::Value),
238+
Some(Ok(..)) => self.cfg().map(|v| match v {
239+
Cfg::Name(n) if n == "true" => CfgExpr::True,
240+
Cfg::Name(n) if n == "false" => CfgExpr::False,
241+
v => CfgExpr::Value(v),
242+
}),
233243
Some(Err(..)) => Err(self.t.next().unwrap().err().unwrap()),
234244
None => Err(ParseError::new(
235245
self.t.orig,

Diff for: crates/cargo-platform/src/lib.rs

+10-21
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ impl Platform {
9898
))
9999
},
100100
}
101+
CfgExpr::True | CfgExpr::False => {},
101102
}
102103
}
103104

@@ -115,30 +116,18 @@ impl Platform {
115116
check_cfg_expr(e, warnings, path);
116117
}
117118
}
119+
CfgExpr::True | CfgExpr::False => {}
118120
CfgExpr::Value(ref e) => match e {
119121
Cfg::Name(name) | Cfg::KeyPair(name, _) => {
120122
if !name.raw && KEYWORDS.contains(&name.as_str()) {
121-
if name.as_str() == "true" || name.as_str() == "false" {
122-
warnings.push(format!(
123-
"[{}] future-incompatibility: the meaning of `cfg({e})` will change in the future\n \
124-
| Cargo is erroneously allowing `cfg(true)` and `cfg(false)`, but both forms are interpreted as false unless manually overridden with `--cfg`.\n \
125-
| In the future these will be built-in defines that will have the corresponding true/false value.\n \
126-
| It is recommended to avoid using these configs until they are properly supported.\n \
127-
| See <https://github.com/rust-lang/rust/issues/131204> for more information.\n \
128-
|\n \
129-
| help: use raw-idents instead: `cfg(r#{name})`",
130-
path.display()
131-
));
132-
} else {
133-
warnings.push(format!(
134-
"[{}] future-incompatibility: `cfg({e})` is deprecated as `{name}` is a keyword \
135-
and not an identifier and should not have have been accepted in this position.\n \
136-
| this was previously accepted by Cargo but is being phased out; it will become a hard error in a future release!\n \
137-
|\n \
138-
| help: use raw-idents instead: `cfg(r#{name})`",
139-
path.display()
140-
));
141-
}
123+
warnings.push(format!(
124+
"[{}] future-incompatibility: `cfg({e})` is deprecated as `{name}` is a keyword \
125+
and not an identifier and should not have have been accepted in this position.\n \
126+
| this was previously accepted by Cargo but is being phased out; it will become a hard error in a future release!\n \
127+
|\n \
128+
| help: use raw-idents instead: `cfg(r#{name})`",
129+
path.display()
130+
));
142131
}
143132
}
144133
},

Diff for: crates/cargo-platform/tests/test_cfg.rs

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ macro_rules! e {
3939
(any($($t:tt),*)) => (CfgExpr::Any(vec![$(e!($t)),*]));
4040
(all($($t:tt),*)) => (CfgExpr::All(vec![$(e!($t)),*]));
4141
(not($($t:tt)*)) => (CfgExpr::Not(Box::new(e!($($t)*))));
42+
(true) => (CfgExpr::True);
43+
(false) => (CfgExpr::False);
4244
(($($t:tt)*)) => (e!($($t)*));
4345
($($t:tt)*) => (CfgExpr::Value(c!($($t)*)));
4446
}
@@ -122,6 +124,9 @@ fn cfg_expr() {
122124
good(" foo=\"3\" ", e!(foo = "3"));
123125
good("foo = \"3 e\"", e!(foo = "3 e"));
124126

127+
good("true", e!(true));
128+
good("false", e!(false));
129+
125130
good("all()", e!(all()));
126131
good("all(a)", e!(all(a)));
127132
good("all(a, b)", e!(all(a, b)));
@@ -249,6 +254,8 @@ fn check_cfg_attributes() {
249254
ok("windows");
250255
ok("any(not(unix), windows)");
251256
ok("foo");
257+
ok("true");
258+
ok("false");
252259

253260
ok("target_arch = \"abc\"");
254261
ok("target_feature = \"abc\"");

Diff for: tests/testsuite/cfg.rs

+6-82
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ fn cfg_raw_idents() {
545545
p.cargo("check")
546546
.with_stderr_data(str![[r#"
547547
[LOCKING] 1 package to latest compatible version
548+
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
548549
[CHECKING] foo v0.1.0 ([ROOT]/foo)
549550
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
550551
@@ -638,21 +639,8 @@ fn cfg_keywords() {
638639

639640
p.cargo("check")
640641
.with_stderr_data(str![[r#"
641-
[WARNING] [[ROOT]/foo/Cargo.toml] future-incompatibility: the meaning of `cfg(true)` will change in the future
642-
| Cargo is erroneously allowing `cfg(true)` and `cfg(false)`, but both forms are interpreted as false unless manually overridden with `--cfg`.
643-
| In the future these will be built-in defines that will have the corresponding true/false value.
644-
| It is recommended to avoid using these configs until they are properly supported.
645-
| See <https://github.com/rust-lang/rust/issues/131204> for more information.
646-
|
647-
| [HELP] use raw-idents instead: `cfg(r#true)`
648-
[WARNING] [.cargo/config.toml] future-incompatibility: the meaning of `cfg(false)` will change in the future
649-
| Cargo is erroneously allowing `cfg(true)` and `cfg(false)`, but both forms are interpreted as false unless manually overridden with `--cfg`.
650-
| In the future these will be built-in defines that will have the corresponding true/false value.
651-
| It is recommended to avoid using these configs until they are properly supported.
652-
| See <https://github.com/rust-lang/rust/issues/131204> for more information.
653-
|
654-
| [HELP] use raw-idents instead: `cfg(r#false)`
655642
[LOCKING] 1 package to latest compatible version
643+
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
656644
[CHECKING] foo v0.1.0 ([ROOT]/foo)
657645
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
658646
@@ -687,23 +675,10 @@ fn cfg_booleans() {
687675
.build();
688676

689677
p.cargo("check")
690-
// FIXME: `b` should be compiled
678+
.masquerade_as_nightly_cargo(&["cfg-boolean-literals feature"])
691679
.with_stderr_data(str![[r#"
692-
[WARNING] [[ROOT]/foo/Cargo.toml] future-incompatibility: the meaning of `cfg(false)` will change in the future
693-
| Cargo is erroneously allowing `cfg(true)` and `cfg(false)`, but both forms are interpreted as false unless manually overridden with `--cfg`.
694-
| In the future these will be built-in defines that will have the corresponding true/false value.
695-
| It is recommended to avoid using these configs until they are properly supported.
696-
| See <https://github.com/rust-lang/rust/issues/131204> for more information.
697-
|
698-
| [HELP] use raw-idents instead: `cfg(r#false)`
699-
[WARNING] [[ROOT]/foo/Cargo.toml] future-incompatibility: the meaning of `cfg(true)` will change in the future
700-
| Cargo is erroneously allowing `cfg(true)` and `cfg(false)`, but both forms are interpreted as false unless manually overridden with `--cfg`.
701-
| In the future these will be built-in defines that will have the corresponding true/false value.
702-
| It is recommended to avoid using these configs until they are properly supported.
703-
| See <https://github.com/rust-lang/rust/issues/131204> for more information.
704-
|
705-
| [HELP] use raw-idents instead: `cfg(r#true)`
706680
[LOCKING] 2 packages to latest compatible versions
681+
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
707682
[CHECKING] a v0.0.1 ([ROOT]/foo)
708683
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
709684
@@ -735,13 +710,6 @@ fn cfg_booleans_config() {
735710

736711
p.cargo("check")
737712
.with_stderr_data(str![[r#"
738-
[WARNING] [.cargo/config.toml] future-incompatibility: the meaning of `cfg(true)` will change in the future
739-
| Cargo is erroneously allowing `cfg(true)` and `cfg(false)`, but both forms are interpreted as false unless manually overridden with `--cfg`.
740-
| In the future these will be built-in defines that will have the corresponding true/false value.
741-
| It is recommended to avoid using these configs until they are properly supported.
742-
| See <https://github.com/rust-lang/rust/issues/131204> for more information.
743-
|
744-
| [HELP] use raw-idents instead: `cfg(r#true)`
745713
[CHECKING] a v0.0.1 ([ROOT]/foo)
746714
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
747715
@@ -772,13 +740,6 @@ fn cfg_booleans_not() {
772740

773741
p.cargo("check")
774742
.with_stderr_data(str![[r#"
775-
[WARNING] [[ROOT]/foo/Cargo.toml] future-incompatibility: the meaning of `cfg(false)` will change in the future
776-
| Cargo is erroneously allowing `cfg(true)` and `cfg(false)`, but both forms are interpreted as false unless manually overridden with `--cfg`.
777-
| In the future these will be built-in defines that will have the corresponding true/false value.
778-
| It is recommended to avoid using these configs until they are properly supported.
779-
| See <https://github.com/rust-lang/rust/issues/131204> for more information.
780-
|
781-
| [HELP] use raw-idents instead: `cfg(r#false)`
782743
[LOCKING] 1 package to latest compatible version
783744
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
784745
[CHECKING] a v0.0.1 ([ROOT]/foo)
@@ -810,30 +771,9 @@ fn cfg_booleans_combinators() {
810771
.build();
811772

812773
p.cargo("check")
813-
// FIXME: `b` should be compiled
814774
.with_stderr_data(str![[r#"
815-
[WARNING] [[ROOT]/foo/Cargo.toml] future-incompatibility: the meaning of `cfg(true)` will change in the future
816-
| Cargo is erroneously allowing `cfg(true)` and `cfg(false)`, but both forms are interpreted as false unless manually overridden with `--cfg`.
817-
| In the future these will be built-in defines that will have the corresponding true/false value.
818-
| It is recommended to avoid using these configs until they are properly supported.
819-
| See <https://github.com/rust-lang/rust/issues/131204> for more information.
820-
|
821-
| [HELP] use raw-idents instead: `cfg(r#true)`
822-
[WARNING] [[ROOT]/foo/Cargo.toml] future-incompatibility: the meaning of `cfg(false)` will change in the future
823-
| Cargo is erroneously allowing `cfg(true)` and `cfg(false)`, but both forms are interpreted as false unless manually overridden with `--cfg`.
824-
| In the future these will be built-in defines that will have the corresponding true/false value.
825-
| It is recommended to avoid using these configs until they are properly supported.
826-
| See <https://github.com/rust-lang/rust/issues/131204> for more information.
827-
|
828-
| [HELP] use raw-idents instead: `cfg(r#false)`
829-
[WARNING] [[ROOT]/foo/Cargo.toml] future-incompatibility: the meaning of `cfg(true)` will change in the future
830-
| Cargo is erroneously allowing `cfg(true)` and `cfg(false)`, but both forms are interpreted as false unless manually overridden with `--cfg`.
831-
| In the future these will be built-in defines that will have the corresponding true/false value.
832-
| It is recommended to avoid using these configs until they are properly supported.
833-
| See <https://github.com/rust-lang/rust/issues/131204> for more information.
834-
|
835-
| [HELP] use raw-idents instead: `cfg(r#true)`
836775
[LOCKING] 1 package to latest compatible version
776+
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
837777
[CHECKING] a v0.0.1 ([ROOT]/foo)
838778
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
839779
@@ -868,29 +808,13 @@ fn cfg_booleans_rustflags_no_effect() {
868808
.build();
869809

870810
p.cargo("check")
871-
// FIXME: only `b` should be compiled, the rustflags don't take effect
872811
.with_stderr_data(str![[r#"
873-
[WARNING] [[ROOT]/foo/Cargo.toml] future-incompatibility: the meaning of `cfg(false)` will change in the future
874-
| Cargo is erroneously allowing `cfg(true)` and `cfg(false)`, but both forms are interpreted as false unless manually overridden with `--cfg`.
875-
| In the future these will be built-in defines that will have the corresponding true/false value.
876-
| It is recommended to avoid using these configs until they are properly supported.
877-
| See <https://github.com/rust-lang/rust/issues/131204> for more information.
878-
|
879-
| [HELP] use raw-idents instead: `cfg(r#false)`
880-
[WARNING] [[ROOT]/foo/Cargo.toml] future-incompatibility: the meaning of `cfg(true)` will change in the future
881-
| Cargo is erroneously allowing `cfg(true)` and `cfg(false)`, but both forms are interpreted as false unless manually overridden with `--cfg`.
882-
| In the future these will be built-in defines that will have the corresponding true/false value.
883-
| It is recommended to avoid using these configs until they are properly supported.
884-
| See <https://github.com/rust-lang/rust/issues/131204> for more information.
885-
|
886-
| [HELP] use raw-idents instead: `cfg(r#true)`
887812
[LOCKING] 2 packages to latest compatible versions
888813
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
889-
[CHECKING] c v0.0.1 ([ROOT]/foo/c)
890814
[CHECKING] a v0.0.1 ([ROOT]/foo)
891815
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
892816
893817
"#]])
894-
.env("RUSTFLAGS", "--cfg true --cfg false")
818+
.env("RUSTFLAGS", "--cfg false")
895819
.run();
896820
}

0 commit comments

Comments
 (0)