From 3ab596514a34cf3f5ef75ec4ac9ddf2f0b4d268f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Sat, 22 Feb 2020 00:00:00 +0000 Subject: [PATCH] Improve linking of crates with circular dependencies Previously, the code responsible for handling the cycles between crates introduces through weak lang items, would keep a set of missing language items: * extending it with items missing from the current crate, * removing items provided by the current crate, * grouping the crates when the set changed from non-empty back to empty. This could produce incorrect results, if a lang item was missing from a crate that comes after the crate that provides it (in the loop iteration order). In that case the grouping would not take place. The changes here address this specific failure scenario by keeping track of two separate sets of crates. Those that are required to link successfully, and those that are available for linking. Verified using test case from 69368. --- src/librustc_codegen_ssa/back/link.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/librustc_codegen_ssa/back/link.rs b/src/librustc_codegen_ssa/back/link.rs index 90601521b19..847b5b81d5a 100644 --- a/src/librustc_codegen_ssa/back/link.rs +++ b/src/librustc_codegen_ssa/back/link.rs @@ -1519,17 +1519,25 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>( // for the current implementation of the standard library. let mut group_end = None; let mut group_start = None; - let mut end_with = FxHashSet::default(); + // Crates available for linking thus far. + let mut available = FxHashSet::default(); + // Crates required to satisfy dependencies discovered so far. + let mut required = FxHashSet::default(); + let info = &codegen_results.crate_info; for &(cnum, _) in deps.iter().rev() { if let Some(missing) = info.missing_lang_items.get(&cnum) { - end_with.extend(missing.iter().cloned()); - if !end_with.is_empty() && group_end.is_none() { - group_end = Some(cnum); - } + let missing_crates = missing.iter().map(|i| info.lang_item_to_crate.get(i).copied()); + required.extend(missing_crates); } - end_with.retain(|item| info.lang_item_to_crate.get(item) != Some(&cnum)); - if end_with.is_empty() && group_end.is_some() { + + required.insert(Some(cnum)); + available.insert(Some(cnum)); + + if required.len() > available.len() && group_end.is_none() { + group_end = Some(cnum); + } + if required.len() == available.len() && group_end.is_some() { group_start = Some(cnum); break; }