Skip to content

Commit d849068

Browse files
mo8itepage
andcommitted
feat(platform)!: Require the rust version to do matching
Co-authored-by: Ed Page <[email protected]>
1 parent 013e9d2 commit d849068

File tree

6 files changed

+42
-35
lines changed

6 files changed

+42
-35
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cargo-platform/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ documentation = "https://docs.rs/cargo-platform"
1010
description = "Cargo's representation of a target platform."
1111

1212
[dependencies]
13+
semver.workspace = true
1314
serde.workspace = true
1415

1516
[dev-dependencies]

crates/cargo-platform/examples/matches.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ fn main() {
2626
examples.push(target.as_str());
2727
for example in examples {
2828
let p = Platform::from_str(example).unwrap();
29-
println!("{:?} matches: {:?}", example, p.matches(&target, &cfgs));
29+
println!(
30+
"{:?} matches: {:?}",
31+
example,
32+
p.matches(&target, &cfgs, &semver::Version::new(0, 0, 0))
33+
);
3034
}
3135
}
3236

crates/cargo-platform/src/cfg.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,24 +130,24 @@ impl fmt::Display for Cfg {
130130

131131
impl CfgExpr {
132132
/// Utility function to check if the key, "cfg(..)" matches the `target_cfg`
133-
pub fn matches_key(key: &str, target_cfg: &[Cfg]) -> bool {
133+
pub fn matches_key(key: &str, target_cfg: &[Cfg], rustc_version: &semver::Version) -> bool {
134134
if key.starts_with("cfg(") && key.ends_with(')') {
135135
let cfg = &key[4..key.len() - 1];
136136

137137
CfgExpr::from_str(cfg)
138138
.ok()
139-
.map(|ce| ce.matches(target_cfg))
139+
.map(|ce| ce.matches(target_cfg, rustc_version))
140140
.unwrap_or(false)
141141
} else {
142142
false
143143
}
144144
}
145145

146-
pub fn matches(&self, cfg: &[Cfg]) -> bool {
146+
pub fn matches(&self, cfg: &[Cfg], rustc_version: &semver::Version) -> bool {
147147
match *self {
148-
CfgExpr::Not(ref e) => !e.matches(cfg),
149-
CfgExpr::All(ref e) => e.iter().all(|e| e.matches(cfg)),
150-
CfgExpr::Any(ref e) => e.iter().any(|e| e.matches(cfg)),
148+
CfgExpr::Not(ref e) => !e.matches(cfg, rustc_version),
149+
CfgExpr::All(ref e) => e.iter().all(|e| e.matches(cfg, rustc_version)),
150+
CfgExpr::Any(ref e) => e.iter().any(|e| e.matches(cfg, rustc_version)),
151151
CfgExpr::Value(ref e) => cfg.contains(e),
152152
CfgExpr::True => true,
153153
CfgExpr::False => false,

crates/cargo-platform/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ impl Platform {
3434
/// Returns whether the Platform matches the given target and cfg.
3535
///
3636
/// The named target and cfg values should be obtained from `rustc`.
37-
pub fn matches(&self, name: &str, cfg: &[Cfg]) -> bool {
37+
pub fn matches(&self, name: &str, cfg: &[Cfg], rustc_version: &semver::Version) -> bool {
3838
match *self {
3939
Platform::Name(ref p) => p == name,
40-
Platform::Cfg(ref p) => p.matches(cfg),
40+
Platform::Cfg(ref p) => p.matches(cfg, rustc_version),
4141
}
4242
}
4343

crates/cargo-platform/tests/test_cfg.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -251,33 +251,34 @@ fn cfg_expr_bad() {
251251

252252
#[test]
253253
fn cfg_matches() {
254-
assert!(e!(foo).matches(&[c!(bar), c!(foo), c!(baz)]));
255-
assert!(e!(any(foo)).matches(&[c!(bar), c!(foo), c!(baz)]));
256-
assert!(e!(any(foo, bar)).matches(&[c!(bar)]));
257-
assert!(e!(any(foo, bar)).matches(&[c!(foo)]));
258-
assert!(e!(all(foo, bar)).matches(&[c!(foo), c!(bar)]));
259-
assert!(e!(all(foo, bar)).matches(&[c!(foo), c!(bar)]));
260-
assert!(e!(not(foo)).matches(&[c!(bar)]));
261-
assert!(e!(not(foo)).matches(&[]));
262-
assert!(e!(any((not(foo)), (all(foo, bar)))).matches(&[c!(bar)]));
263-
assert!(e!(any((not(foo)), (all(foo, bar)))).matches(&[c!(foo), c!(bar)]));
264-
assert!(e!(foo).matches(&[c!(r # foo)]));
265-
assert!(e!(r # foo).matches(&[c!(foo)]));
266-
assert!(e!(r # foo).matches(&[c!(r # foo)]));
254+
let v87 = semver::Version::new(1, 87, 0);
255+
assert!(e!(foo).matches(&[c!(bar), c!(foo), c!(baz)], &v87));
256+
assert!(e!(any(foo)).matches(&[c!(bar), c!(foo), c!(baz)], &v87));
257+
assert!(e!(any(foo, bar)).matches(&[c!(bar)], &v87));
258+
assert!(e!(any(foo, bar)).matches(&[c!(foo)], &v87));
259+
assert!(e!(all(foo, bar)).matches(&[c!(foo), c!(bar)], &v87));
260+
assert!(e!(all(foo, bar)).matches(&[c!(foo), c!(bar)], &v87));
261+
assert!(e!(not(foo)).matches(&[c!(bar)], &v87));
262+
assert!(e!(not(foo)).matches(&[], &v87));
263+
assert!(e!(any((not(foo)), (all(foo, bar)))).matches(&[c!(bar)], &v87));
264+
assert!(e!(any((not(foo)), (all(foo, bar)))).matches(&[c!(foo), c!(bar)], &v87));
265+
assert!(e!(foo).matches(&[c!(r # foo)], &v87));
266+
assert!(e!(r # foo).matches(&[c!(foo)], &v87));
267+
assert!(e!(r # foo).matches(&[c!(r # foo)], &v87));
267268

268-
assert!(!e!(foo).matches(&[]));
269-
assert!(!e!(foo).matches(&[c!(bar)]));
270-
assert!(!e!(foo).matches(&[c!(fo)]));
271-
assert!(!e!(any(foo)).matches(&[]));
272-
assert!(!e!(any(foo)).matches(&[c!(bar)]));
273-
assert!(!e!(any(foo)).matches(&[c!(bar), c!(baz)]));
274-
assert!(!e!(all(foo)).matches(&[c!(bar), c!(baz)]));
275-
assert!(!e!(all(foo, bar)).matches(&[c!(bar)]));
276-
assert!(!e!(all(foo, bar)).matches(&[c!(foo)]));
277-
assert!(!e!(all(foo, bar)).matches(&[]));
278-
assert!(!e!(not(bar)).matches(&[c!(bar)]));
279-
assert!(!e!(not(bar)).matches(&[c!(baz), c!(bar)]));
280-
assert!(!e!(any((not(foo)), (all(foo, bar)))).matches(&[c!(foo)]));
269+
assert!(!e!(foo).matches(&[], &v87));
270+
assert!(!e!(foo).matches(&[c!(bar)], &v87));
271+
assert!(!e!(foo).matches(&[c!(fo)], &v87));
272+
assert!(!e!(any(foo)).matches(&[], &v87));
273+
assert!(!e!(any(foo)).matches(&[c!(bar)], &v87));
274+
assert!(!e!(any(foo)).matches(&[c!(bar), c!(baz)], &v87));
275+
assert!(!e!(all(foo)).matches(&[c!(bar), c!(baz)], &v87));
276+
assert!(!e!(all(foo, bar)).matches(&[c!(bar)], &v87));
277+
assert!(!e!(all(foo, bar)).matches(&[c!(foo)], &v87));
278+
assert!(!e!(all(foo, bar)).matches(&[], &v87));
279+
assert!(!e!(not(bar)).matches(&[c!(bar)], &v87));
280+
assert!(!e!(not(bar)).matches(&[c!(baz), c!(bar)], &v87));
281+
assert!(!e!(any((not(foo)), (all(foo, bar)))).matches(&[c!(foo)], &v87));
281282
}
282283

283284
#[test]

0 commit comments

Comments
 (0)