Do not allow crate-type or proc-macro for [[bin]]-targets
authorLukas Lueg <lukas.lueg@gmail.com>
Mon, 19 Mar 2018 17:23:40 +0000 (18:23 +0100)
committerLukas Lueg <lukas.lueg@gmail.com>
Tue, 20 Mar 2018 15:51:03 +0000 (16:51 +0100)
in the current workspace.

Fixes #5199.

src/cargo/util/toml/targets.rs
tests/testsuite/build.rs

index 93b1abbb824b5c48a3a85138fa6a4c9a9712af9e..fbba46453d176fdea8e2503635bcfcaa475f164a 100644 (file)
@@ -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<String>,
+    errors: &mut Vec<String>,
     has_lib: bool,
 ) -> CargoResult<Vec<Target>> {
     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)
         }
index c87d588c1d38320d54056f0e29d6cfb45b078760..145e59c8d2102b7bef8832680459e508a0b9dc0c 100644 (file)
@@ -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")