From 3738002e4332420f17e283d1f5d24f5cc58e4b9e Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Sun, 16 Jan 2022 17:36:56 +0800 Subject: [PATCH] Error when setting crate type of both dylib and cdylib in library Signed-off-by: hi-rustin --- src/cargo/util/toml/targets.rs | 9 +++++++++ tests/testsuite/build.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/cargo/util/toml/targets.rs b/src/cargo/util/toml/targets.rs index 473994b285d..af98bc7469d 100644 --- a/src/cargo/util/toml/targets.rs +++ b/src/cargo/util/toml/targets.rs @@ -215,6 +215,15 @@ fn clean_lib( // A plugin requires exporting plugin_registrar so a crate cannot be // both at once. let crate_types = match (lib.crate_types(), lib.plugin, lib.proc_macro()) { + (Some(kinds), _, _) + if kinds.contains(&CrateType::Dylib.as_str().to_owned()) + && kinds.contains(&CrateType::Cdylib.as_str().to_owned()) => + { + anyhow::bail!(format!( + "library `{}` cannot set the crate type of both `dylib` and `cdylib`", + lib.name() + )); + } (Some(kinds), _, _) if kinds.contains(&"proc-macro".to_string()) => { if let Some(true) = lib.plugin { // This is a warning to retain backwards compatibility. diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 487965e432e..b080cc6b188 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -1633,6 +1633,39 @@ fn many_crate_types_correct() { assert!(p.root().join("target/debug").join(&fname).is_file()); } +#[cargo_test] +fn set_both_dylib_and_cdylib_crate_types() { + let p = project() + .file( + "Cargo.toml", + r#" + [project] + + name = "foo" + version = "0.5.0" + authors = ["wycats@example.com"] + + [lib] + + name = "foo" + crate_type = ["cdylib", "dylib"] + "#, + ) + .file("src/lib.rs", "pub fn foo() {}") + .build(); + p.cargo("build") + .with_status(101) + .with_stderr( + "\ +error: failed to parse manifest at `[..]` + +Caused by: + library `foo` cannot set the crate type of both `dylib` and `cdylib` +", + ) + .run(); +} + #[cargo_test] fn self_dependency() { let p = project()