diff --git a/src/cargo/util/toml/targets.rs b/src/cargo/util/toml/targets.rs index 93b1abbb8..fbba46453 100644 --- a/src/cargo/util/toml/targets.rs +++ b/src/cargo/util/toml/targets.rs @@ -44,6 +44,7 @@ pub fn targets( package_root, package_name, warnings, + errors, has_lib, )?); @@ -164,6 +165,7 @@ fn clean_bins( package_root: &Path, package_name: &str, warnings: &mut Vec, + errors: &mut Vec, has_lib: bool, ) -> CargoResult> { let inferred = inferred_bins(package_root, package_name); @@ -183,6 +185,26 @@ fn clean_bins( validate_has_name(bin, "binary", "bin")?; let name = bin.name(); + + if let Some(crate_types) = bin.crate_types() { + if !crate_types.is_empty() { + errors.push(format!( + "the target `{}` is a binary and can't have any \ + crate-types set (currently \"{}\")", + name, + crate_types.join(", ") + )); + } + } + + if bin.proc_macro() == Some(true) { + errors.push(format!( + "the target `{}` is a binary and can't have `proc-macro` \ + set `true`", + name + )); + } + if is_bad_artifact_name(&name) { bail!("the binary target name `{}` is forbidden", name) } diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index c00909890..d498d8510 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -449,6 +449,71 @@ Caused by: ) } +#[test] +fn cargo_compile_with_bin_and_crate_type() { + let p = project("foo") + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + authors = [] + version = "0.0.0" + + [[bin]] + name = "the_foo_bin" + path = "src/foo.rs" + crate-type = ["cdylib", "rlib"] + "#, + ) + .file("src/foo.rs", "fn main() {}") + .build(); + + assert_that( + p.cargo("build"), + execs().with_status(101).with_stderr( + "\ +[ERROR] failed to parse manifest at `[..]` + +Caused by: + the target `the_foo_bin` is a binary and can't have any crate-types set \ +(currently \"cdylib, rlib\")", + ), + ) +} + +#[test] +fn cargo_compile_with_bin_and_proc() { + let p = project("foo") + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + authors = [] + version = "0.0.0" + + [[bin]] + name = "the_foo_bin" + path = "src/foo.rs" + proc-macro = true + "#, + ) + .file("src/foo.rs", "fn main() {}") + .build(); + + assert_that( + p.cargo("build"), + execs().with_status(101).with_stderr( + "\ +[ERROR] failed to parse manifest at `[..]` + +Caused by: + the target `the_foo_bin` is a binary and can't have `proc-macro` set `true`", + ), + ) +} + #[test] fn cargo_compile_with_invalid_lib_target_name() { let p = project("foo")