diff --git a/src/bin/cargo/commands/rustc.rs b/src/bin/cargo/commands/rustc.rs index ff660a33518..3db62928f00 100644 --- a/src/bin/cargo/commands/rustc.rs +++ b/src/bin/cargo/commands/rustc.rs @@ -92,14 +92,17 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult { return Ok(()); } - let crate_types = args - .get_many::(CRATE_TYPE_ARG_NAME) - .into_iter() - .flatten() - .flat_map(|s| s.split(',')) - .filter(|s| !s.is_empty()) - .map(String::from) - .collect::>(); + let crate_types = { + let mut seen = std::collections::HashSet::new(); + args.get_many::(CRATE_TYPE_ARG_NAME) + .into_iter() + .flatten() + .flat_map(|s| s.split(',')) + .filter(|s| !s.is_empty()) + .map(String::from) + .filter(|s| seen.insert(s.clone())) + .collect::>() + }; compile_opts.target_rustc_crate_types = if crate_types.is_empty() { None diff --git a/tests/testsuite/rustc.rs b/tests/testsuite/rustc.rs index 124877f5468..6e9b5b22844 100644 --- a/tests/testsuite/rustc.rs +++ b/tests/testsuite/rustc.rs @@ -855,3 +855,25 @@ fn precedence() { "#]]) .run(); } + +#[cargo_test] +fn build_with_duplicate_crate_types() { + let p = project().file("src/lib.rs", "").build(); + + p.cargo("rustc -v --crate-type staticlib --crate-type staticlib") + .with_stderr_data(str![[r#" +[COMPILING] foo v0.0.1 ([ROOT]/foo) +[RUNNING] `rustc [..] --crate-type staticlib --emit[..] +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) + .run(); + + p.cargo("rustc -v --crate-type staticlib --crate-type staticlib") + .with_stderr_data(str![[r#" +[FRESH] foo v0.0.1 ([ROOT]/foo) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) + .run(); +}