From fbdff5145ac3f6ecfe7bde656cfa41f5a77c8f10 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 4 Apr 2020 12:19:10 +0200 Subject: [PATCH] avoid printing allocations twice --- src/librustc_mir/interpret/machine.rs | 2 +- src/librustc_mir/interpret/memory.rs | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/librustc_mir/interpret/machine.rs b/src/librustc_mir/interpret/machine.rs index 48082a1e346..8875baad58c 100644 --- a/src/librustc_mir/interpret/machine.rs +++ b/src/librustc_mir/interpret/machine.rs @@ -51,7 +51,7 @@ fn remove(&mut self, k: &Q) -> Option where K: Borrow; - /// Returns data based the keys and values in the map. + /// Returns data based on the keys and values in the map. fn filter_map_collect(&self, f: impl FnMut(&K, &V) -> Option) -> Vec; /// Returns a reference to entry `k`. If no such entry exists, call diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index a97df313ea0..66357a48634 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -646,14 +646,11 @@ pub fn dump_alloc(&self, id: AllocId) { fn dump_alloc_helper( &self, - allocs_seen: &mut FxHashSet, allocs_to_print: &mut VecDeque, alloc: &Allocation, ) { for &(_, target_id) in alloc.relocations().values() { - if allocs_seen.insert(target_id) { - allocs_to_print.push_back(target_id); - } + allocs_to_print.push_back(target_id); } crate::util::pretty::write_allocation(self.tcx.tcx, alloc, &mut std::io::stderr(), "") .unwrap(); @@ -666,9 +663,14 @@ pub fn dump_allocs(&self, mut allocs: Vec) { allocs.sort(); allocs.dedup(); let mut allocs_to_print = VecDeque::from(allocs); - let mut allocs_seen = FxHashSet::default(); + // `allocs_printed` contains all allocations that we have already printed. + let mut allocs_printed = FxHashSet::default(); while let Some(id) = allocs_to_print.pop_front() { + if !allocs_printed.insert(id) { + // Already printed, so skip this. + continue; + } eprint!("Alloc {:<5}: ", id); fn msg(alloc: &Allocation, extra: &str) { eprintln!( @@ -688,14 +690,14 @@ fn msg(alloc: &Allocation, extra: &str) { MemoryKind::CallerLocation => msg(alloc, " (caller_location)"), MemoryKind::Machine(m) => msg(alloc, &format!(" ({:?})", m)), }; - self.dump_alloc_helper(&mut allocs_seen, &mut allocs_to_print, alloc); + self.dump_alloc_helper(&mut allocs_to_print, alloc); } Err(()) => { // global alloc? match self.tcx.alloc_map.lock().get(id) { Some(GlobalAlloc::Memory(alloc)) => { msg(alloc, " (immutable)"); - self.dump_alloc_helper(&mut allocs_seen, &mut allocs_to_print, alloc); + self.dump_alloc_helper(&mut allocs_to_print, alloc); } Some(GlobalAlloc::Function(func)) => { eprintln!("{}", func); @@ -722,8 +724,8 @@ pub fn leak_report(&self) -> usize { }); while let Some(id) = todo.pop() { if reachable.insert(id) { + // This is a new allocation, add its relocations to `todo`. if let Some((_, alloc)) = self.alloc_map.get(id) { - // This is a new allocation, add its relocations to `todo`. todo.extend(alloc.relocations().values().map(|&(_, target_id)| target_id)); } }