Description
Source-based code coverage (via -Z instrument-coverage
) does not show coverage for unreachable blocks that were optimized out during MIR transformations. For example:
if false {
println!("never executed");
}
Ideally the second line would show a coverage execution count of 0
, but, assuming this snippet is executed, it would instead show something like:
1 if false {
println!("never executed");
1 }
The coverage percentage for this block would be computed as 100%, but should be about 66% (by lines covered).
The MIR InstrumentCoverage
pass is performed before MIR optimizations, so the coverage instrumentation is available in MIR for line 2. However, subsequent optimizations drop the MIR block, including the coverage Statement
before the codegen phase. The coverage map is generated during codegen, and can use tcx
queries to extract data from the optimized_mir()
, but that version of the MIR has already been purged of the unreachable blocks.
As far as I can tell, tcx
queries for intermediate MIR representations (for example, tcx.mir_promoted()
) are not available at codegen (they have already been "stolen" by the optimization passes).
To solve this problem, I believe I need to cache at least the coverage statements (or their data at least) for the pre-optimized MIR, before those statements are potentially dropped by subsequent MIR passes.
cc: @tmandry @wesleywiser
Activity