- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Closed
Labels
A-code-coverageArea: Source-based code coverage (-Cinstrument-coverage)Area: Source-based code coverage (-Cinstrument-coverage)C-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
-Z instrument-coverage seems to ignore files or modules where all items are dead code.
Originally found in taiki-e/cargo-llvm-cov#26.
How to reproduce
mkdir repro
cd repro
mkdir src
echo 'mod module;
pub use module::*;
#[test]
fn f() {}' >src/lib.rs
echo 'pub fn func(x: u32) {
    match x {
        0 => {}
        1 => {}
        _ => {}
    }
}' >src/module.rs
echo '[package]
name = "repro"
version = "0.0.0"' >Cargo.toml
RUSTFLAGS="-Z instrument-coverage" \
    LLVM_PROFILE_FILE="repro-%m.profraw" \
    cargo test --tests
cargo profdata -- merge \
    -sparse repro-*.profraw -o repro.profdata
cargo cov -- report \
    $( \
      for file in \
        $( \
          RUSTFLAGS="-Z instrument-coverage" \
            cargo test --tests --no-run --message-format=json \
              | jq -r "select(.profile.test == true) | .filenames[]" \
              | grep -v dSYM - \
        ); \
      do \
        printf "%s %s " -object $file; \
      done \
    ) \
  --instr-profile=repro.profdata --summary-onlyResult (src/module.rs is ignored):
Filename                      Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
src/lib.rs                          3                 0   100.00%           3                 0   100.00%           3                 0   100.00%           0                 0         -
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                               3                 0   100.00%           3                 0   100.00%           3                 0   100.00%           0                 0         -
Workaround
The known workaround is using -C link-dead-code, but it seems not the correct workaround and is likely to cause another problem. See also taiki-e/cargo-llvm-cov#26 (comment)
Meta
rustc --version --verbose:
rustc 1.54.0-nightly (ed597e7e1 2021-06-08)
binary: rustc
commit-hash: ed597e7e19d0fe716d9f81b1e840a5abbfd7c28d
commit-date: 2021-06-08
host: x86_64-apple-darwin
release: 1.54.0-nightly
LLVM version: 12.0.1
cc @richkadel
@rustbot label A-code-coverage
Anders429
Metadata
Metadata
Assignees
Labels
A-code-coverageArea: Source-based code coverage (-Cinstrument-coverage)Area: Source-based code coverage (-Cinstrument-coverage)C-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.