Skip to content

Commit 319693a

Browse files
committed
coverage: Simplify initial creation of coverage spans
1 parent e164944 commit 319693a

File tree

2 files changed

+32
-45
lines changed

2 files changed

+32
-45
lines changed

compiler/rustc_mir_transform/src/coverage/spans.rs

+4-29
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::cell::OnceCell;
22

33
use rustc_data_structures::graph::WithNumNodes;
44
use rustc_index::IndexVec;
5-
use rustc_middle::mir::{self, AggregateKind, Rvalue, Statement, StatementKind};
5+
use rustc_middle::mir;
66
use rustc_span::{BytePos, ExpnKind, MacroKind, Span, Symbol, DUMMY_SP};
77

88
use super::graph::{BasicCoverageBlock, CoverageGraph, START_BCB};
@@ -75,29 +75,15 @@ struct CoverageSpan {
7575

7676
impl CoverageSpan {
7777
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-
merged_spans: vec![fn_sig_span],
84-
is_closure: false,
85-
}
78+
Self::new(fn_sig_span, fn_sig_span, START_BCB, false)
8679
}
8780

88-
pub fn for_statement(
89-
statement: &Statement<'_>,
81+
pub(super) fn new(
9082
span: Span,
9183
expn_span: Span,
9284
bcb: BasicCoverageBlock,
85+
is_closure: bool,
9386
) -> Self {
94-
let is_closure = match statement.kind {
95-
StatementKind::Assign(box (_, Rvalue::Aggregate(box ref kind, _))) => {
96-
matches!(kind, AggregateKind::Closure(_, _) | AggregateKind::Coroutine(_, _, _))
97-
}
98-
_ => false,
99-
};
100-
10187
Self {
10288
span,
10389
expn_span,
@@ -108,17 +94,6 @@ impl CoverageSpan {
10894
}
10995
}
11096

111-
pub fn for_terminator(span: Span, expn_span: Span, bcb: BasicCoverageBlock) -> Self {
112-
Self {
113-
span,
114-
expn_span,
115-
current_macro_or_none: Default::default(),
116-
bcb,
117-
merged_spans: vec![span],
118-
is_closure: false,
119-
}
120-
}
121-
12297
pub fn merge_from(&mut self, mut other: CoverageSpan) {
12398
debug_assert!(self.is_mergeable(&other));
12499
self.span = self.span.to(other.span);

compiler/rustc_mir_transform/src/coverage/spans/from_mir.rs

+28-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_data_structures::captures::Captures;
22
use rustc_middle::mir::{
3-
self, FakeReadCause, Statement, StatementKind, Terminator, TerminatorKind,
3+
self, AggregateKind, FakeReadCause, Rvalue, Statement, StatementKind, Terminator,
4+
TerminatorKind,
45
};
56
use rustc_span::Span;
67

@@ -59,24 +60,35 @@ fn bcb_to_initial_coverage_spans<'a, 'tcx>(
5960
) -> impl Iterator<Item = CoverageSpan> + Captures<'a> + Captures<'tcx> {
6061
bcb_data.basic_blocks.iter().flat_map(move |&bb| {
6162
let data = &mir_body[bb];
62-
data.statements
63-
.iter()
64-
.filter_map(move |statement| {
65-
filtered_statement_span(statement).map(|span| {
66-
CoverageSpan::for_statement(
67-
statement,
68-
function_source_span(span, body_span),
69-
span,
70-
bcb,
71-
)
72-
})
73-
})
74-
.chain(filtered_terminator_span(data.terminator()).map(|span| {
75-
CoverageSpan::for_terminator(function_source_span(span, body_span), span, bcb)
76-
}))
63+
64+
let statement_spans = data.statements.iter().filter_map(move |statement| {
65+
let expn_span = filtered_statement_span(statement)?;
66+
let span = function_source_span(expn_span, body_span);
67+
68+
Some(CoverageSpan::new(span, expn_span, bcb, is_closure(statement)))
69+
});
70+
71+
let terminator_span = Some(data.terminator()).into_iter().filter_map(move |terminator| {
72+
let expn_span = filtered_terminator_span(terminator)?;
73+
let span = function_source_span(expn_span, body_span);
74+
75+
Some(CoverageSpan::new(span, expn_span, bcb, false))
76+
});
77+
78+
statement_spans.chain(terminator_span)
7779
})
7880
}
7981

82+
fn is_closure(statement: &Statement<'_>) -> bool {
83+
match statement.kind {
84+
StatementKind::Assign(box (_, Rvalue::Aggregate(box ref agg_kind, _))) => match agg_kind {
85+
AggregateKind::Closure(_, _) | AggregateKind::Coroutine(_, _, _) => true,
86+
_ => false,
87+
},
88+
_ => false,
89+
}
90+
}
91+
8092
/// If the MIR `Statement` has a span contributive to computing coverage spans,
8193
/// return it; otherwise return `None`.
8294
fn filtered_statement_span(statement: &Statement<'_>) -> Option<Span> {

0 commit comments

Comments
 (0)