Skip to content

Commit 774d949

Browse files
committed
Auto merge of #7660 - benediktwerner:master, r=alexcrichton
Emit error on [target.'cfg(debug_assertions)'.dependencies] and similar Closes #7634 Cargo now emits an error if an unsupported cfg key or name is used for specifying a target. This PR also updates the docs to clarify this behavior.
2 parents 4a9b6be + aa93f61 commit 774d949

File tree

4 files changed

+125
-1
lines changed

4 files changed

+125
-1
lines changed

crates/cargo-platform/src/lib.rs

+42
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,48 @@ impl Platform {
6161
}
6262
Ok(())
6363
}
64+
65+
pub fn check_cfg_attributes(&self, warnings: &mut Vec<String>) {
66+
fn check_cfg_expr(expr: &CfgExpr, warnings: &mut Vec<String>) {
67+
match *expr {
68+
CfgExpr::Not(ref e) => check_cfg_expr(e, warnings),
69+
CfgExpr::All(ref e) | CfgExpr::Any(ref e) => {
70+
for e in e {
71+
check_cfg_expr(e, warnings);
72+
}
73+
}
74+
CfgExpr::Value(ref e) => match e {
75+
Cfg::Name(name) => match name.as_str() {
76+
"test" | "debug_assertions" | "proc_macro" =>
77+
warnings.push(format!(
78+
"Found `{}` in `target.'cfg(...)'.dependencies`. \
79+
This value is not supported for selecting dependencies \
80+
and will not work as expected. \
81+
To learn more visit \
82+
https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#platform-specific-dependencies",
83+
name
84+
)),
85+
_ => (),
86+
},
87+
Cfg::KeyPair(name, _) => match name.as_str() {
88+
"feature" =>
89+
warnings.push(String::from(
90+
"Found `feature = ...` in `target.'cfg(...)'.dependencies`. \
91+
This key is not supported for selecting dependencies \
92+
and will not work as expected. \
93+
Use the [features] section instead: \
94+
https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section"
95+
)),
96+
_ => (),
97+
},
98+
}
99+
}
100+
}
101+
102+
if let Platform::Cfg(cfg) = self {
103+
check_cfg_expr(cfg, warnings);
104+
}
105+
}
64106
}
65107

66108
impl serde::Serialize for Platform {

crates/cargo-platform/tests/test_cfg.rs

+73
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,76 @@ fn round_trip_platform() {
176176
all(target_os = \"freebsd\", target_arch = \"x86_64\")))",
177177
);
178178
}
179+
180+
#[test]
181+
fn check_cfg_attributes() {
182+
fn ok(s: &str) {
183+
let p = Platform::Cfg(s.parse().unwrap());
184+
let mut warnings = Vec::new();
185+
p.check_cfg_attributes(&mut warnings);
186+
assert!(
187+
warnings.is_empty(),
188+
"Expected no warnings but got: {:?}",
189+
warnings,
190+
);
191+
}
192+
193+
fn warn(s: &str, names: &[&str]) {
194+
let p = Platform::Cfg(s.parse().unwrap());
195+
let mut warnings = Vec::new();
196+
p.check_cfg_attributes(&mut warnings);
197+
assert_eq!(
198+
warnings.len(),
199+
names.len(),
200+
"Expecter warnings about {:?} but got {:?}",
201+
names,
202+
warnings,
203+
);
204+
for (name, warning) in names.iter().zip(warnings.iter()) {
205+
assert!(
206+
warning.contains(name),
207+
"Expected warning about '{}' but got: {}",
208+
name,
209+
warning,
210+
);
211+
}
212+
}
213+
214+
ok("unix");
215+
ok("windows");
216+
ok("any(not(unix), windows)");
217+
ok("foo");
218+
219+
ok("target_arch = \"abc\"");
220+
ok("target_feature = \"abc\"");
221+
ok("target_os = \"abc\"");
222+
ok("target_family = \"abc\"");
223+
ok("target_env = \"abc\"");
224+
ok("target_endian = \"abc\"");
225+
ok("target_pointer_width = \"abc\"");
226+
ok("target_vendor = \"abc\"");
227+
ok("bar = \"def\"");
228+
229+
warn("test", &["test"]);
230+
warn("debug_assertions", &["debug_assertions"]);
231+
warn("proc_macro", &["proc_macro"]);
232+
warn("feature = \"abc\"", &["feature"]);
233+
234+
warn("any(not(debug_assertions), windows)", &["debug_assertions"]);
235+
warn(
236+
"any(not(feature = \"def\"), target_arch = \"abc\")",
237+
&["feature"],
238+
);
239+
warn(
240+
"any(not(target_os = \"windows\"), proc_macro)",
241+
&["proc_macro"],
242+
);
243+
warn(
244+
"any(not(feature = \"windows\"), proc_macro)",
245+
&["feature", "proc_macro"],
246+
);
247+
warn(
248+
"all(not(debug_assertions), any(windows, proc_macro))",
249+
&["debug_assertions", "proc_macro"],
250+
);
251+
}

src/cargo/util/toml/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,11 @@ impl TomlManifest {
10851085
process_dependencies(&mut cx, build_deps, Some(Kind::Build))?;
10861086

10871087
for (name, platform) in me.target.iter().flatten() {
1088-
cx.platform = Some(name.parse()?);
1088+
cx.platform = {
1089+
let platform: Platform = name.parse()?;
1090+
platform.check_cfg_attributes(&mut cx.warnings);
1091+
Some(platform)
1092+
};
10891093
process_dependencies(&mut cx, platform.dependencies.as_ref(), None)?;
10901094
let build_deps = platform
10911095
.build_dependencies

src/doc/src/reference/specifying-dependencies.md

+5
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,11 @@ dependencies based on optional crate features.
476476
Use [the `[features]` section](manifest.md#the-features-section)
477477
instead.
478478

479+
The same applies to `cfg(debug_assertions)`, `cfg(test)` and `cfg(prog_macro)`.
480+
These values will not work as expected and will always have the default value
481+
returned by `rustc --print=cfg`.
482+
There is currently no way to add dependencies based on these configuration values.
483+
479484
In addition to `#[cfg]` syntax, Cargo also supports listing out the full target
480485
the dependencies would apply to:
481486

0 commit comments

Comments
 (0)