Skip to content

Commit 9ec86a4

Browse files
committed
Ignore version = … and raise a warning instead of an error
1 parent d1ee399 commit 9ec86a4

File tree

4 files changed

+50
-32
lines changed

4 files changed

+50
-32
lines changed

crates/cargo-platform/src/cfg.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -281,26 +281,6 @@ impl<'a> Parser<'a> {
281281

282282
fn cfg(&mut self) -> Result<Cfg, ParseError> {
283283
match self.t.next() {
284-
Some(Ok(Token::Ident(false, "version"))) => {
285-
self.eat(&Token::LeftParen)?;
286-
let token = self
287-
.t
288-
.next()
289-
.ok_or_else(|| ParseError::new(self.t.orig, InvalidVersion))??;
290-
let Token::String(version_str) = token else {
291-
return Err(ParseError::new(
292-
self.t.orig,
293-
UnexpectedToken {
294-
expected: "a string",
295-
found: token.classify(),
296-
},
297-
));
298-
};
299-
self.eat(&Token::RightParen)?;
300-
let version = CfgRustVersion::parse(version_str)
301-
.ok_or_else(|| ParseError::new(self.t.orig, InvalidVersion))?;
302-
Ok(Cfg::Version(version))
303-
}
304284
Some(Ok(Token::Ident(raw, name))) => {
305285
let e = if self.r#try(&Token::Equals) {
306286
let val = match self.t.next() {
@@ -326,6 +306,25 @@ impl<'a> Parser<'a> {
326306
},
327307
val.to_string(),
328308
)
309+
} else if name == "version" {
310+
self.eat(&Token::LeftParen)?;
311+
let token = self
312+
.t
313+
.next()
314+
.ok_or_else(|| ParseError::new(self.t.orig, InvalidVersion))??;
315+
let Token::String(version_str) = token else {
316+
return Err(ParseError::new(
317+
self.t.orig,
318+
UnexpectedToken {
319+
expected: "a string",
320+
found: token.classify(),
321+
},
322+
));
323+
};
324+
self.eat(&Token::RightParen)?;
325+
let version = CfgRustVersion::parse(version_str)
326+
.ok_or_else(|| ParseError::new(self.t.orig, InvalidVersion))?;
327+
Cfg::Version(version)
329328
} else {
330329
Cfg::Name(Ident {
331330
name: name.to_string(),

crates/cargo-platform/src/lib.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,24 @@ impl Platform {
8888
)),
8989
_ => (),
9090
},
91-
Cfg::KeyPair(name, _) => if name.as_str() == "feature" {
92-
warnings.push(String::from(
93-
"Found `feature = ...` in `target.'cfg(...)'.dependencies`. \
94-
This key is not supported for selecting dependencies \
95-
and will not work as expected. \
96-
Use the [features] section instead: \
97-
https://doc.rust-lang.org/cargo/reference/features.html"
98-
))
91+
Cfg::KeyPair(name, _) => match name.as_str() {
92+
"feature" => {
93+
warnings.push(String::from(
94+
"Found `feature = ...` in `target.'cfg(...)'.dependencies`. \
95+
This key is not supported for selecting dependencies \
96+
and will not work as expected. \
97+
Use the [features] section instead: \
98+
https://doc.rust-lang.org/cargo/reference/features.html"
99+
));
100+
},
101+
"version" => {
102+
warnings.push(String::from(
103+
"Found `version = ...` in `target.'cfg(...)'.dependencies`. \
104+
This format is ignored by Cargo. \
105+
Use the format `version(\"1.23.4\")` or `version(\"1.23\")` instead."
106+
));
107+
},
108+
_ => {}
99109
},
100110
Cfg::Version(..) => {},
101111
}

crates/cargo-platform/tests/test_cfg.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ fn cfg_syntax() {
100100
good(" version(\"1.23.4\")", c!(version(23, 4)));
101101
good("version(\"1.23.4\") ", c!(version(23, 4)));
102102
good(" version(\"1.23.4\") ", c!(version(23, 4)));
103+
good("version = \"1.23.4\"", c!(version = "1.23.4"));
103104
}
104105

105106
#[test]
@@ -151,6 +152,7 @@ fn cfg_expr() {
151152
good("all(a, )", e!(all(a)));
152153
good("not(a = \"b\")", e!(not(a = "b")));
153154
good("not(all(a))", e!(not(all(a))));
155+
good("not(version(\"1.23.4\"))", e!(not(version(23, 4))));
154156
}
155157

156158
#[test]

tests/testsuite/cfg.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -944,10 +944,17 @@ fn cfg_bad_version3() {
944944
p.cargo("check")
945945
.with_status(101)
946946
.with_stderr_data(str![[r#"
947-
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
948-
949-
Caused by:
950-
failed to parse `version = "1.87.0"` as a cfg expression: expected `(`, found `=`
947+
[WARNING] Found `version = ...` in `target.'cfg(...)'.dependencies`. This format is ignored by Cargo. Use the format `version("1.23.4")` or `version("1.23")` instead.
948+
[LOCKING] 1 package to latest compatible version
949+
[CHECKING] foo v0.0.0 ([ROOT]/foo)
950+
error[E0463]: can't find crate for `b`
951+
--> src/lib.rs:1:1
952+
|
953+
1 | extern crate b;
954+
| ^^^^^^^^^^^^^^^ can't find crate
955+
956+
For more information about this error, try `rustc --explain E0463`.
957+
[ERROR] could not compile `foo` (lib) due to 1 previous error
951958
952959
"#]])
953960
.run();

0 commit comments

Comments
 (0)