Skip to content

Commit 7e00b82

Browse files
committed
Auto merge of #4288 - matklad:bench-bench, r=alexcrichton
Allow `src/bench.rs` as a benchmark for legacy reasons closes #4287 cc #3947
2 parents efe6b11 + 4eb28fc commit 7e00b82

File tree

2 files changed

+91
-25
lines changed

2 files changed

+91
-25
lines changed

src/cargo/util/toml/targets.rs

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn targets(manifest: &TomlManifest,
5151
);
5252

5353
targets.extend(
54-
clean_benches(manifest.bench.as_ref(), package_root)?
54+
clean_benches(manifest.bench.as_ref(), package_root, warnings)?
5555
);
5656

5757
// processing the custom build script
@@ -175,26 +175,23 @@ fn clean_bins(toml_bins: Option<&Vec<TomlBinTarget>>,
175175

176176
let mut result = Vec::new();
177177
for bin in bins.iter() {
178-
let path = match target_path(bin, &inferred, "bin", package_root) {
179-
Ok(path) => path,
180-
Err(e) => {
181-
if let Some(legacy_path) = legacy_bin_path(package_root, &bin.name(), has_lib) {
182-
{
183-
let short_path = without_prefix(&legacy_path, package_root)
184-
.unwrap_or(&legacy_path);
185-
186-
warnings.push(format!(
187-
"path `{}` was erroneously implicitly accepted for binary {},\n\
188-
please set bin.path in Cargo.toml",
189-
short_path.display(), bin.name()
190-
));
191-
}
192-
legacy_path
193-
} else {
194-
return Err(e);
178+
let path = target_path(bin, &inferred, "bin", package_root, &mut |_| {
179+
if let Some(legacy_path) = legacy_bin_path(package_root, &bin.name(), has_lib) {
180+
{
181+
let short_path = without_prefix(&legacy_path, package_root)
182+
.unwrap_or(&legacy_path);
183+
184+
warnings.push(format!(
185+
"path `{}` was erroneously implicitly accepted for binary {},\n\
186+
please set bin.path in Cargo.toml",
187+
short_path.display(), bin.name()
188+
));
195189
}
190+
Some(legacy_path)
191+
} else {
192+
None
196193
}
197-
};
194+
})?;
198195

199196
let mut target = Target::bin_target(&bin.name(), path,
200197
bin.required_features.clone());
@@ -263,10 +260,30 @@ fn clean_tests(toml_tests: Option<&Vec<TomlTestTarget>>,
263260
}
264261

265262
fn clean_benches(toml_benches: Option<&Vec<TomlBenchTarget>>,
266-
package_root: &Path) -> CargoResult<Vec<Target>> {
267-
let targets = clean_targets("benchmark", "bench",
268-
toml_benches, inferred_benches(package_root),
269-
package_root)?;
263+
package_root: &Path,
264+
warnings: &mut Vec<String>) -> CargoResult<Vec<Target>> {
265+
let mut legacy_bench_path = |bench: &TomlTarget| {
266+
let legacy_path = package_root.join("src").join("bench.rs");
267+
if !(bench.name() == "bench" && legacy_path.exists()) {
268+
return None;
269+
}
270+
{
271+
let short_path = without_prefix(&legacy_path, package_root)
272+
.unwrap_or(&legacy_path);
273+
274+
warnings.push(format!(
275+
"path `{}` was erroneously implicitly accepted for benchmark {},\n\
276+
please set bench.path in Cargo.toml",
277+
short_path.display(), bench.name()
278+
));
279+
}
280+
Some(legacy_path)
281+
};
282+
283+
let targets = clean_targets_with_legacy_path("benchmark", "bench",
284+
toml_benches, inferred_benches(package_root),
285+
package_root,
286+
&mut legacy_bench_path)?;
270287

271288
let mut result = Vec::new();
272289
for (path, toml) in targets {
@@ -284,6 +301,19 @@ fn clean_targets(target_kind_human: &str, target_kind: &str,
284301
inferred: Vec<(String, PathBuf)>,
285302
package_root: &Path)
286303
-> CargoResult<Vec<(PathBuf, TomlTarget)>> {
304+
clean_targets_with_legacy_path(target_kind_human, target_kind,
305+
toml_targets,
306+
inferred,
307+
package_root,
308+
&mut |_| None)
309+
}
310+
311+
fn clean_targets_with_legacy_path(target_kind_human: &str, target_kind: &str,
312+
toml_targets: Option<&Vec<TomlTarget>>,
313+
inferred: Vec<(String, PathBuf)>,
314+
package_root: &Path,
315+
legacy_path: &mut FnMut(&TomlTarget) -> Option<PathBuf>)
316+
-> CargoResult<Vec<(PathBuf, TomlTarget)>> {
287317
let toml_targets = match toml_targets {
288318
Some(targets) => targets.clone(),
289319
None => inferred.iter().map(|&(ref name, ref path)| {
@@ -302,7 +332,7 @@ fn clean_targets(target_kind_human: &str, target_kind: &str,
302332
validate_unique_names(&toml_targets, target_kind)?;
303333
let mut result = Vec::new();
304334
for target in toml_targets {
305-
let path = target_path(&target, &inferred, target_kind, package_root)?;
335+
let path = target_path(&target, &inferred, target_kind, package_root, legacy_path)?;
306336
result.push((path, target));
307337
}
308338
Ok(result)
@@ -428,7 +458,8 @@ fn configure(toml: &TomlTarget, target: &mut Target) {
428458
fn target_path(target: &TomlTarget,
429459
inferred: &[(String, PathBuf)],
430460
target_kind: &str,
431-
package_root: &Path) -> CargoResult<PathBuf> {
461+
package_root: &Path,
462+
legacy_path: &mut FnMut(&TomlTarget) -> Option<PathBuf>) -> CargoResult<PathBuf> {
432463
if let Some(ref path) = target.path {
433464
// Should we verify that this path exists here?
434465
return Ok(package_root.join(&path.0));
@@ -444,6 +475,10 @@ fn target_path(target: &TomlTarget,
444475
match (first, second) {
445476
(Some(path), None) => Ok(path),
446477
(None, None) | (Some(_), Some(_)) => {
478+
if let Some(path) = legacy_path(target) {
479+
return Ok(path);
480+
}
481+
447482
bail!("can't find `{name}` {target_kind}, specify {target_kind}.path",
448483
name = name, target_kind = target_kind)
449484
}

tests/bench.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,3 +1158,34 @@ fn bench_all_virtual_manifest() {
11581158
.with_stdout_contains("test bench_foo ... bench: [..]"));
11591159
}
11601160

1161+
// https://github.com/rust-lang/cargo/issues/4287
1162+
#[test]
1163+
fn legacy_bench_name() {
1164+
if !is_nightly() { return }
1165+
1166+
let p = project("foo")
1167+
.file("Cargo.toml", r#"
1168+
[project]
1169+
name = "foo"
1170+
version = "0.1.0"
1171+
1172+
[[bench]]
1173+
name = "bench"
1174+
"#)
1175+
.file("src/lib.rs", r#"
1176+
pub fn foo() {}
1177+
"#)
1178+
.file("src/bench.rs", r#"
1179+
#![feature(test)]
1180+
extern crate test;
1181+
1182+
use test::Bencher;
1183+
1184+
#[bench]
1185+
fn bench_foo(_: &mut Bencher) -> () { () }
1186+
"#);
1187+
1188+
assert_that(p.cargo_process("bench"), execs().with_status(0).with_stderr_contains("\
1189+
[WARNING] path `src[/]bench.rs` was erroneously implicitly accepted for benchmark bench,
1190+
please set bench.path in Cargo.toml"));
1191+
}

0 commit comments

Comments
 (0)