Skip to content

-Zinstrument-coverage ignores file (or module)  #86177

Closed
@taiki-e

Description

@taiki-e

-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-only

Result (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

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