Skip to content

Commit

Permalink
Auto merge of #5204 - lukaslueg:issue5199, r=alexcrichton
Browse files Browse the repository at this point in the history
Do not allow crate-type or proc-macro for [[bin]]-targets

Fixes #5199

This simply disallows `proc-macro` and `crate-type` to be set to anything for binary targets. Is this the best way to go or does a warning about the unused setting suffice?
  • Loading branch information
bors committed Mar 21, 2018
2 parents 0a30bf0 + 257c233 commit 4dde726
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/cargo/util/toml/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub fn targets(
package_root,
package_name,
warnings,
errors,
has_lib,
)?);

Expand Down Expand Up @@ -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);
Expand All @@ -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)
}
Expand Down
65 changes: 65 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 4dde726

Please sign in to comment.