11use std:: collections:: VecDeque ;
2+ use std:: iter;
23
34use rustc_data_structures:: fx:: FxHashSet ;
45use rustc_middle:: mir;
6+ use rustc_middle:: ty:: TyCtxt ;
57use rustc_span:: { DesugaringKind , ExpnKind , MacroKind , Span } ;
68use tracing:: { debug, debug_span, instrument} ;
79
@@ -11,8 +13,9 @@ use crate::coverage::{ExtractedHirInfo, mappings, unexpand};
1113
1214mod from_mir;
1315
14- pub ( super ) fn extract_refined_covspans (
15- mir_body : & mir:: Body < ' _ > ,
16+ pub ( super ) fn extract_refined_covspans < ' tcx > (
17+ tcx : TyCtxt < ' tcx > ,
18+ mir_body : & mir:: Body < ' tcx > ,
1619 hir_info : & ExtractedHirInfo ,
1720 graph : & CoverageGraph ,
1821 code_mappings : & mut impl Extend < mappings:: CodeMapping > ,
@@ -50,7 +53,7 @@ pub(super) fn extract_refined_covspans(
5053 // First, perform the passes that need macro information.
5154 covspans. sort_by ( |a, b| graph. cmp_in_dominator_order ( a. bcb , b. bcb ) ) ;
5255 remove_unwanted_expansion_spans ( & mut covspans) ;
53- split_visible_macro_spans ( & mut covspans) ;
56+ shrink_visible_macro_spans ( tcx , & mut covspans) ;
5457
5558 // We no longer need the extra information in `SpanFromMir`, so convert to `Covspan`.
5659 let mut covspans = covspans. into_iter ( ) . map ( SpanFromMir :: into_covspan) . collect :: < Vec < _ > > ( ) ;
@@ -83,9 +86,7 @@ pub(super) fn extract_refined_covspans(
8386 // Split the covspans into separate buckets that don't overlap any holes.
8487 let buckets = divide_spans_into_buckets ( covspans, & holes) ;
8588
86- for mut covspans in buckets {
87- // Make sure each individual bucket is internally sorted.
88- covspans. sort_by ( compare_covspans) ;
89+ for covspans in buckets {
8990 let _span = debug_span ! ( "processing bucket" , ?covspans) . entered ( ) ;
9091
9192 let mut covspans = remove_unwanted_overlapping_spans ( covspans) ;
@@ -129,82 +130,50 @@ fn remove_unwanted_expansion_spans(covspans: &mut Vec<SpanFromMir>) {
129130}
130131
131132/// When a span corresponds to a macro invocation that is visible from the
132- /// function body, split it into two parts. The first part covers just the
133- /// macro name plus `!`, and the second part covers the rest of the macro
134- /// invocation. This seems to give better results for code that uses macros.
135- fn split_visible_macro_spans ( covspans : & mut Vec < SpanFromMir > ) {
136- let mut extra_spans = vec ! [ ] ;
137-
138- covspans. retain ( |covspan| {
139- let Some ( ExpnKind :: Macro ( MacroKind :: Bang , visible_macro) ) = covspan. expn_kind else {
140- return true ;
141- } ;
142-
143- let split_len = visible_macro. as_str ( ) . len ( ) as u32 + 1 ;
144- let ( before, after) = covspan. span . split_at ( split_len) ;
145- if !covspan. span . contains ( before) || !covspan. span . contains ( after) {
146- // Something is unexpectedly wrong with the split point.
147- // The debug assertion in `split_at` will have already caught this,
148- // but in release builds it's safer to do nothing and maybe get a
149- // bug report for unexpected coverage, rather than risk an ICE.
150- return true ;
133+ /// function body, truncate it to just the macro name plus `!`.
134+ /// This seems to give better results for code that uses macros.
135+ fn shrink_visible_macro_spans ( tcx : TyCtxt < ' _ > , covspans : & mut Vec < SpanFromMir > ) {
136+ let source_map = tcx. sess . source_map ( ) ;
137+
138+ for covspan in covspans {
139+ if matches ! ( covspan. expn_kind, Some ( ExpnKind :: Macro ( MacroKind :: Bang , _) ) ) {
140+ covspan. span = source_map. span_through_char ( covspan. span , '!' ) ;
151141 }
152-
153- extra_spans. push ( SpanFromMir :: new ( before, covspan. expn_kind . clone ( ) , covspan. bcb ) ) ;
154- extra_spans. push ( SpanFromMir :: new ( after, covspan. expn_kind . clone ( ) , covspan. bcb ) ) ;
155- false // Discard the original covspan that we just split.
156- } ) ;
157-
158- // The newly-split spans are added at the end, so any previous sorting
159- // is not preserved.
160- covspans. extend ( extra_spans) ;
142+ }
161143}
162144
163145/// Uses the holes to divide the given covspans into buckets, such that:
164- /// - No span in any hole overlaps a bucket (truncating the spans if necessary).
146+ /// - No span in any hole overlaps a bucket (discarding spans if necessary).
165147/// - The spans in each bucket are strictly after all spans in previous buckets,
166148/// and strictly before all spans in subsequent buckets.
167149///
168- /// The resulting buckets are sorted relative to each other, but might not be
169- /// internally sorted.
150+ /// The lists of covspans and holes must be sorted.
151+ /// The resulting buckets are sorted relative to each other, and each bucket's
152+ /// contents are sorted.
170153#[ instrument( level = "debug" ) ]
171154fn divide_spans_into_buckets ( input_covspans : Vec < Covspan > , holes : & [ Hole ] ) -> Vec < Vec < Covspan > > {
172155 debug_assert ! ( input_covspans. is_sorted_by( |a, b| compare_spans( a. span, b. span) . is_le( ) ) ) ;
173156 debug_assert ! ( holes. is_sorted_by( |a, b| compare_spans( a. span, b. span) . is_le( ) ) ) ;
174157
175- // Now we're ready to start carving holes out of the initial coverage spans,
176- // and grouping them in buckets separated by the holes.
158+ // Now we're ready to start grouping spans into buckets separated by holes.
177159
178160 let mut input_covspans = VecDeque :: from ( input_covspans) ;
179- let mut fragments = vec ! [ ] ;
180161
181162 // For each hole:
182163 // - Identify the spans that are entirely or partly before the hole.
183- // - Put those spans in a corresponding bucket, truncated to the start of the hole.
184- // - If one of those spans also extends after the hole, put the rest of it
185- // in a "fragments" vector that is processed by the next hole.
164+ // - Discard any that overlap with the hole.
165+ // - Add the remaining identified spans to the corresponding bucket.
186166 let mut buckets = ( 0 ..holes. len ( ) ) . map ( |_| vec ! [ ] ) . collect :: < Vec < _ > > ( ) ;
187167 for ( hole, bucket) in holes. iter ( ) . zip ( & mut buckets) {
188- let fragments_from_prev = std:: mem:: take ( & mut fragments) ;
189-
190- // Only inspect spans that precede or overlap this hole,
191- // leaving the rest to be inspected by later holes.
192- // (This relies on the spans and holes both being sorted.)
193- let relevant_input_covspans =
194- drain_front_while ( & mut input_covspans, |c| c. span . lo ( ) < hole. span . hi ( ) ) ;
195-
196- for covspan in fragments_from_prev. into_iter ( ) . chain ( relevant_input_covspans) {
197- let ( before, after) = covspan. split_around_hole_span ( hole. span ) ;
198- bucket. extend ( before) ;
199- fragments. extend ( after) ;
200- }
168+ bucket. extend (
169+ drain_front_while ( & mut input_covspans, |c| c. span . lo ( ) < hole. span . hi ( ) )
170+ . filter ( |c| !c. span . overlaps ( hole. span ) ) ,
171+ ) ;
201172 }
202173
203- // After finding the spans before each hole, any remaining fragments/spans
204- // form their own final bucket, after the final hole.
174+ // Any remaining spans form their own final bucket, after the final hole.
205175 // (If there were no holes, this will just be all of the initial spans.)
206- fragments. extend ( input_covspans) ;
207- buckets. push ( fragments) ;
176+ buckets. push ( Vec :: from ( input_covspans) ) ;
208177
209178 buckets
210179}
@@ -215,7 +184,7 @@ fn drain_front_while<'a, T>(
215184 queue : & ' a mut VecDeque < T > ,
216185 mut pred_fn : impl FnMut ( & T ) -> bool ,
217186) -> impl Iterator < Item = T > {
218- std :: iter:: from_fn ( move || if pred_fn ( queue. front ( ) ? ) { queue . pop_front ( ) } else { None } )
187+ iter:: from_fn ( move || queue. pop_front_if ( |x| pred_fn ( x ) ) )
219188}
220189
221190/// Takes one of the buckets of (sorted) spans extracted from MIR, and "refines"
@@ -258,22 +227,6 @@ struct Covspan {
258227}
259228
260229impl Covspan {
261- /// Splits this covspan into 0-2 parts:
262- /// - The part that is strictly before the hole span, if any.
263- /// - The part that is strictly after the hole span, if any.
264- fn split_around_hole_span ( & self , hole_span : Span ) -> ( Option < Self > , Option < Self > ) {
265- let before = try {
266- let span = self . span . trim_end ( hole_span) ?;
267- Self { span, ..* self }
268- } ;
269- let after = try {
270- let span = self . span . trim_start ( hole_span) ?;
271- Self { span, ..* self }
272- } ;
273-
274- ( before, after)
275- }
276-
277230 /// If `self` and `other` can be merged (i.e. they have the same BCB),
278231 /// mutates `self.span` to also include `other.span` and returns true.
279232 ///
0 commit comments