From 46c545c1ba7a859825b4760cbf6360f4d0a8e3de Mon Sep 17 00:00:00 2001 From: Zalathar Date: Mon, 16 Oct 2023 20:52:20 +1100 Subject: [PATCH 01/14] coverage: Rename `check_invoked_macro_name_span` to `maybe_push_macro_name_span` --- compiler/rustc_mir_transform/src/coverage/spans.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index 506bcea0e3914..2c5aeeee84186 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -290,12 +290,12 @@ impl<'a> CoverageSpansGenerator<'a> { while self.next_coverage_span() { if self.some_prev.is_none() { debug!(" initial span"); - self.check_invoked_macro_name_span(); + self.maybe_push_macro_name_span(); } else if self.curr().is_mergeable(self.prev()) { debug!(" same bcb (and neither is a closure), merge with prev={:?}", self.prev()); let prev = self.take_prev(); self.curr_mut().merge_from(prev); - self.check_invoked_macro_name_span(); + self.maybe_push_macro_name_span(); // Note that curr.span may now differ from curr_original_span } else if self.prev_ends_before_curr() { debug!( @@ -305,7 +305,7 @@ impl<'a> CoverageSpansGenerator<'a> { ); let prev = self.take_prev(); self.push_refined_span(prev); - self.check_invoked_macro_name_span(); + self.maybe_push_macro_name_span(); } else if self.prev().is_closure { // drop any equal or overlapping span (`curr`) and keep `prev` to test again in the // next iter @@ -347,7 +347,7 @@ impl<'a> CoverageSpansGenerator<'a> { } } else { self.cutoff_prev_at_overlapping_curr(); - self.check_invoked_macro_name_span(); + self.maybe_push_macro_name_span(); } } @@ -399,7 +399,9 @@ impl<'a> CoverageSpansGenerator<'a> { self.refined_spans.push(covspan) } - fn check_invoked_macro_name_span(&mut self) { + /// If `curr` is part of a new macro expansion, carve out and push a separate + /// span that ends just after the macro name and its subsequent `!`. + fn maybe_push_macro_name_span(&mut self) { if let Some(visible_macro) = self.curr().visible_macro(self.body_span) { if !self .prev_expn_span From 9b6ce4fb3c86b9b1eadf6cef537d17138ec56d1e Mon Sep 17 00:00:00 2001 From: Zalathar Date: Mon, 16 Oct 2023 20:53:41 +1100 Subject: [PATCH 02/14] coverage: Rename `check_pending_dups` to `maybe_flush_pending_dups` This method's main responsibility is to flush the pending dups into refined spans, if appropriate. --- compiler/rustc_mir_transform/src/coverage/spans.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index 2c5aeeee84186..f9b7e74b0b5ad 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -462,7 +462,7 @@ impl<'a> CoverageSpansGenerator<'a> { /// `pending_dups` could have as few as one span) /// In either case, no more spans will match the span of `pending_dups`, so /// add the `pending_dups` if they don't overlap `curr`, and clear the list. - fn check_pending_dups(&mut self) { + fn maybe_flush_pending_dups(&mut self) { if let Some(dup) = self.pending_dups.last() && dup.span != self.prev().span { @@ -502,7 +502,7 @@ impl<'a> CoverageSpansGenerator<'a> { // by `self.curr_mut().merge_from(prev)`. self.curr_original_span = curr.span; self.some_curr.replace(curr); - self.check_pending_dups(); + self.maybe_flush_pending_dups(); return true; } } From d928d3e5d85c850ed2fe07dbeb00b3130ecfe6a5 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sun, 15 Oct 2023 16:39:27 +1100 Subject: [PATCH 03/14] coverage: Rename `hold_pending_dups_unless_dominated` to `update_pending_dups` --- compiler/rustc_mir_transform/src/coverage/spans.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index f9b7e74b0b5ad..e574ded8e2b53 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -343,7 +343,7 @@ impl<'a> CoverageSpansGenerator<'a> { ); self.take_curr(); } else { - self.hold_pending_dups_unless_dominated(); + self.update_pending_dups(); } } else { self.cutoff_prev_at_overlapping_curr(); @@ -585,7 +585,7 @@ impl<'a> CoverageSpansGenerator<'a> { /// neither `CoverageSpan` dominates the other, both (or possibly more than two) are held, /// until their disposition is determined. In this latter case, the `prev` dup is moved into /// `pending_dups` so the new `curr` dup can be moved to `prev` for the next iteration. - fn hold_pending_dups_unless_dominated(&mut self) { + fn update_pending_dups(&mut self) { // Equal coverage spans are ordered by dominators before dominated (if any), so it should be // impossible for `curr` to dominate any previous `CoverageSpan`. debug_assert!(!self.span_bcb_dominates(self.curr(), self.prev())); From fa2e26285c9b44ff709ad1cfc291eb9d7a0c199d Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sun, 15 Oct 2023 12:22:59 +1100 Subject: [PATCH 04/14] coverage: Use `DUMMY_SP` instead of creating a dummy span manually This patch also sorts the constructor fields into declaration order. --- compiler/rustc_mir_transform/src/coverage/spans.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index e574ded8e2b53..4af0a96de3b9d 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -3,7 +3,7 @@ use std::cell::OnceCell; use rustc_data_structures::graph::WithNumNodes; use rustc_index::IndexVec; use rustc_middle::mir::{self, AggregateKind, Rvalue, Statement, StatementKind}; -use rustc_span::{BytePos, ExpnKind, MacroKind, Span, Symbol}; +use rustc_span::{BytePos, ExpnKind, MacroKind, Span, Symbol, DUMMY_SP}; use super::graph::{BasicCoverageBlock, CoverageGraph, START_BCB}; @@ -272,13 +272,13 @@ impl<'a> CoverageSpansGenerator<'a> { body_span, basic_coverage_blocks, sorted_spans_iter: sorted_spans.into_iter(), - refined_spans: Vec::with_capacity(basic_coverage_blocks.num_nodes() * 2), some_curr: None, - curr_original_span: Span::with_root_ctxt(BytePos(0), BytePos(0)), + curr_original_span: DUMMY_SP, some_prev: None, - prev_original_span: Span::with_root_ctxt(BytePos(0), BytePos(0)), + prev_original_span: DUMMY_SP, prev_expn_span: None, pending_dups: Vec::new(), + refined_spans: Vec::with_capacity(basic_coverage_blocks.num_nodes() * 2), }; coverage_spans.to_refined_spans() From 5f1e8f99505b9be3cb8a474dee64c1770070de18 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 14 Oct 2023 21:52:36 +1100 Subject: [PATCH 05/14] coverage: Simplify `push_refined_span` It turns out that all of the `len` manipulation here was just reimplementing `last_mut`. --- .../rustc_mir_transform/src/coverage/spans.rs | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index 4af0a96de3b9d..3d2fe2913f4e4 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -384,19 +384,15 @@ impl<'a> CoverageSpansGenerator<'a> { } fn push_refined_span(&mut self, covspan: CoverageSpan) { - let len = self.refined_spans.len(); - if len > 0 { - let last = &mut self.refined_spans[len - 1]; - if last.is_mergeable(&covspan) { - debug!( - "merging new refined span with last refined span, last={:?}, covspan={:?}", - last, covspan - ); - last.merge_from(covspan); - return; - } + if let Some(last) = self.refined_spans.last_mut() + && last.is_mergeable(&covspan) + { + // Instead of pushing the new span, merge it with the last refined span. + debug!(?last, ?covspan, "merging new refined span with last refined span"); + last.merge_from(covspan); + } else { + self.refined_spans.push(covspan); } - self.refined_spans.push(covspan) } /// If `curr` is part of a new macro expansion, carve out and push a separate From 97d1a9120e565949b84e760e33e82fcd27325f36 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Mon, 16 Oct 2023 20:54:56 +1100 Subject: [PATCH 06/14] coverage: Flatten guard logic in `maybe_push_macro_name_span` --- .../rustc_mir_transform/src/coverage/spans.rs | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index 3d2fe2913f4e4..ad07313645720 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -398,27 +398,24 @@ impl<'a> CoverageSpansGenerator<'a> { /// If `curr` is part of a new macro expansion, carve out and push a separate /// span that ends just after the macro name and its subsequent `!`. fn maybe_push_macro_name_span(&mut self) { - if let Some(visible_macro) = self.curr().visible_macro(self.body_span) { - if !self - .prev_expn_span - .is_some_and(|prev_expn_span| self.curr().expn_span.ctxt() == prev_expn_span.ctxt()) - { - let merged_prefix_len = self.curr_original_span.lo() - self.curr().span.lo(); - let after_macro_bang = - merged_prefix_len + BytePos(visible_macro.as_str().len() as u32 + 1); - let mut macro_name_cov = self.curr().clone(); - self.curr_mut().span = - self.curr().span.with_lo(self.curr().span.lo() + after_macro_bang); - macro_name_cov.span = - macro_name_cov.span.with_hi(macro_name_cov.span.lo() + after_macro_bang); - debug!( - " and curr starts a new macro expansion, so add a new span just for \ - the macro `{}!`, new span={:?}", - visible_macro, macro_name_cov - ); - self.push_refined_span(macro_name_cov); - } + let Some(visible_macro) = self.curr().visible_macro(self.body_span) else { return }; + if let Some(prev_expn_span) = &self.prev_expn_span + && prev_expn_span.ctxt() == self.curr().expn_span.ctxt() + { + return; } + + let merged_prefix_len = self.curr_original_span.lo() - self.curr().span.lo(); + let after_macro_bang = merged_prefix_len + BytePos(visible_macro.as_str().len() as u32 + 1); + let mut macro_name_cov = self.curr().clone(); + self.curr_mut().span = self.curr().span.with_lo(self.curr().span.lo() + after_macro_bang); + macro_name_cov.span = + macro_name_cov.span.with_hi(macro_name_cov.span.lo() + after_macro_bang); + debug!( + " and curr starts a new macro expansion, so add a new span just for \ + the macro `{visible_macro}!`, new span={macro_name_cov:?}", + ); + self.push_refined_span(macro_name_cov); } fn curr(&self) -> &CoverageSpan { From 7bbe4be5685c7e3ba7bb72f921cee08f48db429d Mon Sep 17 00:00:00 2001 From: Zalathar Date: Mon, 16 Oct 2023 20:56:16 +1100 Subject: [PATCH 07/14] coverage: Flatten guard logic in `maybe_flush_pending_dups` --- .../rustc_mir_transform/src/coverage/spans.rs | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index ad07313645720..2e415a61417fe 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -456,22 +456,23 @@ impl<'a> CoverageSpansGenerator<'a> { /// In either case, no more spans will match the span of `pending_dups`, so /// add the `pending_dups` if they don't overlap `curr`, and clear the list. fn maybe_flush_pending_dups(&mut self) { - if let Some(dup) = self.pending_dups.last() - && dup.span != self.prev().span - { - debug!( - " SAME spans, but pending_dups are NOT THE SAME, so BCBs matched on \ - previous iteration, or prev started a new disjoint span" - ); - if dup.span.hi() <= self.curr().span.lo() { - let pending_dups = self.pending_dups.split_off(0); - for dup in pending_dups.into_iter() { - debug!(" ...adding at least one pending={:?}", dup); - self.push_refined_span(dup); - } - } else { - self.pending_dups.clear(); + let Some(last_dup) = self.pending_dups.last() else { return }; + if last_dup.span == self.prev().span { + return; + } + + debug!( + " SAME spans, but pending_dups are NOT THE SAME, so BCBs matched on \ + previous iteration, or prev started a new disjoint span" + ); + if last_dup.span.hi() <= self.curr().span.lo() { + let pending_dups = self.pending_dups.split_off(0); + for dup in pending_dups.into_iter() { + debug!(" ...adding at least one pending={:?}", dup); + self.push_refined_span(dup); } + } else { + self.pending_dups.clear(); } } From 9bb27f3adfe1df21d1c85b96aaf75f0b349d3ab4 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sun, 15 Oct 2023 13:13:05 +1100 Subject: [PATCH 08/14] coverage: Remove redundant field `prev_expn_span` This span can always be retrieved from `prev`, so there is no need to store it separately. --- compiler/rustc_mir_transform/src/coverage/spans.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index 2e415a61417fe..3e627ac7a09b0 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -215,9 +215,6 @@ struct CoverageSpansGenerator<'a> { /// is mutated. prev_original_span: Span, - /// A copy of the expn_span from the prior iteration. - prev_expn_span: Option, - /// One or more `CoverageSpan`s with the same `Span` but different `BasicCoverageBlock`s, and /// no `BasicCoverageBlock` in this list dominates another `BasicCoverageBlock` in the list. /// If a new `curr` span also fits this criteria (compared to an existing list of @@ -276,7 +273,6 @@ impl<'a> CoverageSpansGenerator<'a> { curr_original_span: DUMMY_SP, some_prev: None, prev_original_span: DUMMY_SP, - prev_expn_span: None, pending_dups: Vec::new(), refined_spans: Vec::with_capacity(basic_coverage_blocks.num_nodes() * 2), }; @@ -399,8 +395,8 @@ impl<'a> CoverageSpansGenerator<'a> { /// span that ends just after the macro name and its subsequent `!`. fn maybe_push_macro_name_span(&mut self) { let Some(visible_macro) = self.curr().visible_macro(self.body_span) else { return }; - if let Some(prev_expn_span) = &self.prev_expn_span - && prev_expn_span.ctxt() == self.curr().expn_span.ctxt() + if let Some(prev) = &self.some_prev + && prev.expn_span.ctxt() == self.curr().expn_span.ctxt() { return; } @@ -479,7 +475,6 @@ impl<'a> CoverageSpansGenerator<'a> { /// Advance `prev` to `curr` (if any), and `curr` to the next `CoverageSpan` in sorted order. fn next_coverage_span(&mut self) -> bool { if let Some(curr) = self.some_curr.take() { - self.prev_expn_span = Some(curr.expn_span); self.some_prev = Some(curr); self.prev_original_span = self.curr_original_span; } From b1c44f4a25bb7cec5941362143f2fc0abbc67925 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sun, 15 Oct 2023 13:17:47 +1100 Subject: [PATCH 09/14] coverage: Call `prev`/`curr` less in `to_refined_spans` This makes it easier to see that the non-initial cases assume that `prev` and `curr` are set, and all operate on the same prev/curr references. --- .../rustc_mir_transform/src/coverage/spans.rs | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index 3e627ac7a09b0..503cad0e9dc37 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -284,43 +284,48 @@ impl<'a> CoverageSpansGenerator<'a> { /// de-duplicated `CoverageSpan`s. fn to_refined_spans(mut self) -> Vec { while self.next_coverage_span() { + // For the first span we don't have `prev` set, so most of the + // span-processing steps don't make sense yet. if self.some_prev.is_none() { debug!(" initial span"); self.maybe_push_macro_name_span(); - } else if self.curr().is_mergeable(self.prev()) { - debug!(" same bcb (and neither is a closure), merge with prev={:?}", self.prev()); + continue; + } + + // The remaining cases assume that `prev` and `curr` are set. + let prev = self.prev(); + let curr = self.curr(); + + if curr.is_mergeable(prev) { + debug!(" same bcb (and neither is a closure), merge with prev={prev:?}"); let prev = self.take_prev(); self.curr_mut().merge_from(prev); self.maybe_push_macro_name_span(); // Note that curr.span may now differ from curr_original_span - } else if self.prev_ends_before_curr() { + } else if prev.span.hi() <= curr.span.lo() { debug!( - " different bcbs and disjoint spans, so keep curr for next iter, and add \ - prev={:?}", - self.prev() + " different bcbs and disjoint spans, so keep curr for next iter, and add prev={prev:?}", ); let prev = self.take_prev(); self.push_refined_span(prev); self.maybe_push_macro_name_span(); - } else if self.prev().is_closure { + } else if prev.is_closure { // drop any equal or overlapping span (`curr`) and keep `prev` to test again in the // next iter debug!( - " curr overlaps a closure (prev). Drop curr and keep prev for next iter. \ - prev={:?}", - self.prev() + " curr overlaps a closure (prev). Drop curr and keep prev for next iter. prev={prev:?}", ); self.take_curr(); - } else if self.curr().is_closure { + } else if curr.is_closure { self.carve_out_span_for_closure(); - } else if self.prev_original_span == self.curr().span { + } else if self.prev_original_span == curr.span { // Note that this compares the new (`curr`) span to `prev_original_span`. // In this branch, the actual span byte range of `prev_original_span` is not // important. What is important is knowing whether the new `curr` span was // **originally** the same as the original span of `prev()`. The original spans // reflect their original sort order, and for equal spans, conveys a partial // ordering based on CFG dominator priority. - if self.prev().is_macro_expansion() && self.curr().is_macro_expansion() { + if prev.is_macro_expansion() && curr.is_macro_expansion() { // Macros that expand to include branching (such as // `assert_eq!()`, `assert_ne!()`, `info!()`, `debug!()`, or // `trace!()`) typically generate callee spans with identical @@ -334,8 +339,7 @@ impl<'a> CoverageSpansGenerator<'a> { debug!( " curr and prev are part of a macro expansion, and curr has the same span \ as prev, but is in a different bcb. Drop curr and keep prev for next iter. \ - prev={:?}", - self.prev() + prev={prev:?}", ); self.take_curr(); } else { @@ -347,8 +351,8 @@ impl<'a> CoverageSpansGenerator<'a> { } } - debug!(" AT END, adding last prev={:?}", self.prev()); let prev = self.take_prev(); + debug!(" AT END, adding last prev={prev:?}"); let pending_dups = self.pending_dups.split_off(0); for dup in pending_dups { debug!(" ...adding at least one pending dup={:?}", dup); @@ -511,12 +515,6 @@ impl<'a> CoverageSpansGenerator<'a> { self.prev().span.lo() > next_curr.span.lo() } - /// Returns true if the curr span starts past the end of the prev span, which means they don't - /// overlap, so we now know the prev can be added to the refined coverage spans. - fn prev_ends_before_curr(&self) -> bool { - self.prev().span.hi() <= self.curr().span.lo() - } - /// If `prev`s span extends left of the closure (`curr`), carve out the closure's span from /// `prev`'s span. (The closure's coverage counters will be injected when processing the /// closure's own MIR.) Add the portion of the span to the left of the closure; and if the span From 41038dbe4a350f09fe6da66322a2ce8c874c4563 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sun, 15 Oct 2023 13:23:26 +1100 Subject: [PATCH 10/14] coverage: Call `prev`/`curr` less in other places This reduces clutter, and makes it easier to notice regions where mutations definitely don't occur. --- .../rustc_mir_transform/src/coverage/spans.rs | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index 503cad0e9dc37..dd3b51e7f0988 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -398,17 +398,19 @@ impl<'a> CoverageSpansGenerator<'a> { /// If `curr` is part of a new macro expansion, carve out and push a separate /// span that ends just after the macro name and its subsequent `!`. fn maybe_push_macro_name_span(&mut self) { - let Some(visible_macro) = self.curr().visible_macro(self.body_span) else { return }; + let curr = self.curr(); + + let Some(visible_macro) = curr.visible_macro(self.body_span) else { return }; if let Some(prev) = &self.some_prev - && prev.expn_span.ctxt() == self.curr().expn_span.ctxt() + && prev.expn_span.ctxt() == curr.expn_span.ctxt() { return; } - let merged_prefix_len = self.curr_original_span.lo() - self.curr().span.lo(); + let merged_prefix_len = self.curr_original_span.lo() - curr.span.lo(); let after_macro_bang = merged_prefix_len + BytePos(visible_macro.as_str().len() as u32 + 1); - let mut macro_name_cov = self.curr().clone(); - self.curr_mut().span = self.curr().span.with_lo(self.curr().span.lo() + after_macro_bang); + let mut macro_name_cov = curr.clone(); + self.curr_mut().span = curr.span.with_lo(curr.span.lo() + after_macro_bang); macro_name_cov.span = macro_name_cov.span.with_hi(macro_name_cov.span.lo() + after_macro_bang); debug!( @@ -521,11 +523,14 @@ impl<'a> CoverageSpansGenerator<'a> { /// extends to the right of the closure, update `prev` to that portion of the span. For any /// `pending_dups`, repeat the same process. fn carve_out_span_for_closure(&mut self) { - let curr_span = self.curr().span; - let left_cutoff = curr_span.lo(); - let right_cutoff = curr_span.hi(); - let has_pre_closure_span = self.prev().span.lo() < right_cutoff; - let has_post_closure_span = self.prev().span.hi() > right_cutoff; + let prev = self.prev(); + let curr = self.curr(); + + let left_cutoff = curr.span.lo(); + let right_cutoff = curr.span.hi(); + let has_pre_closure_span = prev.span.lo() < right_cutoff; + let has_post_closure_span = prev.span.hi() > right_cutoff; + let mut pending_dups = self.pending_dups.split_off(0); if has_pre_closure_span { let mut pre_closure = self.prev().clone(); @@ -580,7 +585,8 @@ impl<'a> CoverageSpansGenerator<'a> { let initial_pending_count = self.pending_dups.len(); if initial_pending_count > 0 { let mut pending_dups = self.pending_dups.split_off(0); - pending_dups.retain(|dup| !self.span_bcb_dominates(dup, self.curr())); + let curr = self.curr(); + pending_dups.retain(|dup| !self.span_bcb_dominates(dup, curr)); self.pending_dups.append(&mut pending_dups); if self.pending_dups.len() < initial_pending_count { debug!( From 25e63032020a2bb97a00ff4db3e0be6a1b7494df Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sun, 15 Oct 2023 17:20:43 +1100 Subject: [PATCH 11/14] coverage: Move `take_curr` and note what its callers are doing --- .../rustc_mir_transform/src/coverage/spans.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index dd3b51e7f0988..63d2a2eeaebb0 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -315,7 +315,7 @@ impl<'a> CoverageSpansGenerator<'a> { debug!( " curr overlaps a closure (prev). Drop curr and keep prev for next iter. prev={prev:?}", ); - self.take_curr(); + self.take_curr(); // Discards curr. } else if curr.is_closure { self.carve_out_span_for_closure(); } else if self.prev_original_span == curr.span { @@ -341,7 +341,7 @@ impl<'a> CoverageSpansGenerator<'a> { as prev, but is in a different bcb. Drop curr and keep prev for next iter. \ prev={prev:?}", ); - self.take_curr(); + self.take_curr(); // Discards curr. } else { self.update_pending_dups(); } @@ -432,6 +432,12 @@ impl<'a> CoverageSpansGenerator<'a> { .unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_curr")) } + /// If called, then the next call to `next_coverage_span()` will *not* update `prev` with the + /// `curr` coverage span. + fn take_curr(&mut self) -> CoverageSpan { + self.some_curr.take().unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_curr")) + } + fn prev(&self) -> &CoverageSpan { self.some_prev .as_ref() @@ -504,12 +510,6 @@ impl<'a> CoverageSpansGenerator<'a> { false } - /// If called, then the next call to `next_coverage_span()` will *not* update `prev` with the - /// `curr` coverage span. - fn take_curr(&mut self) -> CoverageSpan { - self.some_curr.take().unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_curr")) - } - /// Returns true if the curr span should be skipped because prev has already advanced beyond the /// end of curr. This can only happen if a prior iteration updated `prev` to skip past a region /// of code, such as skipping past a closure. @@ -556,7 +556,7 @@ impl<'a> CoverageSpansGenerator<'a> { dup.span = dup.span.with_lo(right_cutoff); } self.pending_dups.append(&mut pending_dups); - let closure_covspan = self.take_curr(); + let closure_covspan = self.take_curr(); // Prevent this curr from becoming prev. self.push_refined_span(closure_covspan); // since self.prev() was already updated } else { pending_dups.clear(); From 4ab4273d6425e69d0d4652d15f84adbc00f093bd Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 14 Oct 2023 22:15:18 +1100 Subject: [PATCH 12/14] coverage: Inline `prev_starts_after_next` --- .../rustc_mir_transform/src/coverage/spans.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index 63d2a2eeaebb0..66d49bd33db4f 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -492,11 +492,13 @@ impl<'a> CoverageSpansGenerator<'a> { } while let Some(curr) = self.sorted_spans_iter.next() { debug!("FOR curr={:?}", curr); - if self.some_prev.is_some() && self.prev_starts_after_next(&curr) { + if let Some(prev) = &self.some_prev && prev.span.lo() > curr.span.lo() { + // Skip curr because prev has already advanced beyond the end of curr. + // This can only happen if a prior iteration updated `prev` to skip past + // a region of code, such as skipping past a closure. debug!( " prev.span starts after curr.span, so curr will be dropped (skipping past \ - closure?); prev={:?}", - self.prev() + closure?); prev={prev:?}", ); } else { // Save a copy of the original span for `curr` in case the `CoverageSpan` is changed @@ -510,13 +512,6 @@ impl<'a> CoverageSpansGenerator<'a> { false } - /// Returns true if the curr span should be skipped because prev has already advanced beyond the - /// end of curr. This can only happen if a prior iteration updated `prev` to skip past a region - /// of code, such as skipping past a closure. - fn prev_starts_after_next(&self, next_curr: &CoverageSpan) -> bool { - self.prev().span.lo() > next_curr.span.lo() - } - /// If `prev`s span extends left of the closure (`curr`), carve out the closure's span from /// `prev`'s span. (The closure's coverage counters will be injected when processing the /// closure's own MIR.) Add the portion of the span to the left of the closure; and if the span From 5e5a8e77695a9156e9b78e7d5d7b6b44c26c0cae Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sun, 15 Oct 2023 16:54:12 +1100 Subject: [PATCH 13/14] coverage: Inline `span_bcb_dominates` Interacting with `basic_coverage_blocks` directly makes it easier to satisfy the borrow checker when mutating `pending_dups` while reading other fields. --- .../rustc_mir_transform/src/coverage/spans.rs | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index 66d49bd33db4f..b71a3101bac81 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -573,26 +573,27 @@ impl<'a> CoverageSpansGenerator<'a> { /// until their disposition is determined. In this latter case, the `prev` dup is moved into /// `pending_dups` so the new `curr` dup can be moved to `prev` for the next iteration. fn update_pending_dups(&mut self) { + let prev_bcb = self.prev().bcb; + let curr_bcb = self.curr().bcb; + // Equal coverage spans are ordered by dominators before dominated (if any), so it should be // impossible for `curr` to dominate any previous `CoverageSpan`. - debug_assert!(!self.span_bcb_dominates(self.curr(), self.prev())); + debug_assert!(!self.basic_coverage_blocks.dominates(curr_bcb, prev_bcb)); let initial_pending_count = self.pending_dups.len(); if initial_pending_count > 0 { - let mut pending_dups = self.pending_dups.split_off(0); - let curr = self.curr(); - pending_dups.retain(|dup| !self.span_bcb_dominates(dup, curr)); - self.pending_dups.append(&mut pending_dups); - if self.pending_dups.len() < initial_pending_count { + self.pending_dups + .retain(|dup| !self.basic_coverage_blocks.dominates(dup.bcb, curr_bcb)); + + let n_discarded = initial_pending_count - self.pending_dups.len(); + if n_discarded > 0 { debug!( - " discarded {} of {} pending_dups that dominated curr", - initial_pending_count - self.pending_dups.len(), - initial_pending_count + " discarded {n_discarded} of {initial_pending_count} pending_dups that dominated curr", ); } } - if self.span_bcb_dominates(self.prev(), self.curr()) { + if self.basic_coverage_blocks.dominates(prev_bcb, curr_bcb) { debug!( " different bcbs but SAME spans, and prev dominates curr. Discard prev={:?}", self.prev() @@ -657,8 +658,4 @@ impl<'a> CoverageSpansGenerator<'a> { self.pending_dups.clear(); } } - - fn span_bcb_dominates(&self, dom_covspan: &CoverageSpan, covspan: &CoverageSpan) -> bool { - self.basic_coverage_blocks.dominates(dom_covspan.bcb, covspan.bcb) - } } From 7aa1b8390bc574f50bf7fc62c8ed0f33c9494a0f Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 14 Oct 2023 23:59:11 +1100 Subject: [PATCH 14/14] coverage: Explain why we temporarily steal `pending_dups` --- .../rustc_mir_transform/src/coverage/spans.rs | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index b71a3101bac81..1d1be8f249274 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -353,8 +353,10 @@ impl<'a> CoverageSpansGenerator<'a> { let prev = self.take_prev(); debug!(" AT END, adding last prev={prev:?}"); - let pending_dups = self.pending_dups.split_off(0); - for dup in pending_dups { + + // Take `pending_dups` so that we can drain it while calling self methods. + // It is never used as a field after this point. + for dup in std::mem::take(&mut self.pending_dups) { debug!(" ...adding at least one pending dup={:?}", dup); self.push_refined_span(dup); } @@ -474,11 +476,16 @@ impl<'a> CoverageSpansGenerator<'a> { previous iteration, or prev started a new disjoint span" ); if last_dup.span.hi() <= self.curr().span.lo() { - let pending_dups = self.pending_dups.split_off(0); - for dup in pending_dups.into_iter() { + // Temporarily steal `pending_dups` into a local, so that we can + // drain it while calling other self methods. + let mut pending_dups = std::mem::take(&mut self.pending_dups); + for dup in pending_dups.drain(..) { debug!(" ...adding at least one pending={:?}", dup); self.push_refined_span(dup); } + // The list of dups is now empty, but we can recycle its capacity. + assert!(pending_dups.is_empty() && self.pending_dups.is_empty()); + self.pending_dups = pending_dups; } else { self.pending_dups.clear(); } @@ -526,7 +533,10 @@ impl<'a> CoverageSpansGenerator<'a> { let has_pre_closure_span = prev.span.lo() < right_cutoff; let has_post_closure_span = prev.span.hi() > right_cutoff; - let mut pending_dups = self.pending_dups.split_off(0); + // Temporarily steal `pending_dups` into a local, so that we can + // mutate and/or drain it while calling other self methods. + let mut pending_dups = std::mem::take(&mut self.pending_dups); + if has_pre_closure_span { let mut pre_closure = self.prev().clone(); pre_closure.span = pre_closure.span.with_hi(left_cutoff); @@ -540,6 +550,7 @@ impl<'a> CoverageSpansGenerator<'a> { } self.push_refined_span(pre_closure); } + if has_post_closure_span { // Mutate `prev.span()` to start after the closure (and discard curr). // (**NEVER** update `prev_original_span` because it affects the assumptions @@ -550,12 +561,15 @@ impl<'a> CoverageSpansGenerator<'a> { debug!(" ...and at least one overlapping dup={:?}", dup); dup.span = dup.span.with_lo(right_cutoff); } - self.pending_dups.append(&mut pending_dups); let closure_covspan = self.take_curr(); // Prevent this curr from becoming prev. self.push_refined_span(closure_covspan); // since self.prev() was already updated } else { pending_dups.clear(); } + + // Restore the modified post-closure spans, or the empty vector's capacity. + assert!(self.pending_dups.is_empty()); + self.pending_dups = pending_dups; } /// Called if `curr.span` equals `prev_original_span` (and potentially equal to all