rust/tests/coverage/closure.coverage

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

229 lines
8.5 KiB
Plaintext
Raw Normal View History

LL| |#![allow(unused_assignments, unused_variables)]
LL| |//@ compile-flags: -C opt-level=2
LL| |
LL| |// This test used to be sensitive to certain coverage-specific hacks in
LL| |// `rustc_middle/mir/mono.rs`, but those hacks were later cleaned up by
LL| |// <https://github.com/rust-lang/rust/pull/83666>.
LL| |
LL| |#[rustfmt::skip]
LL| 1|fn main() {
LL| 1| // Initialize test constants in a way that cannot be determined at compile time, to ensure
LL| 1| // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from
LL| 1| // dependent conditions.
LL| 1| let is_true = std::env::args().len() == 1;
LL| 1| let is_false = !is_true;
LL| 1|
LL| 1| let mut some_string = Some(String::from("the string content"));
LL| 1| println!(
LL| 1| "The string or alt: {}"
LL| 1| ,
LL| 1| some_string
LL| 1| .
LL| 1| unwrap_or_else
LL| 1| (
LL| 1| ||
LL| 0| {
LL| 0| let mut countdown = 0;
LL| 0| if is_false {
LL| 0| countdown = 10;
LL| 0| }
LL| 0| "alt string 1".to_owned()
LL| 1| }
LL| 1| )
LL| 1| );
LL| 1|
LL| 1| some_string = Some(String::from("the string content"));
LL| 1| let
LL| 1| a
LL| | =
LL| | ||
LL| 0| {
LL| 0| let mut countdown = 0;
LL| 0| if is_false {
LL| 0| countdown = 10;
LL| 0| }
LL| 0| "alt string 2".to_owned()
LL| 0| };
LL| 1| println!(
LL| 1| "The string or alt: {}"
LL| 1| ,
LL| 1| some_string
LL| 1| .
LL| 1| unwrap_or_else
LL| 1| (
LL| 1| a
LL| 1| )
LL| 1| );
LL| 1|
LL| 1| some_string = None;
LL| 1| println!(
LL| 1| "The string or alt: {}"
LL| 1| ,
LL| 1| some_string
LL| 1| .
LL| 1| unwrap_or_else
LL| 1| (
LL| 1| ||
LL| 1| {
LL| 1| let mut countdown = 0;
LL| 1| if is_false {
LL| 0| countdown = 10;
LL| 1| }
LL| 1| "alt string 3".to_owned()
LL| 1| }
LL| 1| )
LL| 1| );
LL| 1|
LL| 1| some_string = None;
LL| 1| let
LL| 1| a
LL| | =
LL| | ||
LL| 1| {
LL| 1| let mut countdown = 0;
LL| 1| if is_false {
LL| 0| countdown = 10;
LL| 1| }
LL| 1| "alt string 4".to_owned()
LL| 1| };
LL| 1| println!(
LL| 1| "The string or alt: {}"
LL| 1| ,
LL| 1| some_string
LL| 1| .
LL| 1| unwrap_or_else
LL| 1| (
LL| 1| a
LL| 1| )
LL| 1| );
LL| 1|
LL| 1| let
LL| 1| quote_closure
LL| | =
LL| | |val|
LL| 5| {
LL| 5| let mut countdown = 0;
LL| 5| if is_false {
LL| 0| countdown = 10;
LL| 5| }
LL| 5| format!("'{}'", val)
LL| 5| };
LL| 1| println!(
LL| 1| "Repeated, quoted string: {:?}"
LL| 1| ,
LL| 1| std::iter::repeat("repeat me")
LL| 1| .take(5)
LL| 1| .map
LL| 1| (
LL| 1| quote_closure
LL| 1| )
LL| 1| .collect::<Vec<_>>()
LL| 1| );
LL| 1|
LL| 1| let
LL| 1| _unused_closure
LL| | =
LL| | |
LL| | mut countdown
LL| | |
LL| 0| {
LL| 0| if is_false {
LL| 0| countdown = 10;
LL| 0| }
LL| 0| "closure should be unused".to_owned()
LL| 0| };
LL| |
LL| 1| let mut countdown = 10;
LL| 1| let _short_unused_closure = | _unused_arg: u8 | countdown += 1;
^0
LL| |
LL| |
LL| 1| let short_used_covered_closure_macro = | used_arg: u8 | println!("called");
LL| 1| let short_used_not_covered_closure_macro = | used_arg: u8 | println!("not called");
Coverage instruments closure bodies in macros (not the macro body) Fixes: #84884 This solution might be considered a compromise, but I think it is the better choice. The results in the `closure.rs` test correctly resolve all test cases broken as described in #84884. One test pattern (in both `closure_macro.rs` and `closure_macro_async.rs`) was also affected, and removes coverage statistics for the lines inside the closure, because the closure includes a macro. (The coverage remains at the callsite of the macro, so we lose some detail, but there isn't a perfect choice with macros. Often macro implementations are split across the macro and the callsite, and there doesn't appear to be a single "right choice" for which body should be covered. For the current implementation, we can't do both. The callsite is most likely to be the preferred site for coverage. I applied this fix to all `MacroKinds`, not just `Bang`. I'm trying to resolve an issue of lost coverage in a `MacroKind::Attr`-based, function-scoped macro. Instead of only searching for a body_span that is "not a function-like macro" (that is, MacroKind::Bang), I'm expanding this to all `MacroKind`s. Maybe I should expand this to `ExpnKind::Desugaring` and `ExpnKind::AstPass` (or subsets, depending on their sub-kinds) as well, but I'm not sure that's a good idea. I'd like to add a test of the `Attr` macro on functions, but I need time to figure out how to constract a good, simple example without external crate dependencies. For the moment, all tests still work as expected (no change), this new commit shouldn't have a negative affect, and more importantly, I believe it will have a positive effect. I will try to confirm this.
2021-05-04 01:21:24 -05:00
^0
LL| 1| let _short_unused_closure_macro = | _unused_arg: u8 | println!("not called");
Coverage instruments closure bodies in macros (not the macro body) Fixes: #84884 This solution might be considered a compromise, but I think it is the better choice. The results in the `closure.rs` test correctly resolve all test cases broken as described in #84884. One test pattern (in both `closure_macro.rs` and `closure_macro_async.rs`) was also affected, and removes coverage statistics for the lines inside the closure, because the closure includes a macro. (The coverage remains at the callsite of the macro, so we lose some detail, but there isn't a perfect choice with macros. Often macro implementations are split across the macro and the callsite, and there doesn't appear to be a single "right choice" for which body should be covered. For the current implementation, we can't do both. The callsite is most likely to be the preferred site for coverage. I applied this fix to all `MacroKinds`, not just `Bang`. I'm trying to resolve an issue of lost coverage in a `MacroKind::Attr`-based, function-scoped macro. Instead of only searching for a body_span that is "not a function-like macro" (that is, MacroKind::Bang), I'm expanding this to all `MacroKind`s. Maybe I should expand this to `ExpnKind::Desugaring` and `ExpnKind::AstPass` (or subsets, depending on their sub-kinds) as well, but I'm not sure that's a good idea. I'd like to add a test of the `Attr` macro on functions, but I need time to figure out how to constract a good, simple example without external crate dependencies. For the moment, all tests still work as expected (no change), this new commit shouldn't have a negative affect, and more importantly, I believe it will have a positive effect. I will try to confirm this.
2021-05-04 01:21:24 -05:00
^0
LL| |
LL| |
LL| |
LL| |
LL| 1| let _short_unused_closure_block = | _unused_arg: u8 | { println!("not called") };
^0
LL| |
LL| 1| let _shortish_unused_closure = | _unused_arg: u8 | {
LL| 0| println!("not called")
LL| 0| };
LL| |
LL| 1| let _as_short_unused_closure = |
LL| | _unused_arg: u8
LL| 0| | { println!("not called") };
LL| |
LL| 1| let _almost_as_short_unused_closure = |
LL| | _unused_arg: u8
LL| 0| | { println!("not called") }
LL| | ;
LL| |
LL| |
LL| |
LL| |
LL| |
LL| 1| let _short_unused_closure_line_break_no_block = | _unused_arg: u8 |
LL| 0|println!("not called")
LL| | ;
LL| |
LL| 1| let _short_unused_closure_line_break_no_block2 =
LL| | | _unused_arg: u8 |
LL| 0| println!(
LL| 0| "not called"
LL| 0| )
LL| | ;
LL| |
LL| 1| let short_used_not_covered_closure_line_break_no_block_embedded_branch =
LL| | | _unused_arg: u8 |
LL| 0| println!(
LL| 0| "not called: {}",
LL| 0| if is_true { "check" } else { "me" }
LL| 0| )
LL| | ;
LL| |
LL| 1| let short_used_not_covered_closure_line_break_block_embedded_branch =
LL| | | _unused_arg: u8 |
LL| 0| {
LL| 0| println!(
LL| 0| "not called: {}",
LL| 0| if is_true { "check" } else { "me" }
LL| | )
LL| 0| }
LL| | ;
LL| |
LL| 1| let short_used_covered_closure_line_break_no_block_embedded_branch =
LL| | | _unused_arg: u8 |
LL| 1| println!(
LL| 1| "not called: {}",
LL| 1| if is_true { "check" } else { "me" }
Coverage instruments closure bodies in macros (not the macro body) Fixes: #84884 This solution might be considered a compromise, but I think it is the better choice. The results in the `closure.rs` test correctly resolve all test cases broken as described in #84884. One test pattern (in both `closure_macro.rs` and `closure_macro_async.rs`) was also affected, and removes coverage statistics for the lines inside the closure, because the closure includes a macro. (The coverage remains at the callsite of the macro, so we lose some detail, but there isn't a perfect choice with macros. Often macro implementations are split across the macro and the callsite, and there doesn't appear to be a single "right choice" for which body should be covered. For the current implementation, we can't do both. The callsite is most likely to be the preferred site for coverage. I applied this fix to all `MacroKinds`, not just `Bang`. I'm trying to resolve an issue of lost coverage in a `MacroKind::Attr`-based, function-scoped macro. Instead of only searching for a body_span that is "not a function-like macro" (that is, MacroKind::Bang), I'm expanding this to all `MacroKind`s. Maybe I should expand this to `ExpnKind::Desugaring` and `ExpnKind::AstPass` (or subsets, depending on their sub-kinds) as well, but I'm not sure that's a good idea. I'd like to add a test of the `Attr` macro on functions, but I need time to figure out how to constract a good, simple example without external crate dependencies. For the moment, all tests still work as expected (no change), this new commit shouldn't have a negative affect, and more importantly, I believe it will have a positive effect. I will try to confirm this.
2021-05-04 01:21:24 -05:00
^0
LL| 1| )
LL| | ;
LL| |
LL| 1| let short_used_covered_closure_line_break_block_embedded_branch =
LL| | | _unused_arg: u8 |
LL| 1| {
LL| 1| println!(
LL| 1| "not called: {}",
LL| 1| if is_true { "check" } else { "me" }
Coverage instruments closure bodies in macros (not the macro body) Fixes: #84884 This solution might be considered a compromise, but I think it is the better choice. The results in the `closure.rs` test correctly resolve all test cases broken as described in #84884. One test pattern (in both `closure_macro.rs` and `closure_macro_async.rs`) was also affected, and removes coverage statistics for the lines inside the closure, because the closure includes a macro. (The coverage remains at the callsite of the macro, so we lose some detail, but there isn't a perfect choice with macros. Often macro implementations are split across the macro and the callsite, and there doesn't appear to be a single "right choice" for which body should be covered. For the current implementation, we can't do both. The callsite is most likely to be the preferred site for coverage. I applied this fix to all `MacroKinds`, not just `Bang`. I'm trying to resolve an issue of lost coverage in a `MacroKind::Attr`-based, function-scoped macro. Instead of only searching for a body_span that is "not a function-like macro" (that is, MacroKind::Bang), I'm expanding this to all `MacroKind`s. Maybe I should expand this to `ExpnKind::Desugaring` and `ExpnKind::AstPass` (or subsets, depending on their sub-kinds) as well, but I'm not sure that's a good idea. I'd like to add a test of the `Attr` macro on functions, but I need time to figure out how to constract a good, simple example without external crate dependencies. For the moment, all tests still work as expected (no change), this new commit shouldn't have a negative affect, and more importantly, I believe it will have a positive effect. I will try to confirm this.
2021-05-04 01:21:24 -05:00
^0
LL| | )
LL| 1| }
LL| | ;
LL| |
LL| 1| if is_false {
LL| 0| short_used_not_covered_closure_macro(0);
LL| 0| short_used_not_covered_closure_line_break_no_block_embedded_branch(0);
LL| 0| short_used_not_covered_closure_line_break_block_embedded_branch(0);
LL| 1| }
LL| 1| short_used_covered_closure_macro(0);
LL| 1| short_used_covered_closure_line_break_no_block_embedded_branch(0);
LL| 1| short_used_covered_closure_line_break_block_embedded_branch(0);
LL| 1|}
Updates to experimental coverage counter injection This is a combination of 18 commits. Commit #2: Additional examples and some small improvements. Commit #3: fixed mir-opt non-mir extensions and spanview title elements Corrected a fairly recent assumption in runtest.rs that all MIR dump files end in .mir. (It was appending .mir to the graphviz .dot and spanview .html file names when generating blessed output files. That also left outdated files in the baseline alongside the files with the incorrect names, which I've now removed.) Updated spanview HTML title elements to match their content, replacing a hardcoded and incorrect name that was left in accidentally when originally submitted. Commit #4: added more test examples also improved Makefiles with support for non-zero exit status and to force validation of tests unless a specific test overrides it with a specific comment. Commit #5: Fixed rare issues after testing on real-world crate Commit #6: Addressed PR feedback, and removed temporary -Zexperimental-coverage -Zinstrument-coverage once again supports the latest capabilities of LLVM instrprof coverage instrumentation. Also fixed a bug in spanview. Commit #7: Fix closure handling, add tests for closures and inner items And cleaned up other tests for consistency, and to make it more clear where spans start/end by breaking up lines. Commit #8: renamed "typical" test results "expected" Now that the `llvm-cov show` tests are improved to normally expect matching actuals, and to allow individual tests to override that expectation. Commit #9: test coverage of inline generic struct function Commit #10: Addressed review feedback * Removed unnecessary Unreachable filter. * Replaced a match wildcard with remining variants. * Added more comments to help clarify the role of successors() in the CFG traversal Commit #11: refactoring based on feedback * refactored `fn coverage_spans()`. * changed the way I expand an empty coverage span to improve performance * fixed a typo that I had accidently left in, in visit.rs Commit #12: Optimized use of SourceMap and SourceFile Commit #13: Fixed a regression, and synched with upstream Some generated test file names changed due to some new change upstream. Commit #14: Stripping out crate disambiguators from demangled names These can vary depending on the test platform. Commit #15: Ignore llvm-cov show diff on test with generics, expand IO error message Tests with generics produce llvm-cov show results with demangled names that can include an unstable "crate disambiguator" (hex value). The value changes when run in the Rust CI Windows environment. I added a sed filter to strip them out (in a prior commit), but sed also appears to fail in the same environment. Until I can figure out a workaround, I'm just going to ignore this specific test result. I added a FIXME to follow up later, but it's not that critical. I also saw an error with Windows GNU, but the IO error did not specify a path for the directory or file that triggered the error. I updated the error messages to provide more info for next, time but also noticed some other tests with similar steps did not fail. Looks spurious. Commit #16: Modify rust-demangler to strip disambiguators by default Commit #17: Remove std::process::exit from coverage tests Due to Issue #77553, programs that call std::process::exit() do not generate coverage results on Windows MSVC. Commit #18: fix: test file paths exceeding Windows max path len
2020-09-01 18:15:17 -05:00