1
1
use std:: collections:: VecDeque ;
2
+ use std:: iter;
2
3
3
4
use rustc_data_structures:: fx:: FxHashSet ;
4
5
use rustc_middle:: mir;
6
+ use rustc_middle:: ty:: TyCtxt ;
5
7
use rustc_span:: { DesugaringKind , ExpnKind , MacroKind , Span } ;
6
8
use tracing:: { debug, debug_span, instrument} ;
7
9
@@ -11,8 +13,9 @@ use crate::coverage::{ExtractedHirInfo, mappings, unexpand};
11
13
12
14
mod from_mir;
13
15
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 > ,
16
19
hir_info : & ExtractedHirInfo ,
17
20
graph : & CoverageGraph ,
18
21
code_mappings : & mut impl Extend < mappings:: CodeMapping > ,
@@ -50,7 +53,7 @@ pub(super) fn extract_refined_covspans(
50
53
// First, perform the passes that need macro information.
51
54
covspans. sort_by ( |a, b| graph. cmp_in_dominator_order ( a. bcb , b. bcb ) ) ;
52
55
remove_unwanted_expansion_spans ( & mut covspans) ;
53
- split_visible_macro_spans ( & mut covspans) ;
56
+ shrink_visible_macro_spans ( tcx , & mut covspans) ;
54
57
55
58
// We no longer need the extra information in `SpanFromMir`, so convert to `Covspan`.
56
59
let mut covspans = covspans. into_iter ( ) . map ( SpanFromMir :: into_covspan) . collect :: < Vec < _ > > ( ) ;
@@ -83,9 +86,7 @@ pub(super) fn extract_refined_covspans(
83
86
// Split the covspans into separate buckets that don't overlap any holes.
84
87
let buckets = divide_spans_into_buckets ( covspans, & holes) ;
85
88
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 {
89
90
let _span = debug_span ! ( "processing bucket" , ?covspans) . entered ( ) ;
90
91
91
92
let mut covspans = remove_unwanted_overlapping_spans ( covspans) ;
@@ -129,82 +130,50 @@ fn remove_unwanted_expansion_spans(covspans: &mut Vec<SpanFromMir>) {
129
130
}
130
131
131
132
/// 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 , '!' ) ;
151
141
}
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
+ }
161
143
}
162
144
163
145
/// 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).
165
147
/// - The spans in each bucket are strictly after all spans in previous buckets,
166
148
/// and strictly before all spans in subsequent buckets.
167
149
///
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.
170
153
#[ instrument( level = "debug" ) ]
171
154
fn divide_spans_into_buckets ( input_covspans : Vec < Covspan > , holes : & [ Hole ] ) -> Vec < Vec < Covspan > > {
172
155
debug_assert ! ( input_covspans. is_sorted_by( |a, b| compare_spans( a. span, b. span) . is_le( ) ) ) ;
173
156
debug_assert ! ( holes. is_sorted_by( |a, b| compare_spans( a. span, b. span) . is_le( ) ) ) ;
174
157
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.
177
159
178
160
let mut input_covspans = VecDeque :: from ( input_covspans) ;
179
- let mut fragments = vec ! [ ] ;
180
161
181
162
// For each hole:
182
163
// - 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.
186
166
let mut buckets = ( 0 ..holes. len ( ) ) . map ( |_| vec ! [ ] ) . collect :: < Vec < _ > > ( ) ;
187
167
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
+ ) ;
201
172
}
202
173
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.
205
175
// (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) ) ;
208
177
209
178
buckets
210
179
}
@@ -215,7 +184,7 @@ fn drain_front_while<'a, T>(
215
184
queue : & ' a mut VecDeque < T > ,
216
185
mut pred_fn : impl FnMut ( & T ) -> bool ,
217
186
) -> 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 ) ) )
219
188
}
220
189
221
190
/// Takes one of the buckets of (sorted) spans extracted from MIR, and "refines"
@@ -258,22 +227,6 @@ struct Covspan {
258
227
}
259
228
260
229
impl 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
-
277
230
/// If `self` and `other` can be merged (i.e. they have the same BCB),
278
231
/// mutates `self.span` to also include `other.span` and returns true.
279
232
///
0 commit comments