diff --git a/src/cargo/core/compiler/unit_dependencies.rs b/src/cargo/core/compiler/unit_dependencies.rs index bd3288f4d1d..91cfd92660b 100644 --- a/src/cargo/core/compiler/unit_dependencies.rs +++ b/src/cargo/core/compiler/unit_dependencies.rs @@ -142,6 +142,7 @@ fn attach_std_deps( std_unit_deps: UnitGraph, ) { // Attach the standard library as a dependency of every target unit. + let mut found = false; for (unit, deps) in state.unit_dependencies.iter_mut() { if !unit.kind.is_host() && !unit.mode.is_run_custom_build() { deps.extend(std_roots[&unit.kind].iter().map(|unit| UnitDep { @@ -152,12 +153,16 @@ fn attach_std_deps( public: true, noprelude: true, })); + found = true; } } - // And also include the dependencies of the standard library itself. - for (unit, deps) in std_unit_deps.into_iter() { - if let Some(other_unit) = state.unit_dependencies.insert(unit, deps) { - panic!("std unit collision with existing unit: {:?}", other_unit); + // And also include the dependencies of the standard library itself. Don't + // include these if no units actually needed the standard library. + if found { + for (unit, deps) in std_unit_deps.into_iter() { + if let Some(other_unit) = state.unit_dependencies.insert(unit, deps) { + panic!("std unit collision with existing unit: {:?}", other_unit); + } } } } diff --git a/tests/testsuite/standard_lib.rs b/tests/testsuite/standard_lib.rs index b0d42b38d0f..5e2b5719319 100644 --- a/tests/testsuite/standard_lib.rs +++ b/tests/testsuite/standard_lib.rs @@ -662,3 +662,31 @@ fn no_roots() { .with_stderr_contains("[FINISHED] [..]") .run(); } + +#[cargo_test] +fn proc_macro_only() { + // Checks for a bug where it would panic if building a proc-macro only + let setup = match setup() { + Some(s) => s, + None => return, + }; + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "pm" + version = "0.1.0" + + [lib] + proc-macro = true + "#, + ) + .file("src/lib.rs", "") + .build(); + p.cargo("build") + .build_std(&setup) + .target_host() + .with_stderr_contains("[FINISHED] [..]") + .run(); +}