Change:
); } <del> /// When finalizing the coverage map, `FunctionCoverage` only has the `CodeRegion`s and counters for /// the functions that went through codegen; such as public functions and "used" functions /// (functions referenced by other "used" or public items). Any other functions considered unused, /// or "Unreachable", were still parsed and processed through the MIR stage, but were not /// codegenned. (Note that `-Clink-dead-code` can force some unused code to be codegenned, but /// that flag is known to cause other errors, when combined with `-C instrument-coverage`; and /// `-Clink-dead-code` will not generate code for unused generic functions.) /// /// We can find the unused functions (including generic functions) by the set difference of all MIR /// `DefId`s (`tcx` query `mir_keys`) minus the codegenned `DefId`s (`codegenned_and_inlined_items`). </del> <ins> /// Each CGU will normally only emit coverage metadata for the functions that it actually generates. /// But since we don't want unused functions to disappear from coverage reports, we also scan for /// functions that were instrumented but are not participating in codegen. </ins> /// /// These unused functions don't need to be codegenned, but we do need to add them to the function /// coverage map (in a single designated CGU) so that we still emit coverage mappings for them.

Discussion:
When coverage instrumentation and MIR opts are both enabled, coverage relies on two assumptions: MIR opts that would delete statements instead move them into bb0 and change them to .MIR opts won't delete all from an instrumented function. The first assumption will be lifted by , which stores coverage mappings in a separate per-function struct, so the original mappings will be available during codegen even if MIR opts delete arbitrary coverage statements. The second assumption is trickier. If a function is instrumented for coverage, but MIR opts remove all of its counter-increment statements (e.g. because bb0's ends with ), then we will codegen a function with no intrinsics. This causes LLVM to assume that the function wasn't instrumented, so it will disappear from the final coverage mappings and won't participate in coverage reports. Coverage has special code for handling functions that are deemed “unused” at the MIR level, but if a function is used (e.g. as a function pointer) but its body is unreachable, then that special code isn't applied. Ideally what we need is some way for coverage codegen to detect that an instrumented function has no remaining counter-increment statements, and take special action to either re-insert at least one counter-increment (so LLVM treats it as instrumented), or treat it as though it were unused (so that it shows up in reports as never-executed). label +A-code-coverage +T-compiler
Creation of this issue was requested in , so that it can be linked to in a comment explaining why we turn off the pass when coverage is enabled. Once coverage instrumentation is robust against MIR opts that delete all coverage statements, we can remove that condition and enable in optimized coverage builds.
Currently it's difficult to detect this case because coverage codegen normally only knows about a function if it contains one or more statements, or if it was not codegenned at all. There is no way to detect a function that was instrumented and then codegenned without any coverage statements. Once we have per-function coverage info from , we'll be in a position to detect instrumented functions that have no remaining coverage statements, because they'll still have the coverage info that was attached during instrumentation.
Hmm, I may have made a mistake in concluding that LLVM sees the function as not instrumented (based on looking at the wrong logs).

It might be that LLVM doesn't see them at all? Either way turning off still seems like the right call, but I'll have to do more investigation into where the functions are disappearing. EDIT: Oh, never mind, I was misled by the fact that I was re-injecting coverage statements but wasn't seeing them in codegen. But that turned out to be because they had the wrong source info (from the terminator inlined from ), so they were treated as inlined and were being discarded on that basis.
II have a work-in-progress patch that fixes this via a new MIR pass that runs after optimizations, detects functions that were instrumented but no longer have any counter increments, and re-adds a counter increment to bb0. That seems to do the trick. I’ll polish it up and add it to my patch queue.
I also found a way to test this that doesn't rely on : write a function that uses . That triggers the existing code in that can remove all statements from a block.