rust-lang / rust-lang/rust

Inlining `-Cinstrument-coverage` code into a uninstrumented crate causes problems

Open
#132,436 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-code-coverage T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

-Cinstrument-coverage is a per-crate setting, which makes it possible for instrumented and uninstrumented crates to be mixed during compilation.

This must be supported to some extent, because the standard libraries are built without coverage instrumentation, and instrumenting them would be unwanted in most cases. And we would also like to support instrumenting a project without instrumenting its dependencies, though cargo currently doesn't have a good way to actually set different rustflags for dependencies. Fortunately, inlining uninstrumented code into an instrumented crate mostly works, though in some cases it might end up being unexpectedly instrumented.

The more difficult case is when instrumented code gets inlined into an uninstrumented crate. This is an unusual configuration, but it can occur in practice, e.g. when building doctests as in https://github.com/rust-lang/rust/pull/132395#issuecomment-2449638516. When this happens, two problems can arise:

  • Inlined MIR might contain StatementKind::Coverage statements, despite the current crate being built without coverage instrumentation. When codegen sees coverage statements, it cannot assume that coverage instrumentation is enabled.
  • If the inlined code isn't instrumented, then executing it won't increase coverage counts, despite its original crate having been built with instrumentation.

For now, the workaround is to quietly discard any coverage statements that make their way into an uninstrumented crate. This avoids the ICE seen in #132395, but doesn't solve the problem of that code not being instrumented as a result.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the doctest-building scenario referenced in the comment on #132395, then trace how inlined MIR containing StatementKind::Coverage is handled in an uninstrumented crate. Compare the current workaround of discarding coverage statements with the two reported failures: avoiding the ICE and preserving coverage counts for inlined instrumented code.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.