Skip to content

Commit 57a7c8f

Browse files
committed
Fix bug in mark_code_coverage_dead_code_cgus.
The comment says "Find the smallest CGU that has exported symbols and put the dead function stubs in that CGU". But the code sorts the CGUs by size (smallest first) and then searches them in reverse order, which means it will find the *largest* CGU that has exported symbols. The erroneous code was introduced in rust-lang#92142. This commit changes it to use a simpler search, avoiding the sort, and fixes the bug in the process.
1 parent 9d7295f commit 57a7c8f

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

compiler/rustc_monomorphize/src/partitioning.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -529,20 +529,15 @@ fn mark_code_coverage_dead_code_cgu<'tcx>(codegen_units: &mut [CodegenUnit<'tcx>
529529
// the object file (CGU) containing the dead function stubs is included
530530
// in the final binary. This will probably require forcing these
531531
// function symbols to be included via `-u` or `/include` linker args.
532-
let mut cgus: Vec<&mut CodegenUnit<'tcx>> = codegen_units.iter_mut().collect();
533-
cgus.sort_by_key(|cgu| cgu.size_estimate());
532+
let dead_code_cgu = codegen_units
533+
.iter_mut()
534+
.filter(|cgu| cgu.items().iter().any(|(_, (linkage, _))| *linkage == Linkage::External))
535+
.min_by_key(|cgu| cgu.size_estimate());
536+
537+
// If there are no CGUs that have externally linked items, then we just
538+
// pick the first CGU as a fallback.
539+
let dead_code_cgu = if let Some(cgu) = dead_code_cgu { cgu } else { &mut codegen_units[0] };
534540

535-
let dead_code_cgu = if let Some(cgu) = cgus
536-
.into_iter()
537-
.rev()
538-
.find(|cgu| cgu.items().iter().any(|(_, (linkage, _))| *linkage == Linkage::External))
539-
{
540-
cgu
541-
} else {
542-
// If there are no CGUs that have externally linked items,
543-
// then we just pick the first CGU as a fallback.
544-
&mut codegen_units[0]
545-
};
546541
dead_code_cgu.make_code_coverage_dead_code_cgu();
547542
}
548543

0 commit comments

Comments
 (0)