Skip to content

-Cinstrument-coverage produces invalid coverage mapping for the test harness's entry point #110749

Closed
@Zalathar

Description

@Zalathar

Take this simple crate:

// THIS LINE SHOULD NOT BE DUPLICATED

fn main() {}

#[test]
fn my_test() {}

If I try to gather code coverage for cargo test, I end up with the first line mysteriously duplicated as a covered region:

$ cargo llvm-cov test --text
   Compiling cov-playground v0.1.0 (/Users/me/Dev/rust/cov-playground)
    Finished test [unoptimized + debuginfo] target(s) in 0.08s
     Running unittests src/main.rs (target/llvm-cov-target/debug/deps/cov_playground-e5b52a4d8622a05a)

running 1 test
test my_test ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

    1|      1|// THIS LINE SHOULD NOT BE DUPLICATED// THIS LINE SHOULD NOT BE DUPLICATED
    2|       |
    3|      0|fn main() {}
    4|       |
    5|      1|#[test]
    6|      1|fn my_test() {}

(I'm using cargo-llvm-cov for convenience, but the same thing happens when I use RUSTC_FLAGS="-Cinstrument-coverage" and process the coverage data manually with llvm-profdata and llvm-cov.)

Note that in the report produced by llvm-cov, the first line of the file appears twice. It is marked as having executed once, even though it is a comment.

Instead, the line should only appear once, and its execution count (if any) should be the execution count of the actual code on that line.

Meta

$ rustc --version --verbose
rustc 1.69.0 (84c898d65 2023-04-16)
binary: rustc
commit-hash: 84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc
commit-date: 2023-04-16
host: aarch64-apple-darwin
release: 1.69.0
LLVM version: 15.0.7
$ cargo llvm-cov --version
cargo-llvm-cov 0.5.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-code-coverageArea: Source-based code coverage (-Cinstrument-coverage)C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions