1 use super::graph::{BasicCoverageBlock, BasicCoverageBlockData, CoverageGraph, START_BCB};
2
3 use itertools::Itertools;
4 use rustc_data_structures::graph::WithNumNodes;
5 use rustc_middle::mir::spanview::source_range_no_file;
6 use rustc_middle::mir::{
7 self, AggregateKind, BasicBlock, FakeReadCause, Rvalue, Statement, StatementKind, Terminator,
8 TerminatorKind,
9 };
10 use rustc_middle::ty::TyCtxt;
11 use rustc_span::source_map::original_sp;
12 use rustc_span::{BytePos, ExpnKind, MacroKind, Span, Symbol};
13
14 use std::cell::RefCell;
15 use std::cmp::Ordering;
16
17 #[derive(Debug, Copy, Clone)]
18 pub(super) enum CoverageStatement {
19 Statement(BasicBlock, Span, usize),
20 Terminator(BasicBlock, Span),
21 }
22
23 impl CoverageStatement {
format<'tcx>(&self, tcx: TyCtxt<'tcx>, mir_body: &mir::Body<'tcx>) -> String24 pub fn format<'tcx>(&self, tcx: TyCtxt<'tcx>, mir_body: &mir::Body<'tcx>) -> String {
25 match *self {
26 Self::Statement(bb, span, stmt_index) => {
27 let stmt = &mir_body[bb].statements[stmt_index];
28 format!(
29 "{}: @{}[{}]: {:?}",
30 source_range_no_file(tcx, span),
31 bb.index(),
32 stmt_index,
33 stmt
34 )
35 }
36 Self::Terminator(bb, span) => {
37 let term = mir_body[bb].terminator();
38 format!(
39 "{}: @{}.{}: {:?}",
40 source_range_no_file(tcx, span),
41 bb.index(),
42 term.kind.name(),
43 term.kind
44 )
45 }
46 }
47 }
48
span(&self) -> Span49 pub fn span(&self) -> Span {
50 match self {
51 Self::Statement(_, span, _) | Self::Terminator(_, span) => *span,
52 }
53 }
54 }
55
56 /// A BCB is deconstructed into one or more `Span`s. Each `Span` maps to a `CoverageSpan` that
57 /// references the originating BCB and one or more MIR `Statement`s and/or `Terminator`s.
58 /// Initially, the `Span`s come from the `Statement`s and `Terminator`s, but subsequent
59 /// transforms can combine adjacent `Span`s and `CoverageSpan` from the same BCB, merging the
60 /// `CoverageStatement` vectors, and the `Span`s to cover the extent of the combined `Span`s.
61 ///
62 /// Note: A `CoverageStatement` merged into another CoverageSpan may come from a `BasicBlock` that
63 /// is not part of the `CoverageSpan` bcb if the statement was included because it's `Span` matches
64 /// or is subsumed by the `Span` associated with this `CoverageSpan`, and it's `BasicBlock`
65 /// `dominates()` the `BasicBlock`s in this `CoverageSpan`.
66 #[derive(Debug, Clone)]
67 pub(super) struct CoverageSpan {
68 pub span: Span,
69 pub expn_span: Span,
70 pub current_macro_or_none: RefCell<Option<Option<Symbol>>>,
71 pub bcb: BasicCoverageBlock,
72 pub coverage_statements: Vec<CoverageStatement>,
73 pub is_closure: bool,
74 }
75
76 impl CoverageSpan {
for_fn_sig(fn_sig_span: Span) -> Self77 pub fn for_fn_sig(fn_sig_span: Span) -> Self {
78 Self {
79 span: fn_sig_span,
80 expn_span: fn_sig_span,
81 current_macro_or_none: Default::default(),
82 bcb: START_BCB,
83 coverage_statements: vec![],
84 is_closure: false,
85 }
86 }
87
for_statement( statement: &Statement<'_>, span: Span, expn_span: Span, bcb: BasicCoverageBlock, bb: BasicBlock, stmt_index: usize, ) -> Self88 pub fn for_statement(
89 statement: &Statement<'_>,
90 span: Span,
91 expn_span: Span,
92 bcb: BasicCoverageBlock,
93 bb: BasicBlock,
94 stmt_index: usize,
95 ) -> Self {
96 let is_closure = match statement.kind {
97 StatementKind::Assign(box (_, Rvalue::Aggregate(box ref kind, _))) => {
98 matches!(kind, AggregateKind::Closure(_, _) | AggregateKind::Generator(_, _, _))
99 }
100 _ => false,
101 };
102
103 Self {
104 span,
105 expn_span,
106 current_macro_or_none: Default::default(),
107 bcb,
108 coverage_statements: vec![CoverageStatement::Statement(bb, span, stmt_index)],
109 is_closure,
110 }
111 }
112
for_terminator( span: Span, expn_span: Span, bcb: BasicCoverageBlock, bb: BasicBlock, ) -> Self113 pub fn for_terminator(
114 span: Span,
115 expn_span: Span,
116 bcb: BasicCoverageBlock,
117 bb: BasicBlock,
118 ) -> Self {
119 Self {
120 span,
121 expn_span,
122 current_macro_or_none: Default::default(),
123 bcb,
124 coverage_statements: vec![CoverageStatement::Terminator(bb, span)],
125 is_closure: false,
126 }
127 }
128
merge_from(&mut self, mut other: CoverageSpan)129 pub fn merge_from(&mut self, mut other: CoverageSpan) {
130 debug_assert!(self.is_mergeable(&other));
131 self.span = self.span.to(other.span);
132 self.coverage_statements.append(&mut other.coverage_statements);
133 }
134
cutoff_statements_at(&mut self, cutoff_pos: BytePos)135 pub fn cutoff_statements_at(&mut self, cutoff_pos: BytePos) {
136 self.coverage_statements.retain(|covstmt| covstmt.span().hi() <= cutoff_pos);
137 if let Some(highest_covstmt) =
138 self.coverage_statements.iter().max_by_key(|covstmt| covstmt.span().hi())
139 {
140 self.span = self.span.with_hi(highest_covstmt.span().hi());
141 }
142 }
143
144 #[inline]
is_mergeable(&self, other: &Self) -> bool145 pub fn is_mergeable(&self, other: &Self) -> bool {
146 self.is_in_same_bcb(other) && !(self.is_closure || other.is_closure)
147 }
148
149 #[inline]
is_in_same_bcb(&self, other: &Self) -> bool150 pub fn is_in_same_bcb(&self, other: &Self) -> bool {
151 self.bcb == other.bcb
152 }
153
format<'tcx>(&self, tcx: TyCtxt<'tcx>, mir_body: &mir::Body<'tcx>) -> String154 pub fn format<'tcx>(&self, tcx: TyCtxt<'tcx>, mir_body: &mir::Body<'tcx>) -> String {
155 format!(
156 "{}\n {}",
157 source_range_no_file(tcx, self.span),
158 self.format_coverage_statements(tcx, mir_body).replace('\n', "\n "),
159 )
160 }
161
format_coverage_statements<'tcx>( &self, tcx: TyCtxt<'tcx>, mir_body: &mir::Body<'tcx>, ) -> String162 pub fn format_coverage_statements<'tcx>(
163 &self,
164 tcx: TyCtxt<'tcx>,
165 mir_body: &mir::Body<'tcx>,
166 ) -> String {
167 let mut sorted_coverage_statements = self.coverage_statements.clone();
168 sorted_coverage_statements.sort_unstable_by_key(|covstmt| match *covstmt {
169 CoverageStatement::Statement(bb, _, index) => (bb, index),
170 CoverageStatement::Terminator(bb, _) => (bb, usize::MAX),
171 });
172 sorted_coverage_statements.iter().map(|covstmt| covstmt.format(tcx, mir_body)).join("\n")
173 }
174
175 /// If the span is part of a macro, returns the macro name symbol.
current_macro(&self) -> Option<Symbol>176 pub fn current_macro(&self) -> Option<Symbol> {
177 self.current_macro_or_none
178 .borrow_mut()
179 .get_or_insert_with(|| {
180 if let ExpnKind::Macro(MacroKind::Bang, current_macro) =
181 self.expn_span.ctxt().outer_expn_data().kind
182 {
183 return Some(current_macro);
184 }
185 None
186 })
187 .map(|symbol| symbol)
188 }
189
190 /// If the span is part of a macro, and the macro is visible (expands directly to the given
191 /// body_span), returns the macro name symbol.
visible_macro(&self, body_span: Span) -> Option<Symbol>192 pub fn visible_macro(&self, body_span: Span) -> Option<Symbol> {
193 if let Some(current_macro) = self.current_macro() && self
194 .expn_span
195 .parent_callsite()
196 .unwrap_or_else(|| bug!("macro must have a parent"))
197 .eq_ctxt(body_span)
198 {
199 return Some(current_macro);
200 }
201 None
202 }
203
is_macro_expansion(&self) -> bool204 pub fn is_macro_expansion(&self) -> bool {
205 self.current_macro().is_some()
206 }
207 }
208
209 /// Converts the initial set of `CoverageSpan`s (one per MIR `Statement` or `Terminator`) into a
210 /// minimal set of `CoverageSpan`s, using the BCB CFG to determine where it is safe and useful to:
211 ///
212 /// * Remove duplicate source code coverage regions
213 /// * Merge spans that represent continuous (both in source code and control flow), non-branching
214 /// execution
215 /// * Carve out (leave uncovered) any span that will be counted by another MIR (notably, closures)
216 pub struct CoverageSpans<'a, 'tcx> {
217 /// The MIR, used to look up `BasicBlockData`.
218 mir_body: &'a mir::Body<'tcx>,
219
220 /// A `Span` covering the signature of function for the MIR.
221 fn_sig_span: Span,
222
223 /// A `Span` covering the function body of the MIR (typically from left curly brace to right
224 /// curly brace).
225 body_span: Span,
226
227 /// The BasicCoverageBlock Control Flow Graph (BCB CFG).
228 basic_coverage_blocks: &'a CoverageGraph,
229
230 /// The initial set of `CoverageSpan`s, sorted by `Span` (`lo` and `hi`) and by relative
231 /// dominance between the `BasicCoverageBlock`s of equal `Span`s.
232 sorted_spans_iter: Option<std::vec::IntoIter<CoverageSpan>>,
233
234 /// The current `CoverageSpan` to compare to its `prev`, to possibly merge, discard, force the
235 /// discard of the `prev` (and or `pending_dups`), or keep both (with `prev` moved to
236 /// `pending_dups`). If `curr` is not discarded or merged, it becomes `prev` for the next
237 /// iteration.
238 some_curr: Option<CoverageSpan>,
239
240 /// The original `span` for `curr`, in case `curr.span()` is modified. The `curr_original_span`
241 /// **must not be mutated** (except when advancing to the next `curr`), even if `curr.span()`
242 /// is mutated.
243 curr_original_span: Span,
244
245 /// The CoverageSpan from a prior iteration; typically assigned from that iteration's `curr`.
246 /// If that `curr` was discarded, `prev` retains its value from the previous iteration.
247 some_prev: Option<CoverageSpan>,
248
249 /// Assigned from `curr_original_span` from the previous iteration. The `prev_original_span`
250 /// **must not be mutated** (except when advancing to the next `prev`), even if `prev.span()`
251 /// is mutated.
252 prev_original_span: Span,
253
254 /// A copy of the expn_span from the prior iteration.
255 prev_expn_span: Option<Span>,
256
257 /// One or more `CoverageSpan`s with the same `Span` but different `BasicCoverageBlock`s, and
258 /// no `BasicCoverageBlock` in this list dominates another `BasicCoverageBlock` in the list.
259 /// If a new `curr` span also fits this criteria (compared to an existing list of
260 /// `pending_dups`), that `curr` `CoverageSpan` moves to `prev` before possibly being added to
261 /// the `pending_dups` list, on the next iteration. As a result, if `prev` and `pending_dups`
262 /// have the same `Span`, the criteria for `pending_dups` holds for `prev` as well: a `prev`
263 /// with a matching `Span` does not dominate any `pending_dup` and no `pending_dup` dominates a
264 /// `prev` with a matching `Span`)
265 pending_dups: Vec<CoverageSpan>,
266
267 /// The final `CoverageSpan`s to add to the coverage map. A `Counter` or `Expression`
268 /// will also be injected into the MIR for each `CoverageSpan`.
269 refined_spans: Vec<CoverageSpan>,
270 }
271
272 impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
273 /// Generate a minimal set of `CoverageSpan`s, each representing a contiguous code region to be
274 /// counted.
275 ///
276 /// The basic steps are:
277 ///
278 /// 1. Extract an initial set of spans from the `Statement`s and `Terminator`s of each
279 /// `BasicCoverageBlockData`.
280 /// 2. Sort the spans by span.lo() (starting position). Spans that start at the same position
281 /// are sorted with longer spans before shorter spans; and equal spans are sorted
282 /// (deterministically) based on "dominator" relationship (if any).
283 /// 3. Traverse the spans in sorted order to identify spans that can be dropped (for instance,
284 /// if another span or spans are already counting the same code region), or should be merged
285 /// into a broader combined span (because it represents a contiguous, non-branching, and
286 /// uninterrupted region of source code).
287 ///
288 /// Closures are exposed in their enclosing functions as `Assign` `Rvalue`s, and since
289 /// closures have their own MIR, their `Span` in their enclosing function should be left
290 /// "uncovered".
291 ///
292 /// Note the resulting vector of `CoverageSpan`s may not be fully sorted (and does not need
293 /// to be).
generate_coverage_spans( mir_body: &'a mir::Body<'tcx>, fn_sig_span: Span, body_span: Span, basic_coverage_blocks: &'a CoverageGraph, ) -> Vec<CoverageSpan>294 pub(super) fn generate_coverage_spans(
295 mir_body: &'a mir::Body<'tcx>,
296 fn_sig_span: Span, // Ensured to be same SourceFile and SyntaxContext as `body_span`
297 body_span: Span,
298 basic_coverage_blocks: &'a CoverageGraph,
299 ) -> Vec<CoverageSpan> {
300 let mut coverage_spans = CoverageSpans {
301 mir_body,
302 fn_sig_span,
303 body_span,
304 basic_coverage_blocks,
305 sorted_spans_iter: None,
306 refined_spans: Vec::with_capacity(basic_coverage_blocks.num_nodes() * 2),
307 some_curr: None,
308 curr_original_span: Span::with_root_ctxt(BytePos(0), BytePos(0)),
309 some_prev: None,
310 prev_original_span: Span::with_root_ctxt(BytePos(0), BytePos(0)),
311 prev_expn_span: None,
312 pending_dups: Vec::new(),
313 };
314
315 let sorted_spans = coverage_spans.mir_to_initial_sorted_coverage_spans();
316
317 coverage_spans.sorted_spans_iter = Some(sorted_spans.into_iter());
318
319 coverage_spans.to_refined_spans()
320 }
321
mir_to_initial_sorted_coverage_spans(&self) -> Vec<CoverageSpan>322 fn mir_to_initial_sorted_coverage_spans(&self) -> Vec<CoverageSpan> {
323 let mut initial_spans =
324 Vec::<CoverageSpan>::with_capacity(self.mir_body.basic_blocks.len() * 2);
325 for (bcb, bcb_data) in self.basic_coverage_blocks.iter_enumerated() {
326 initial_spans.extend(self.bcb_to_initial_coverage_spans(bcb, bcb_data));
327 }
328
329 if initial_spans.is_empty() {
330 // This can happen if, for example, the function is unreachable (contains only a
331 // `BasicBlock`(s) with an `Unreachable` terminator).
332 return initial_spans;
333 }
334
335 initial_spans.push(CoverageSpan::for_fn_sig(self.fn_sig_span));
336
337 initial_spans.sort_unstable_by(|a, b| {
338 if a.span.lo() == b.span.lo() {
339 if a.span.hi() == b.span.hi() {
340 if a.is_in_same_bcb(b) {
341 Some(Ordering::Equal)
342 } else {
343 // Sort equal spans by dominator relationship (so dominators always come
344 // before the dominated equal spans). When later comparing two spans in
345 // order, the first will either dominate the second, or they will have no
346 // dominator relationship.
347 self.basic_coverage_blocks.rank_partial_cmp(a.bcb, b.bcb)
348 }
349 } else {
350 // Sort hi() in reverse order so shorter spans are attempted after longer spans.
351 // This guarantees that, if a `prev` span overlaps, and is not equal to, a
352 // `curr` span, the prev span either extends further left of the curr span, or
353 // they start at the same position and the prev span extends further right of
354 // the end of the curr span.
355 b.span.hi().partial_cmp(&a.span.hi())
356 }
357 } else {
358 a.span.lo().partial_cmp(&b.span.lo())
359 }
360 .unwrap()
361 });
362
363 initial_spans
364 }
365
366 /// Iterate through the sorted `CoverageSpan`s, and return the refined list of merged and
367 /// de-duplicated `CoverageSpan`s.
to_refined_spans(mut self) -> Vec<CoverageSpan>368 fn to_refined_spans(mut self) -> Vec<CoverageSpan> {
369 while self.next_coverage_span() {
370 if self.some_prev.is_none() {
371 debug!(" initial span");
372 self.check_invoked_macro_name_span();
373 } else if self.curr().is_mergeable(self.prev()) {
374 debug!(" same bcb (and neither is a closure), merge with prev={:?}", self.prev());
375 let prev = self.take_prev();
376 self.curr_mut().merge_from(prev);
377 self.check_invoked_macro_name_span();
378 // Note that curr.span may now differ from curr_original_span
379 } else if self.prev_ends_before_curr() {
380 debug!(
381 " different bcbs and disjoint spans, so keep curr for next iter, and add \
382 prev={:?}",
383 self.prev()
384 );
385 let prev = self.take_prev();
386 self.push_refined_span(prev);
387 self.check_invoked_macro_name_span();
388 } else if self.prev().is_closure {
389 // drop any equal or overlapping span (`curr`) and keep `prev` to test again in the
390 // next iter
391 debug!(
392 " curr overlaps a closure (prev). Drop curr and keep prev for next iter. \
393 prev={:?}",
394 self.prev()
395 );
396 self.take_curr();
397 } else if self.curr().is_closure {
398 self.carve_out_span_for_closure();
399 } else if self.prev_original_span == self.curr().span {
400 // Note that this compares the new (`curr`) span to `prev_original_span`.
401 // In this branch, the actual span byte range of `prev_original_span` is not
402 // important. What is important is knowing whether the new `curr` span was
403 // **originally** the same as the original span of `prev()`. The original spans
404 // reflect their original sort order, and for equal spans, conveys a partial
405 // ordering based on CFG dominator priority.
406 if self.prev().is_macro_expansion() && self.curr().is_macro_expansion() {
407 // Macros that expand to include branching (such as
408 // `assert_eq!()`, `assert_ne!()`, `info!()`, `debug!()`, or
409 // `trace!()`) typically generate callee spans with identical
410 // ranges (typically the full span of the macro) for all
411 // `BasicBlocks`. This makes it impossible to distinguish
412 // the condition (`if val1 != val2`) from the optional
413 // branched statements (such as the call to `panic!()` on
414 // assert failure). In this case it is better (or less
415 // worse) to drop the optional branch bcbs and keep the
416 // non-conditional statements, to count when reached.
417 debug!(
418 " curr and prev are part of a macro expansion, and curr has the same span \
419 as prev, but is in a different bcb. Drop curr and keep prev for next iter. \
420 prev={:?}",
421 self.prev()
422 );
423 self.take_curr();
424 } else {
425 self.hold_pending_dups_unless_dominated();
426 }
427 } else {
428 self.cutoff_prev_at_overlapping_curr();
429 self.check_invoked_macro_name_span();
430 }
431 }
432
433 debug!(" AT END, adding last prev={:?}", self.prev());
434 let prev = self.take_prev();
435 let pending_dups = self.pending_dups.split_off(0);
436 for dup in pending_dups {
437 debug!(" ...adding at least one pending dup={:?}", dup);
438 self.push_refined_span(dup);
439 }
440
441 // Async functions wrap a closure that implements the body to be executed. The enclosing
442 // function is called and returns an `impl Future` without initially executing any of the
443 // body. To avoid showing the return from the enclosing function as a "covered" return from
444 // the closure, the enclosing function's `TerminatorKind::Return`s `CoverageSpan` is
445 // excluded. The closure's `Return` is the only one that will be counted. This provides
446 // adequate coverage, and more intuitive counts. (Avoids double-counting the closing brace
447 // of the function body.)
448 let body_ends_with_closure = if let Some(last_covspan) = self.refined_spans.last() {
449 last_covspan.is_closure && last_covspan.span.hi() == self.body_span.hi()
450 } else {
451 false
452 };
453
454 if !body_ends_with_closure {
455 self.push_refined_span(prev);
456 }
457
458 // Remove `CoverageSpan`s derived from closures, originally added to ensure the coverage
459 // regions for the current function leave room for the closure's own coverage regions
460 // (injected separately, from the closure's own MIR).
461 self.refined_spans.retain(|covspan| !covspan.is_closure);
462 self.refined_spans
463 }
464
push_refined_span(&mut self, covspan: CoverageSpan)465 fn push_refined_span(&mut self, covspan: CoverageSpan) {
466 let len = self.refined_spans.len();
467 if len > 0 {
468 let last = &mut self.refined_spans[len - 1];
469 if last.is_mergeable(&covspan) {
470 debug!(
471 "merging new refined span with last refined span, last={:?}, covspan={:?}",
472 last, covspan
473 );
474 last.merge_from(covspan);
475 return;
476 }
477 }
478 self.refined_spans.push(covspan)
479 }
480
check_invoked_macro_name_span(&mut self)481 fn check_invoked_macro_name_span(&mut self) {
482 if let Some(visible_macro) = self.curr().visible_macro(self.body_span) {
483 if !self
484 .prev_expn_span
485 .is_some_and(|prev_expn_span| self.curr().expn_span.ctxt() == prev_expn_span.ctxt())
486 {
487 let merged_prefix_len = self.curr_original_span.lo() - self.curr().span.lo();
488 let after_macro_bang =
489 merged_prefix_len + BytePos(visible_macro.as_str().len() as u32 + 1);
490 let mut macro_name_cov = self.curr().clone();
491 self.curr_mut().span =
492 self.curr().span.with_lo(self.curr().span.lo() + after_macro_bang);
493 macro_name_cov.span =
494 macro_name_cov.span.with_hi(macro_name_cov.span.lo() + after_macro_bang);
495 debug!(
496 " and curr starts a new macro expansion, so add a new span just for \
497 the macro `{}!`, new span={:?}",
498 visible_macro, macro_name_cov
499 );
500 self.push_refined_span(macro_name_cov);
501 }
502 }
503 }
504
505 // Generate a set of `CoverageSpan`s from the filtered set of `Statement`s and `Terminator`s of
506 // the `BasicBlock`(s) in the given `BasicCoverageBlockData`. One `CoverageSpan` is generated
507 // for each `Statement` and `Terminator`. (Note that subsequent stages of coverage analysis will
508 // merge some `CoverageSpan`s, at which point a `CoverageSpan` may represent multiple
509 // `Statement`s and/or `Terminator`s.)
bcb_to_initial_coverage_spans( &self, bcb: BasicCoverageBlock, bcb_data: &'a BasicCoverageBlockData, ) -> Vec<CoverageSpan>510 fn bcb_to_initial_coverage_spans(
511 &self,
512 bcb: BasicCoverageBlock,
513 bcb_data: &'a BasicCoverageBlockData,
514 ) -> Vec<CoverageSpan> {
515 bcb_data
516 .basic_blocks
517 .iter()
518 .flat_map(|&bb| {
519 let data = &self.mir_body[bb];
520 data.statements
521 .iter()
522 .enumerate()
523 .filter_map(move |(index, statement)| {
524 filtered_statement_span(statement).map(|span| {
525 CoverageSpan::for_statement(
526 statement,
527 function_source_span(span, self.body_span),
528 span,
529 bcb,
530 bb,
531 index,
532 )
533 })
534 })
535 .chain(filtered_terminator_span(data.terminator()).map(|span| {
536 CoverageSpan::for_terminator(
537 function_source_span(span, self.body_span),
538 span,
539 bcb,
540 bb,
541 )
542 }))
543 })
544 .collect()
545 }
546
curr(&self) -> &CoverageSpan547 fn curr(&self) -> &CoverageSpan {
548 self.some_curr
549 .as_ref()
550 .unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_curr"))
551 }
552
curr_mut(&mut self) -> &mut CoverageSpan553 fn curr_mut(&mut self) -> &mut CoverageSpan {
554 self.some_curr
555 .as_mut()
556 .unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_curr"))
557 }
558
prev(&self) -> &CoverageSpan559 fn prev(&self) -> &CoverageSpan {
560 self.some_prev
561 .as_ref()
562 .unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_prev"))
563 }
564
prev_mut(&mut self) -> &mut CoverageSpan565 fn prev_mut(&mut self) -> &mut CoverageSpan {
566 self.some_prev
567 .as_mut()
568 .unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_prev"))
569 }
570
take_prev(&mut self) -> CoverageSpan571 fn take_prev(&mut self) -> CoverageSpan {
572 self.some_prev.take().unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_prev"))
573 }
574
575 /// If there are `pending_dups` but `prev` is not a matching dup (`prev.span` doesn't match the
576 /// `pending_dups` spans), then one of the following two things happened during the previous
577 /// iteration:
578 /// * the previous `curr` span (which is now `prev`) was not a duplicate of the pending_dups
579 /// (in which case there should be at least two spans in `pending_dups`); or
580 /// * the `span` of `prev` was modified by `curr_mut().merge_from(prev)` (in which case
581 /// `pending_dups` could have as few as one span)
582 /// In either case, no more spans will match the span of `pending_dups`, so
583 /// add the `pending_dups` if they don't overlap `curr`, and clear the list.
check_pending_dups(&mut self)584 fn check_pending_dups(&mut self) {
585 if let Some(dup) = self.pending_dups.last() && dup.span != self.prev().span {
586 debug!(
587 " SAME spans, but pending_dups are NOT THE SAME, so BCBs matched on \
588 previous iteration, or prev started a new disjoint span"
589 );
590 if dup.span.hi() <= self.curr().span.lo() {
591 let pending_dups = self.pending_dups.split_off(0);
592 for dup in pending_dups.into_iter() {
593 debug!(" ...adding at least one pending={:?}", dup);
594 self.push_refined_span(dup);
595 }
596 } else {
597 self.pending_dups.clear();
598 }
599 }
600 }
601
602 /// Advance `prev` to `curr` (if any), and `curr` to the next `CoverageSpan` in sorted order.
next_coverage_span(&mut self) -> bool603 fn next_coverage_span(&mut self) -> bool {
604 if let Some(curr) = self.some_curr.take() {
605 self.prev_expn_span = Some(curr.expn_span);
606 self.some_prev = Some(curr);
607 self.prev_original_span = self.curr_original_span;
608 }
609 while let Some(curr) = self.sorted_spans_iter.as_mut().unwrap().next() {
610 debug!("FOR curr={:?}", curr);
611 if self.some_prev.is_some() && self.prev_starts_after_next(&curr) {
612 debug!(
613 " prev.span starts after curr.span, so curr will be dropped (skipping past \
614 closure?); prev={:?}",
615 self.prev()
616 );
617 } else {
618 // Save a copy of the original span for `curr` in case the `CoverageSpan` is changed
619 // by `self.curr_mut().merge_from(prev)`.
620 self.curr_original_span = curr.span;
621 self.some_curr.replace(curr);
622 self.check_pending_dups();
623 return true;
624 }
625 }
626 false
627 }
628
629 /// If called, then the next call to `next_coverage_span()` will *not* update `prev` with the
630 /// `curr` coverage span.
take_curr(&mut self) -> CoverageSpan631 fn take_curr(&mut self) -> CoverageSpan {
632 self.some_curr.take().unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_curr"))
633 }
634
635 /// Returns true if the curr span should be skipped because prev has already advanced beyond the
636 /// end of curr. This can only happen if a prior iteration updated `prev` to skip past a region
637 /// of code, such as skipping past a closure.
prev_starts_after_next(&self, next_curr: &CoverageSpan) -> bool638 fn prev_starts_after_next(&self, next_curr: &CoverageSpan) -> bool {
639 self.prev().span.lo() > next_curr.span.lo()
640 }
641
642 /// Returns true if the curr span starts past the end of the prev span, which means they don't
643 /// overlap, so we now know the prev can be added to the refined coverage spans.
prev_ends_before_curr(&self) -> bool644 fn prev_ends_before_curr(&self) -> bool {
645 self.prev().span.hi() <= self.curr().span.lo()
646 }
647
648 /// If `prev`s span extends left of the closure (`curr`), carve out the closure's span from
649 /// `prev`'s span. (The closure's coverage counters will be injected when processing the
650 /// closure's own MIR.) Add the portion of the span to the left of the closure; and if the span
651 /// extends to the right of the closure, update `prev` to that portion of the span. For any
652 /// `pending_dups`, repeat the same process.
carve_out_span_for_closure(&mut self)653 fn carve_out_span_for_closure(&mut self) {
654 let curr_span = self.curr().span;
655 let left_cutoff = curr_span.lo();
656 let right_cutoff = curr_span.hi();
657 let has_pre_closure_span = self.prev().span.lo() < right_cutoff;
658 let has_post_closure_span = self.prev().span.hi() > right_cutoff;
659 let mut pending_dups = self.pending_dups.split_off(0);
660 if has_pre_closure_span {
661 let mut pre_closure = self.prev().clone();
662 pre_closure.span = pre_closure.span.with_hi(left_cutoff);
663 debug!(" prev overlaps a closure. Adding span for pre_closure={:?}", pre_closure);
664 if !pending_dups.is_empty() {
665 for mut dup in pending_dups.iter().cloned() {
666 dup.span = dup.span.with_hi(left_cutoff);
667 debug!(" ...and at least one pre_closure dup={:?}", dup);
668 self.push_refined_span(dup);
669 }
670 }
671 self.push_refined_span(pre_closure);
672 }
673 if has_post_closure_span {
674 // Mutate `prev.span()` to start after the closure (and discard curr).
675 // (**NEVER** update `prev_original_span` because it affects the assumptions
676 // about how the `CoverageSpan`s are ordered.)
677 self.prev_mut().span = self.prev().span.with_lo(right_cutoff);
678 debug!(" Mutated prev.span to start after the closure. prev={:?}", self.prev());
679 for dup in pending_dups.iter_mut() {
680 debug!(" ...and at least one overlapping dup={:?}", dup);
681 dup.span = dup.span.with_lo(right_cutoff);
682 }
683 self.pending_dups.append(&mut pending_dups);
684 let closure_covspan = self.take_curr();
685 self.push_refined_span(closure_covspan); // since self.prev() was already updated
686 } else {
687 pending_dups.clear();
688 }
689 }
690
691 /// Called if `curr.span` equals `prev_original_span` (and potentially equal to all
692 /// `pending_dups` spans, if any). Keep in mind, `prev.span()` may have been changed.
693 /// If prev.span() was merged into other spans (with matching BCB, for instance),
694 /// `prev.span.hi()` will be greater than (further right of) `prev_original_span.hi()`.
695 /// If prev.span() was split off to the right of a closure, prev.span().lo() will be
696 /// greater than prev_original_span.lo(). The actual span of `prev_original_span` is
697 /// not as important as knowing that `prev()` **used to have the same span** as `curr()`,
698 /// which means their sort order is still meaningful for determining the dominator
699 /// relationship.
700 ///
701 /// When two `CoverageSpan`s have the same `Span`, dominated spans can be discarded; but if
702 /// neither `CoverageSpan` dominates the other, both (or possibly more than two) are held,
703 /// until their disposition is determined. In this latter case, the `prev` dup is moved into
704 /// `pending_dups` so the new `curr` dup can be moved to `prev` for the next iteration.
hold_pending_dups_unless_dominated(&mut self)705 fn hold_pending_dups_unless_dominated(&mut self) {
706 // Equal coverage spans are ordered by dominators before dominated (if any), so it should be
707 // impossible for `curr` to dominate any previous `CoverageSpan`.
708 debug_assert!(!self.span_bcb_dominates(self.curr(), self.prev()));
709
710 let initial_pending_count = self.pending_dups.len();
711 if initial_pending_count > 0 {
712 let mut pending_dups = self.pending_dups.split_off(0);
713 pending_dups.retain(|dup| !self.span_bcb_dominates(dup, self.curr()));
714 self.pending_dups.append(&mut pending_dups);
715 if self.pending_dups.len() < initial_pending_count {
716 debug!(
717 " discarded {} of {} pending_dups that dominated curr",
718 initial_pending_count - self.pending_dups.len(),
719 initial_pending_count
720 );
721 }
722 }
723
724 if self.span_bcb_dominates(self.prev(), self.curr()) {
725 debug!(
726 " different bcbs but SAME spans, and prev dominates curr. Discard prev={:?}",
727 self.prev()
728 );
729 self.cutoff_prev_at_overlapping_curr();
730 // If one span dominates the other, associate the span with the code from the dominated
731 // block only (`curr`), and discard the overlapping portion of the `prev` span. (Note
732 // that if `prev.span` is wider than `prev_original_span`, a `CoverageSpan` will still
733 // be created for `prev`s block, for the non-overlapping portion, left of `curr.span`.)
734 //
735 // For example:
736 // match somenum {
737 // x if x < 1 => { ... }
738 // }...
739 //
740 // The span for the first `x` is referenced by both the pattern block (every time it is
741 // evaluated) and the arm code (only when matched). The counter will be applied only to
742 // the dominated block. This allows coverage to track and highlight things like the
743 // assignment of `x` above, if the branch is matched, making `x` available to the arm
744 // code; and to track and highlight the question mark `?` "try" operator at the end of
745 // a function call returning a `Result`, so the `?` is covered when the function returns
746 // an `Err`, and not counted as covered if the function always returns `Ok`.
747 } else {
748 // Save `prev` in `pending_dups`. (`curr` will become `prev` in the next iteration.)
749 // If the `curr` CoverageSpan is later discarded, `pending_dups` can be discarded as
750 // well; but if `curr` is added to refined_spans, the `pending_dups` will also be added.
751 debug!(
752 " different bcbs but SAME spans, and neither dominates, so keep curr for \
753 next iter, and, pending upcoming spans (unless overlapping) add prev={:?}",
754 self.prev()
755 );
756 let prev = self.take_prev();
757 self.pending_dups.push(prev);
758 }
759 }
760
761 /// `curr` overlaps `prev`. If `prev`s span extends left of `curr`s span, keep _only_
762 /// statements that end before `curr.lo()` (if any), and add the portion of the
763 /// combined span for those statements. Any other statements have overlapping spans
764 /// that can be ignored because `curr` and/or other upcoming statements/spans inside
765 /// the overlap area will produce their own counters. This disambiguation process
766 /// avoids injecting multiple counters for overlapping spans, and the potential for
767 /// double-counting.
cutoff_prev_at_overlapping_curr(&mut self)768 fn cutoff_prev_at_overlapping_curr(&mut self) {
769 debug!(
770 " different bcbs, overlapping spans, so ignore/drop pending and only add prev \
771 if it has statements that end before curr; prev={:?}",
772 self.prev()
773 );
774 if self.pending_dups.is_empty() {
775 let curr_span = self.curr().span;
776 self.prev_mut().cutoff_statements_at(curr_span.lo());
777 if self.prev().coverage_statements.is_empty() {
778 debug!(" ... no non-overlapping statements to add");
779 } else {
780 debug!(" ... adding modified prev={:?}", self.prev());
781 let prev = self.take_prev();
782 self.push_refined_span(prev);
783 }
784 } else {
785 // with `pending_dups`, `prev` cannot have any statements that don't overlap
786 self.pending_dups.clear();
787 }
788 }
789
span_bcb_dominates(&self, dom_covspan: &CoverageSpan, covspan: &CoverageSpan) -> bool790 fn span_bcb_dominates(&self, dom_covspan: &CoverageSpan, covspan: &CoverageSpan) -> bool {
791 self.basic_coverage_blocks.dominates(dom_covspan.bcb, covspan.bcb)
792 }
793 }
794
795 /// If the MIR `Statement` has a span contributive to computing coverage spans,
796 /// return it; otherwise return `None`.
filtered_statement_span(statement: &Statement<'_>) -> Option<Span>797 pub(super) fn filtered_statement_span(statement: &Statement<'_>) -> Option<Span> {
798 match statement.kind {
799 // These statements have spans that are often outside the scope of the executed source code
800 // for their parent `BasicBlock`.
801 StatementKind::StorageLive(_)
802 | StatementKind::StorageDead(_)
803 // Coverage should not be encountered, but don't inject coverage coverage
804 | StatementKind::Coverage(_)
805 // Ignore `ConstEvalCounter`s
806 | StatementKind::ConstEvalCounter
807 // Ignore `Nop`s
808 | StatementKind::Nop => None,
809
810 // FIXME(#78546): MIR InstrumentCoverage - Can the source_info.span for `FakeRead`
811 // statements be more consistent?
812 //
813 // FakeReadCause::ForGuardBinding, in this example:
814 // match somenum {
815 // x if x < 1 => { ... }
816 // }...
817 // The BasicBlock within the match arm code included one of these statements, but the span
818 // for it covered the `1` in this source. The actual statements have nothing to do with that
819 // source span:
820 // FakeRead(ForGuardBinding, _4);
821 // where `_4` is:
822 // _4 = &_1; (at the span for the first `x`)
823 // and `_1` is the `Place` for `somenum`.
824 //
825 // If and when the Issue is resolved, remove this special case match pattern:
826 StatementKind::FakeRead(box (cause, _)) if cause == FakeReadCause::ForGuardBinding => None,
827
828 // Retain spans from all other statements
829 StatementKind::FakeRead(box (_, _)) // Not including `ForGuardBinding`
830 | StatementKind::Intrinsic(..)
831 | StatementKind::Assign(_)
832 | StatementKind::SetDiscriminant { .. }
833 | StatementKind::Deinit(..)
834 | StatementKind::Retag(_, _)
835 | StatementKind::PlaceMention(..)
836 | StatementKind::AscribeUserType(_, _) => {
837 Some(statement.source_info.span)
838 }
839 }
840 }
841
842 /// If the MIR `Terminator` has a span contributive to computing coverage spans,
843 /// return it; otherwise return `None`.
filtered_terminator_span(terminator: &Terminator<'_>) -> Option<Span>844 pub(super) fn filtered_terminator_span(terminator: &Terminator<'_>) -> Option<Span> {
845 match terminator.kind {
846 // These terminators have spans that don't positively contribute to computing a reasonable
847 // span of actually executed source code. (For example, SwitchInt terminators extracted from
848 // an `if condition { block }` has a span that includes the executed block, if true,
849 // but for coverage, the code region executed, up to *and* through the SwitchInt,
850 // actually stops before the if's block.)
851 TerminatorKind::Unreachable // Unreachable blocks are not connected to the MIR CFG
852 | TerminatorKind::Assert { .. }
853 | TerminatorKind::Drop { .. }
854 | TerminatorKind::SwitchInt { .. }
855 // For `FalseEdge`, only the `real` branch is taken, so it is similar to a `Goto`.
856 | TerminatorKind::FalseEdge { .. }
857 | TerminatorKind::Goto { .. } => None,
858
859 // Call `func` operand can have a more specific span when part of a chain of calls
860 | TerminatorKind::Call { ref func, .. } => {
861 let mut span = terminator.source_info.span;
862 if let mir::Operand::Constant(box constant) = func {
863 if constant.span.lo() > span.lo() {
864 span = span.with_lo(constant.span.lo());
865 }
866 }
867 Some(span)
868 }
869
870 // Retain spans from all other terminators
871 TerminatorKind::Resume
872 | TerminatorKind::Terminate
873 | TerminatorKind::Return
874 | TerminatorKind::Yield { .. }
875 | TerminatorKind::GeneratorDrop
876 | TerminatorKind::FalseUnwind { .. }
877 | TerminatorKind::InlineAsm { .. } => {
878 Some(terminator.source_info.span)
879 }
880 }
881 }
882
883 /// Returns an extrapolated span (pre-expansion[^1]) corresponding to a range
884 /// within the function's body source. This span is guaranteed to be contained
885 /// within, or equal to, the `body_span`. If the extrapolated span is not
886 /// contained within the `body_span`, the `body_span` is returned.
887 ///
888 /// [^1]Expansions result from Rust syntax including macros, syntactic sugar,
889 /// etc.).
890 #[inline]
function_source_span(span: Span, body_span: Span) -> Span891 pub(super) fn function_source_span(span: Span, body_span: Span) -> Span {
892 let original_span = original_sp(span, body_span).with_ctxt(body_span.ctxt());
893 if body_span.contains(original_span) { original_span } else { body_span }
894 }
895