From 6b472c90e18a04c4b519ef6ff7f8ff6905364b7a Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Fri, 4 Dec 2020 13:34:09 -0800 Subject: [PATCH] Fix panic with -Zbuild-std and no roots. --- src/cargo/core/compiler/unit_dependencies.rs | 6 ++++++ tests/testsuite/standard_lib.rs | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/cargo/core/compiler/unit_dependencies.rs b/src/cargo/core/compiler/unit_dependencies.rs index b6ae6df45ec..f0dd340cc99 100644 --- a/src/cargo/core/compiler/unit_dependencies.rs +++ b/src/cargo/core/compiler/unit_dependencies.rs @@ -62,6 +62,12 @@ pub fn build_unit_dependencies<'a, 'cfg>( profiles: &'a Profiles, interner: &'a UnitInterner, ) -> CargoResult { + if roots.is_empty() { + // If -Zbuild-std, don't attach units if there is nothing to build. + // Otherwise, other parts of the code may be confused by seeing units + // in the dep graph without a root. + return Ok(HashMap::new()); + } let (std_resolve, std_features) = match std_resolve { Some((r, f)) => (Some(r), Some(f)), None => (None, None), diff --git a/tests/testsuite/standard_lib.rs b/tests/testsuite/standard_lib.rs index 4a091d71e5b..2e0adf92aa1 100644 --- a/tests/testsuite/standard_lib.rs +++ b/tests/testsuite/standard_lib.rs @@ -677,3 +677,23 @@ fn different_features() { .target_host() .run(); } + +#[cargo_test] +fn no_roots() { + // Checks for a bug where it would panic if there are no roots. + let setup = match setup() { + Some(s) => s, + None => return, + }; + let p = project().file("tests/t1.rs", "").build(); + p.cargo("build") + .build_std(&setup) + .target_host() + .with_stderr( + "\ +[UPDATING] [..] +[FINISHED] [..] +", + ) + .run(); +}