Skip to content

Commit 793776f

Browse files
authoredOct 31, 2023
Rollup merge of #117421 - Zalathar:error, r=oli-obk,Swatinem
coverage: Replace impossible `coverage::Error` with assertions Historically, these errors existed so that the coverage debug code could dump additional information before reporting a compiler bug. That debug code was removed by #115962, so we can now simplify these methods by making them panic immediately when they detect a bug.
2 parents 8daa317 + 6d956a2 commit 793776f

File tree

3 files changed

+43
-77
lines changed

3 files changed

+43
-77
lines changed
 

‎compiler/rustc_mir_transform/src/coverage/counters.rs

+40-57
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use super::Error;
2-
31
use super::graph;
42

53
use graph::{BasicCoverageBlock, BcbBranch, CoverageGraph, TraverseCoverageGraphWithLoops};
@@ -53,7 +51,7 @@ pub(super) struct CoverageCounters {
5351
/// edge between two BCBs.
5452
bcb_edge_counters: FxHashMap<(BasicCoverageBlock, BasicCoverageBlock), BcbCounter>,
5553
/// Tracks which BCBs have a counter associated with some incoming edge.
56-
/// Only used by debug assertions, to verify that BCBs with incoming edge
54+
/// Only used by assertions, to verify that BCBs with incoming edge
5755
/// counters do not have their own physical counters (expressions are allowed).
5856
bcb_has_incoming_edge_counters: BitSet<BasicCoverageBlock>,
5957
/// Table of expression data, associating each expression ID with its
@@ -81,7 +79,7 @@ impl CoverageCounters {
8179
&mut self,
8280
basic_coverage_blocks: &CoverageGraph,
8381
bcb_has_coverage_spans: impl Fn(BasicCoverageBlock) -> bool,
84-
) -> Result<(), Error> {
82+
) {
8583
MakeBcbCounters::new(self, basic_coverage_blocks).make_bcb_counters(bcb_has_coverage_spans)
8684
}
8785

@@ -111,26 +109,23 @@ impl CoverageCounters {
111109
self.expressions.len()
112110
}
113111

114-
fn set_bcb_counter(
115-
&mut self,
116-
bcb: BasicCoverageBlock,
117-
counter_kind: BcbCounter,
118-
) -> Result<CovTerm, Error> {
119-
debug_assert!(
112+
fn set_bcb_counter(&mut self, bcb: BasicCoverageBlock, counter_kind: BcbCounter) -> CovTerm {
113+
assert!(
120114
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
121115
// have an expression (to be injected into an existing `BasicBlock` represented by this
122116
// `BasicCoverageBlock`).
123117
counter_kind.is_expression() || !self.bcb_has_incoming_edge_counters.contains(bcb),
124118
"attempt to add a `Counter` to a BCB target with existing incoming edge counters"
125119
);
120+
126121
let term = counter_kind.as_term();
127122
if let Some(replaced) = self.bcb_counters[bcb].replace(counter_kind) {
128-
Error::from_string(format!(
123+
bug!(
129124
"attempt to set a BasicCoverageBlock coverage counter more than once; \
130125
{bcb:?} already had counter {replaced:?}",
131-
))
126+
);
132127
} else {
133-
Ok(term)
128+
term
134129
}
135130
}
136131

@@ -139,27 +134,26 @@ impl CoverageCounters {
139134
from_bcb: BasicCoverageBlock,
140135
to_bcb: BasicCoverageBlock,
141136
counter_kind: BcbCounter,
142-
) -> Result<CovTerm, Error> {
143-
if level_enabled!(tracing::Level::DEBUG) {
144-
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
145-
// have an expression (to be injected into an existing `BasicBlock` represented by this
146-
// `BasicCoverageBlock`).
147-
if self.bcb_counter(to_bcb).is_some_and(|c| !c.is_expression()) {
148-
return Error::from_string(format!(
149-
"attempt to add an incoming edge counter from {from_bcb:?} when the target BCB already \
150-
has a `Counter`"
151-
));
152-
}
137+
) -> CovTerm {
138+
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
139+
// have an expression (to be injected into an existing `BasicBlock` represented by this
140+
// `BasicCoverageBlock`).
141+
if let Some(node_counter) = self.bcb_counter(to_bcb) && !node_counter.is_expression() {
142+
bug!(
143+
"attempt to add an incoming edge counter from {from_bcb:?} \
144+
when the target BCB already has {node_counter:?}"
145+
);
153146
}
147+
154148
self.bcb_has_incoming_edge_counters.insert(to_bcb);
155149
let term = counter_kind.as_term();
156150
if let Some(replaced) = self.bcb_edge_counters.insert((from_bcb, to_bcb), counter_kind) {
157-
Error::from_string(format!(
151+
bug!(
158152
"attempt to set an edge counter more than once; from_bcb: \
159153
{from_bcb:?} already had counter {replaced:?}",
160-
))
154+
);
161155
} else {
162-
Ok(term)
156+
term
163157
}
164158
}
165159

@@ -213,14 +207,7 @@ impl<'a> MakeBcbCounters<'a> {
213207
/// One way to predict which branch executes the least is by considering loops. A loop is exited
214208
/// at a branch, so the branch that jumps to a `BasicCoverageBlock` outside the loop is almost
215209
/// always executed less than the branch that does not exit the loop.
216-
///
217-
/// Returns any non-code-span expressions created to represent intermediate values (such as to
218-
/// add two counters so the result can be subtracted from another counter), or an Error with
219-
/// message for subsequent debugging.
220-
fn make_bcb_counters(
221-
&mut self,
222-
bcb_has_coverage_spans: impl Fn(BasicCoverageBlock) -> bool,
223-
) -> Result<(), Error> {
210+
fn make_bcb_counters(&mut self, bcb_has_coverage_spans: impl Fn(BasicCoverageBlock) -> bool) {
224211
debug!("make_bcb_counters(): adding a counter or expression to each BasicCoverageBlock");
225212

226213
// Walk the `CoverageGraph`. For each `BasicCoverageBlock` node with an associated
@@ -237,10 +224,10 @@ impl<'a> MakeBcbCounters<'a> {
237224
while let Some(bcb) = traversal.next() {
238225
if bcb_has_coverage_spans(bcb) {
239226
debug!("{:?} has at least one coverage span. Get or make its counter", bcb);
240-
let branching_counter_operand = self.get_or_make_counter_operand(bcb)?;
227+
let branching_counter_operand = self.get_or_make_counter_operand(bcb);
241228

242229
if self.bcb_needs_branch_counters(bcb) {
243-
self.make_branch_counters(&traversal, bcb, branching_counter_operand)?;
230+
self.make_branch_counters(&traversal, bcb, branching_counter_operand);
244231
}
245232
} else {
246233
debug!(
@@ -251,22 +238,19 @@ impl<'a> MakeBcbCounters<'a> {
251238
}
252239
}
253240

254-
if traversal.is_complete() {
255-
Ok(())
256-
} else {
257-
Error::from_string(format!(
258-
"`TraverseCoverageGraphWithLoops` missed some `BasicCoverageBlock`s: {:?}",
259-
traversal.unvisited(),
260-
))
261-
}
241+
assert!(
242+
traversal.is_complete(),
243+
"`TraverseCoverageGraphWithLoops` missed some `BasicCoverageBlock`s: {:?}",
244+
traversal.unvisited(),
245+
);
262246
}
263247

264248
fn make_branch_counters(
265249
&mut self,
266250
traversal: &TraverseCoverageGraphWithLoops<'_>,
267251
branching_bcb: BasicCoverageBlock,
268252
branching_counter_operand: CovTerm,
269-
) -> Result<(), Error> {
253+
) {
270254
let branches = self.bcb_branches(branching_bcb);
271255
debug!(
272256
"{:?} has some branch(es) without counters:\n {}",
@@ -299,10 +283,10 @@ impl<'a> MakeBcbCounters<'a> {
299283
counter",
300284
branch, branching_bcb
301285
);
302-
self.get_or_make_counter_operand(branch.target_bcb)?
286+
self.get_or_make_counter_operand(branch.target_bcb)
303287
} else {
304288
debug!(" {:?} has multiple incoming edges, so adding an edge counter", branch);
305-
self.get_or_make_edge_counter_operand(branching_bcb, branch.target_bcb)?
289+
self.get_or_make_edge_counter_operand(branching_bcb, branch.target_bcb)
306290
};
307291
if let Some(sumup_counter_operand) =
308292
some_sumup_counter_operand.replace(branch_counter_operand)
@@ -337,19 +321,18 @@ impl<'a> MakeBcbCounters<'a> {
337321
debug!("{:?} gets an expression: {:?}", expression_branch, expression);
338322
let bcb = expression_branch.target_bcb;
339323
if expression_branch.is_only_path_to_target() {
340-
self.coverage_counters.set_bcb_counter(bcb, expression)?;
324+
self.coverage_counters.set_bcb_counter(bcb, expression);
341325
} else {
342-
self.coverage_counters.set_bcb_edge_counter(branching_bcb, bcb, expression)?;
326+
self.coverage_counters.set_bcb_edge_counter(branching_bcb, bcb, expression);
343327
}
344-
Ok(())
345328
}
346329

347330
#[instrument(level = "debug", skip(self))]
348-
fn get_or_make_counter_operand(&mut self, bcb: BasicCoverageBlock) -> Result<CovTerm, Error> {
331+
fn get_or_make_counter_operand(&mut self, bcb: BasicCoverageBlock) -> CovTerm {
349332
// If the BCB already has a counter, return it.
350333
if let Some(counter_kind) = &self.coverage_counters.bcb_counters[bcb] {
351334
debug!("{bcb:?} already has a counter: {counter_kind:?}");
352-
return Ok(counter_kind.as_term());
335+
return counter_kind.as_term();
353336
}
354337

355338
// A BCB with only one incoming edge gets a simple `Counter` (via `make_counter()`).
@@ -378,10 +361,10 @@ impl<'a> MakeBcbCounters<'a> {
378361

379362
let mut predecessors = self.bcb_predecessors(bcb).to_owned().into_iter();
380363
let first_edge_counter_operand =
381-
self.get_or_make_edge_counter_operand(predecessors.next().unwrap(), bcb)?;
364+
self.get_or_make_edge_counter_operand(predecessors.next().unwrap(), bcb);
382365
let mut some_sumup_edge_counter_operand = None;
383366
for predecessor in predecessors {
384-
let edge_counter_operand = self.get_or_make_edge_counter_operand(predecessor, bcb)?;
367+
let edge_counter_operand = self.get_or_make_edge_counter_operand(predecessor, bcb);
385368
if let Some(sumup_edge_counter_operand) =
386369
some_sumup_edge_counter_operand.replace(edge_counter_operand)
387370
{
@@ -411,7 +394,7 @@ impl<'a> MakeBcbCounters<'a> {
411394
&mut self,
412395
from_bcb: BasicCoverageBlock,
413396
to_bcb: BasicCoverageBlock,
414-
) -> Result<CovTerm, Error> {
397+
) -> CovTerm {
415398
// If the source BCB has only one successor (assumed to be the given target), an edge
416399
// counter is unnecessary. Just get or make a counter for the source BCB.
417400
let successors = self.bcb_successors(from_bcb).iter();
@@ -424,7 +407,7 @@ impl<'a> MakeBcbCounters<'a> {
424407
self.coverage_counters.bcb_edge_counters.get(&(from_bcb, to_bcb))
425408
{
426409
debug!("Edge {from_bcb:?}->{to_bcb:?} already has a counter: {counter_kind:?}");
427-
return Ok(counter_kind.as_term());
410+
return counter_kind.as_term();
428411
}
429412

430413
// Make a new counter to count this edge.

‎compiler/rustc_mir_transform/src/coverage/mod.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,6 @@ use rustc_span::def_id::DefId;
2626
use rustc_span::source_map::SourceMap;
2727
use rustc_span::{ExpnKind, SourceFile, Span, Symbol};
2828

29-
/// A simple error message wrapper for `coverage::Error`s.
30-
#[derive(Debug)]
31-
struct Error {
32-
message: String,
33-
}
34-
35-
impl Error {
36-
pub fn from_string<T>(message: String) -> Result<T, Error> {
37-
Err(Self { message })
38-
}
39-
}
40-
4129
/// Inserts `StatementKind::Coverage` statements that either instrument the binary with injected
4230
/// counters, via intrinsic `llvm.instrprof.increment`, and/or inject metadata used during codegen
4331
/// to construct the coverage map.
@@ -167,10 +155,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
167155
// `BasicCoverageBlock`s not already associated with a coverage span.
168156
let bcb_has_coverage_spans = |bcb| coverage_spans.bcb_has_coverage_spans(bcb);
169157
self.coverage_counters
170-
.make_bcb_counters(&mut self.basic_coverage_blocks, bcb_has_coverage_spans)
171-
.unwrap_or_else(|e| {
172-
bug!("Error processing: {:?}: {:?}", self.mir_body.source.def_id(), e.message)
173-
});
158+
.make_bcb_counters(&self.basic_coverage_blocks, bcb_has_coverage_spans);
174159

175160
let mappings = self.create_mappings_and_inject_coverage_statements(&coverage_spans);
176161

‎compiler/rustc_mir_transform/src/coverage/tests.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -647,15 +647,13 @@ fn test_traverse_coverage_with_loops() {
647647
fn test_make_bcb_counters() {
648648
rustc_span::create_default_session_globals_then(|| {
649649
let mir_body = goto_switchint();
650-
let mut basic_coverage_blocks = graph::CoverageGraph::from_mir(&mir_body);
650+
let basic_coverage_blocks = graph::CoverageGraph::from_mir(&mir_body);
651651
// Historically this test would use `spans` internals to set up fake
652652
// coverage spans for BCBs 1 and 2. Now we skip that step and just tell
653653
// BCB counter construction that those BCBs have spans.
654654
let bcb_has_coverage_spans = |bcb: BasicCoverageBlock| (1..=2).contains(&bcb.as_usize());
655655
let mut coverage_counters = counters::CoverageCounters::new(&basic_coverage_blocks);
656-
coverage_counters
657-
.make_bcb_counters(&mut basic_coverage_blocks, bcb_has_coverage_spans)
658-
.expect("should be Ok");
656+
coverage_counters.make_bcb_counters(&basic_coverage_blocks, bcb_has_coverage_spans);
659657
assert_eq!(coverage_counters.num_expressions(), 0);
660658

661659
let_bcb!(1);

0 commit comments

Comments
 (0)