Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect Rust region coverage? #324

Open
wintered opened this issue Dec 5, 2023 · 6 comments
Open

Incorrect Rust region coverage? #324

wintered opened this issue Dec 5, 2023 · 6 comments
Labels
C-upstream-bug Category: This is a bug of compiler or dependencies (the fix may require action in the upstream)

Comments

@wintered
Copy link

wintered commented Dec 5, 2023

Hi,

cargo-llvm-cov reports that the following Rust code had 5 region coverage. Why? I count only 4: (1+2) both functions, (3) the if condition, (4) the "then branch". I also looked at the generated llvm-ir but could not figure out where the fifth region came from.

// main.rs
fn foo (c1: bool) {
    if c1 {
        println!("c1 true");
    }
    println!("join");
}

fn main() {
    foo(true);
}
Screenshot 2023-12-05 at 12 25 59 PM

The corresponding C++ program has 4 regions.

// main.cpp
#include <iostream>
void foo(bool c1) {
    if (c1) {
        std::cout << "c1 true" << std::endl;
    }
    std::cout << "join" << std::endl;
}
int main() {
    foo(true);
}
image

cargo-llvm-cov: 0.5.36
LLVM version 17.0.4

@Zalathar
Copy link

Zalathar commented Dec 8, 2023

The else arm of the if expression gets its own region, even if there is no else in the source file.

In your example program the else arm isn't executed and has an execution count of 0.

In the C++ program, this fact is represented by the Branch Coverage column instead. (Rust currently doesn't support LLVM branch coverage mappings.)

@wintered
Copy link
Author

wintered commented Dec 8, 2023

The else arm of the if expression gets its own region, even if there is no else in the source file.

Why in C++, the else branch does not get its own region even though it does in Rust?

@taiki-e
Copy link
Owner

taiki-e commented Mar 20, 2024

In Rust with branch coverage enabled (--branch, #8):

issue324-rust-branch-cov issue324-rust-branch-cov-2

@taiki-e taiki-e added the C-upstream-bug Category: This is a bug of compiler or dependencies (the fix may require action in the upstream) label Mar 20, 2024
@taiki-e
Copy link
Owner

taiki-e commented Mar 20, 2024

I believe this is an issue on the rustc rather than the cargo-llvm-cov.

Would you mind submitting an issue at https://github.com/rust-lang/rust?

@Zalathar
Copy link

I’m not quite sure what the bug here is supposed to be.

Are you referring to the fact that the uncovered else block isn’t highlighted on line 4?

@taiki-e
Copy link
Owner

taiki-e commented Mar 20, 2024

Are you referring to the fact that the uncovered else block isn’t highlighted on line 4?

Yes.

I'm not 100% certain that this should be considered a bug, but in any case, since this is a discussion about the behavior of rustc and not cargo-llvm-cov, it is better to discuss it in the rust-lang/rust and not in this repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-upstream-bug Category: This is a bug of compiler or dependencies (the fix may require action in the upstream)
Projects
None yet
Development

No branches or pull requests

3 participants