rust-lang / rust-lang/rust

`-C instrument-coverage` misses regions in const assertions

Open
#130,502 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hey, I've run into a problem with -C instrument-coverage, const generics and assertions.

I tried this code:

const fn check_even<const N: usize>() {
    assert!(N % 2 == 0, "will fail once");
}

#[test]
fn true_branch() {
    check_even::<8>();  // even number
}

#[test]
#[should_panic = "will fail once"]
fn false_branch() {
    check_even::<9>();  // will trigger the assertion
}

I prepared a reproducer with cargo, which is not exactly a MCVE, but the code itself is rather minimal. Therefore the following commands will set up a Cargo project with the aforementioned code in the lib.rs-file.

cargo new --lib repro
RUSTFLAGS="-C instrument-coverage" cargo test --manifest-path repro/Cargo.toml --tests
llvm-profdata merge -sparse repro/default_* -o merged.profdata
llvm-cov report --instr-profile merged.profdata repro/target/debug/deps/repro-* --show-branch-summary=false  # make sure, the correct test binary is picked up

I expected to see this happen: as all branches/regions are covered, I expect 100% test coverage.

Instead, this happened: only 80% region coverage (but 100% line coverage as expected):

Filename                                Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover
------------------------------------------------------------------------------------------------------------------------------------------------------------
/home/jfrimmel/git/repro/src/lib.rs           5                 1    80.00%           3                 0   100.00%           9                 0   100.00%
------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                         5                 1    80.00%           3                 0   100.00%           9                 0   100.00%

Specifically, the region containing the assertion message is not covered, despite it clearly has to be used, since the tests would fail due to the wrong panic message.
llvm-cov show

For reference: this actually hits me in a project of mine, which aims to have 100% coverage. The only regions not covered are caused by assertions. The CI logs are located here.

Meta

rustc --version --verbose:

rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7

This also reproduces with the nightlies 2024-09-18, 2024-09-09, 2024-09-01 and 2024-08-01.

@rustbot label +A-code-coverage

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

Reproduce the issue using the Cargo commands in the report and inspect repro/src/lib.rs together with the llvm-cov region report. Trace how rustc's -C instrument-coverage handles the const assertion and compare the reported regions with the two test paths; done means the assertion-message region is reported as covered and the reproducer reaches 100% region coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.