Skip to content

-C instrument-coverage misses regions in const assertions #130502

Open
@jfrimmel

Description

@jfrimmel

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-code-coverageArea: Source-based code coverage (-Cinstrument-coverage)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions