Rollup merge of #118666 - Zalathar:body-closure, r=cjgillot
coverage: Simplify the heuristic for ignoring `async fn` return spans The code for extracting coverage spans from MIR has a special heuristic for dealing with `async fn`, so that the function's closing brace does not have a confusing double count. The code implementing that heuristic is currently mixed in with the code for flushing remaining spans after the main refinement loop, making the refinement code harder to understand. We can solve that by hoisting the heuristic to an earlier stage, after the spans have been extracted and sorted but before they have been processed by the refinement loop. The coverage tests verify that the heuristic is still effective, so coverage mappings/reports for `async fn` have not changed. --- This PR also has the side-effect of fixing the `None some_prev` panic that started appearing after #118525. The old code assumed that `prev` would always be present after the refinement loop. That was only true if the list of collected spans was non-empty, but prior to #118525 that didn't seem to come up in practice. After that change, the list of collected spans could be empty in some specific circumstances, leading to panics. The new code uses an `if let` to inspect `prev`, which correctly does nothing if there is no span present.
This commit is contained in:
commit
feb879394a
5 changed files with 93 additions and 31 deletions
|
@ -44,6 +44,16 @@ pub(super) fn mir_to_initial_sorted_coverage_spans(
|
|||
.then_with(|| Ord::cmp(&a.is_closure, &b.is_closure).reverse())
|
||||
});
|
||||
|
||||
// The desugaring of an async function includes a closure containing the
|
||||
// original function body, and a terminator that returns the `impl Future`.
|
||||
// That terminator will cause a confusing coverage count for the function's
|
||||
// closing brace, so discard everything after the body closure span.
|
||||
if let Some(body_closure_index) =
|
||||
initial_spans.iter().rposition(|covspan| covspan.is_closure && covspan.span == body_span)
|
||||
{
|
||||
initial_spans.truncate(body_closure_index + 1);
|
||||
}
|
||||
|
||||
initial_spans
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue