From f5aebad28fe69f53497ce0107103c6d5d37b25dc Mon Sep 17 00:00:00 2001 From: Rich Kadel Date: Tue, 1 Sep 2020 16:15:17 -0700 Subject: [PATCH] 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 --- Cargo.lock | 3 +- .../src/coverageinfo/mapgen.rs | 1 + .../rustc_codegen_ssa/src/coverageinfo/map.rs | 13 +- .../src/graph/dominators/mod.rs | 9 + compiler/rustc_middle/src/mir/coverage/mod.rs | 19 + compiler/rustc_middle/src/mir/visit.rs | 2 +- .../src/transform/instrument_coverage.rs | 1228 ++++- compiler/rustc_mir/src/util/pretty.rs | 81 +- compiler/rustc_mir/src/util/spanview.rs | 59 +- compiler/rustc_session/src/config.rs | 4 - compiler/rustc_session/src/options.rs | 5 - compiler/rustc_span/src/lib.rs | 84 + compiler/rustc_span/src/source_map.rs | 79 +- src/test/mir-opt/graphviz.main.mir_map.0.dot | 5 +- .../mir-opt/graphviz.main.mir_map.0.dot.mir | 7 - ...ument_coverage.bar.InstrumentCoverage.diff | 2 +- ...ment_coverage.main.InstrumentCoverage.diff | 3 +- .../spanview_block.main.mir_map.0.html | 9 +- .../spanview_statement.main.mir_map.0.html | 11 +- .../spanview_terminator.main.mir_map.0.html | 11 +- .../Makefile | 33 +- .../expected_export_coverage.closure.json | 59 + .../expected_export_coverage.drop_trait.json | 59 + .../expected_export_coverage.generics.json | 59 + .../expected_export_coverage.if.json | 59 + .../expected_export_coverage.if_else.json | 59 + .../expected_export_coverage.inner_items.json | 59 + ...expected_export_coverage.lazy_boolean.json | 59 + ...cted_export_coverage.loop_break_value.json | 59 + ...t_coverage.question_mark_error_result.json | 59 + .../expected_export_coverage.simple_loop.json | 59 + ...expected_export_coverage.simple_match.json | 59 + ...d_export_coverage.various_conditions.json} | 30 +- ...ed_export_coverage.while_early_return.json | 59 + .../expected_show_coverage.closure.txt | 94 + .../expected_show_coverage.drop_trait.txt | 34 + .../expected_show_coverage.generics.txt | 67 + .../expected_show_coverage.if.txt | 29 + .../expected_show_coverage.if_else.txt | 41 + .../expected_show_coverage.inner_items.txt | 58 + .../expected_show_coverage.lazy_boolean.txt | 44 + ...xpected_show_coverage.loop_break_value.txt | 14 + ...ow_coverage.question_mark_error_result.txt | 36 + .../expected_show_coverage.simple_loop.txt | 36 + .../expected_show_coverage.simple_match.txt | 44 + ...ected_show_coverage.various_conditions.txt | 69 + ...ected_show_coverage.while_early_return.txt | 48 + ...ical_show_coverage.coverage_of_if_else.txt | 64 - .../expected_export_coverage.closure.json | 59 + .../expected_export_coverage.drop_trait.json | 59 + .../expected_export_coverage.generics.json | 59 + .../expected_export_coverage.if.json | 59 + .../expected_export_coverage.if_else.json | 59 + .../expected_export_coverage.inner_items.json | 59 + ...expected_export_coverage.lazy_boolean.json | 59 + ...cted_export_coverage.loop_break_value.json | 59 + ...t_coverage.question_mark_error_result.json | 59 + .../expected_export_coverage.simple_loop.json | 59 + ...expected_export_coverage.simple_match.json | 59 + ...d_export_coverage.various_conditions.json} | 30 +- ...ed_export_coverage.while_early_return.json | 59 + .../expected_show_coverage.closure.txt | 94 + .../expected_show_coverage.drop_trait.txt | 34 + .../expected_show_coverage.generics.txt | 67 + .../expected_show_coverage.if.txt | 29 + .../expected_show_coverage.if_else.txt | 41 + .../expected_show_coverage.inner_items.txt | 58 + .../expected_show_coverage.lazy_boolean.txt | 44 + ...xpected_show_coverage.loop_break_value.txt | 14 + ...ow_coverage.question_mark_error_result.txt | 36 + .../expected_show_coverage.simple_loop.txt | 36 + .../expected_show_coverage.simple_match.txt | 44 + ...ected_show_coverage.various_conditions.txt | 69 + ...ected_show_coverage.while_early_return.txt | 48 + ...ical_show_coverage.coverage_of_if_else.txt | 64 - .../Makefile | 2 +- .../Makefile | 7 +- ...osure#0}.-------.InstrumentCoverage.0.html | 82 + ...osure#1}.-------.InstrumentCoverage.0.html | 82 + ...osure#2}.-------.InstrumentCoverage.0.html | 82 + ...osure#3}.-------.InstrumentCoverage.0.html | 82 + ...ure.main.-------.InstrumentCoverage.0.html | 4505 +++++++++++++++++ ...lse.main.-------.InstrumentCoverage.0.html | 641 --- ...ait.main.-------.InstrumentCoverage.0.html | 119 + ...#0}-drop.-------.InstrumentCoverage.0.html | 123 + ...ics.main.-------.InstrumentCoverage.0.html | 167 + ...trength.-------.InstrumentCoverage.0.html} | 21 +- ...#1}-drop.-------.InstrumentCoverage.0.html | 123 + .../if.main.-------.InstrumentCoverage.0.html | 162 + ...lse.main.-------.InstrumentCoverage.0.html | 163 + ...ait_func.-------.InstrumentCoverage.0.html | 83 + ...function.-------.InstrumentCoverage.0.html | 107 + ...it_func.-------.InstrumentCoverage.0.html} | 17 +- ...ems.main.-------.InstrumentCoverage.0.html | 171 + ...ean.main.-------.InstrumentCoverage.0.html | 160 + ...lue.main.-------.InstrumentCoverage.0.html | 118 + ...ult.call.-------.InstrumentCoverage.0.html | 73 + ...ult.main.-------.InstrumentCoverage.0.html | 101 + ...oop.main.-------.InstrumentCoverage.0.html | 127 + ...tch.main.-------.InstrumentCoverage.0.html | 190 + ...ons.main.-------.InstrumentCoverage.0.html | 228 + ...urn.main.-------.InstrumentCoverage.0.html | 119 + .../Makefile | 2 +- ...osure#0}.-------.InstrumentCoverage.0.html | 82 + ...osure#1}.-------.InstrumentCoverage.0.html | 82 + ...osure#2}.-------.InstrumentCoverage.0.html | 82 + ...osure#3}.-------.InstrumentCoverage.0.html | 82 + ...ure.main.-------.InstrumentCoverage.0.html | 4505 +++++++++++++++++ ...lse.main.-------.InstrumentCoverage.0.html | 641 --- ...ait.main.-------.InstrumentCoverage.0.html | 119 + ...#0}-drop.-------.InstrumentCoverage.0.html | 123 + ...ics.main.-------.InstrumentCoverage.0.html | 167 + ...trength.-------.InstrumentCoverage.0.html} | 21 +- ...#1}-drop.-------.InstrumentCoverage.0.html | 123 + .../if.main.-------.InstrumentCoverage.0.html | 162 + ...lse.main.-------.InstrumentCoverage.0.html | 163 + ...ait_func.-------.InstrumentCoverage.0.html | 83 + ...function.-------.InstrumentCoverage.0.html | 107 + ...ait_func.-------.InstrumentCoverage.0.html | 72 + ...ems.main.-------.InstrumentCoverage.0.html | 171 + ...ean.main.-------.InstrumentCoverage.0.html | 160 + ...lue.main.-------.InstrumentCoverage.0.html | 118 + ...ult.call.-------.InstrumentCoverage.0.html | 73 + ...ult.main.-------.InstrumentCoverage.0.html | 101 + ...oop.main.-------.InstrumentCoverage.0.html | 127 + ...tch.main.-------.InstrumentCoverage.0.html | 190 + ...ons.main.-------.InstrumentCoverage.0.html | 228 + ...urn.main.-------.InstrumentCoverage.0.html | 119 + .../instrument-coverage/closure.rs | 93 + .../compiletest-ignore-dir | 2 +- .../instrument-coverage/drop_trait.rs | 33 + .../instrument-coverage/generics.rs | 44 + .../instrument-coverage/if.rs | 28 + .../instrument-coverage/if_else.rs | 40 + .../instrument-coverage/inner_items.rs | 57 + .../instrument-coverage/lazy_boolean.rs | 43 + .../instrument-coverage/loop_break_value.rs | 13 + .../question_mark_error_result.rs | 35 + .../instrument-coverage/simple_loop.rs | 35 + .../instrument-coverage/simple_match.rs | 43 + ...ge_of_if_else.rs => various_conditions.rs} | 28 +- .../instrument-coverage/while_early_return.rs | 47 + src/tools/compiletest/src/runtest.rs | 14 +- src/tools/rust-demangler/Cargo.toml | 3 +- src/tools/rust-demangler/main.rs | 41 +- 145 files changed, 18993 insertions(+), 1864 deletions(-) delete mode 100644 src/test/mir-opt/graphviz.main.mir_map.0.dot.mir create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.closure.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.drop_trait.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.generics.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.if.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.if_else.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.inner_items.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.lazy_boolean.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.loop_break_value.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.question_mark_error_result.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.simple_loop.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.simple_match.json rename src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/{expected_export_coverage.coverage_of_if_else.json => expected_export_coverage.various_conditions.json} (69%) create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.while_early_return.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.closure.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.drop_trait.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.generics.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.if.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.if_else.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.inner_items.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.lazy_boolean.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.loop_break_value.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.question_mark_error_result.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.simple_loop.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.simple_match.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.various_conditions.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.while_early_return.txt delete mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/typical_show_coverage.coverage_of_if_else.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.closure.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.drop_trait.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.generics.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.if.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.if_else.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.inner_items.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.lazy_boolean.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.loop_break_value.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.question_mark_error_result.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.simple_loop.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.simple_match.json rename src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/{expected_export_coverage.coverage_of_if_else.json => expected_export_coverage.various_conditions.json} (69%) create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.while_early_return.json create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.closure.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.drop_trait.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.generics.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.if.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.if_else.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.inner_items.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.lazy_boolean.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.loop_break_value.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.question_mark_error_result.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.simple_loop.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.simple_match.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.various_conditions.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.while_early_return.txt delete mode 100644 src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/typical_show_coverage.coverage_of_if_else.txt create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main-{closure#0}.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main-{closure#1}.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main-{closure#2}.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main-{closure#3}.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main.-------.InstrumentCoverage.0.html delete mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.coverage_of_if_else/coverage_of_if_else.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.drop_trait/drop_trait.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.drop_trait/drop_trait.{impl#0}-drop.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.generics/generics.main.-------.InstrumentCoverage.0.html rename src/test/{mir-opt/spanview_block.main.mir_map.0.html.mir => run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.generics/generics.{impl#0}-set_strength.-------.InstrumentCoverage.0.html} (59%) create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.generics/generics.{impl#1}-drop.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.if/if.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.if_else/if_else.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.inner_items/inner_items.main-InnerTrait-default_trait_func.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.inner_items/inner_items.main-inner_function.-------.InstrumentCoverage.0.html rename src/test/{mir-opt/spanview_terminator.main.mir_map.0.html.mir => run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.inner_items/inner_items.main-{impl#0}-inner_trait_func.-------.InstrumentCoverage.0.html} (53%) create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.inner_items/inner_items.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.lazy_boolean/lazy_boolean.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.loop_break_value/loop_break_value.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.question_mark_error_result/question_mark_error_result.call.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.question_mark_error_result/question_mark_error_result.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.simple_loop/simple_loop.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.simple_match/simple_match.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.various_conditions/various_conditions.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.while_early_return/while_early_return.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main-{closure#0}.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main-{closure#1}.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main-{closure#2}.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main-{closure#3}.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main.-------.InstrumentCoverage.0.html delete mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.coverage_of_if_else/coverage_of_if_else.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.drop_trait/drop_trait.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.drop_trait/drop_trait.{impl#0}-drop.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.generics/generics.main.-------.InstrumentCoverage.0.html rename src/test/{mir-opt/spanview_statement.main.mir_map.0.html.mir => run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.generics/generics.{impl#0}-set_strength.-------.InstrumentCoverage.0.html} (59%) create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.generics/generics.{impl#1}-drop.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.if/if.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.if_else/if_else.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.inner_items/inner_items.main-InnerTrait-default_trait_func.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.inner_items/inner_items.main-inner_function.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.inner_items/inner_items.main-{impl#0}-inner_trait_func.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.inner_items/inner_items.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.lazy_boolean/lazy_boolean.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.loop_break_value/loop_break_value.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.question_mark_error_result/question_mark_error_result.call.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.question_mark_error_result/question_mark_error_result.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.simple_loop/simple_loop.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.simple_match/simple_match.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.various_conditions/various_conditions.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.while_early_return/while_early_return.main.-------.InstrumentCoverage.0.html create mode 100644 src/test/run-make-fulldeps/instrument-coverage/closure.rs create mode 100644 src/test/run-make-fulldeps/instrument-coverage/drop_trait.rs create mode 100644 src/test/run-make-fulldeps/instrument-coverage/generics.rs create mode 100644 src/test/run-make-fulldeps/instrument-coverage/if.rs create mode 100644 src/test/run-make-fulldeps/instrument-coverage/if_else.rs create mode 100644 src/test/run-make-fulldeps/instrument-coverage/inner_items.rs create mode 100644 src/test/run-make-fulldeps/instrument-coverage/lazy_boolean.rs create mode 100644 src/test/run-make-fulldeps/instrument-coverage/loop_break_value.rs create mode 100644 src/test/run-make-fulldeps/instrument-coverage/question_mark_error_result.rs create mode 100644 src/test/run-make-fulldeps/instrument-coverage/simple_loop.rs create mode 100644 src/test/run-make-fulldeps/instrument-coverage/simple_match.rs rename src/test/run-make-fulldeps/instrument-coverage/{coverage_of_if_else.rs => various_conditions.rs} (62%) create mode 100644 src/test/run-make-fulldeps/instrument-coverage/while_early_return.rs diff --git a/Cargo.lock b/Cargo.lock index fd27f05363832..d216b09c66a60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2938,8 +2938,9 @@ dependencies = [ [[package]] name = "rust-demangler" -version = "0.0.0" +version = "0.0.1" dependencies = [ + "regex", "rustc-demangle", ] diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs index ec6c177614d4e..0098555a3736b 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs @@ -126,6 +126,7 @@ impl CoverageMapGenerator { let (filenames_index, _) = self.filenames.insert_full(c_filename); virtual_file_mapping.push(filenames_index as u32); } + debug!("Adding counter {:?} to map for {:?}", counter, region,); mapping_regions.push(CounterMappingRegion::code_region( counter, current_file_id, diff --git a/compiler/rustc_codegen_ssa/src/coverageinfo/map.rs b/compiler/rustc_codegen_ssa/src/coverageinfo/map.rs index 814e43c5fa5e9..d8bde8ee70533 100644 --- a/compiler/rustc_codegen_ssa/src/coverageinfo/map.rs +++ b/compiler/rustc_codegen_ssa/src/coverageinfo/map.rs @@ -143,7 +143,9 @@ impl FunctionCoverage { let id_to_counter = |new_indexes: &IndexVec, id: ExpressionOperandId| { - if id.index() < self.counters.len() { + if id == ExpressionOperandId::ZERO { + Some(Counter::zero()) + } else if id.index() < self.counters.len() { let index = CounterValueReference::from(id.index()); self.counters .get(index) @@ -179,14 +181,19 @@ impl FunctionCoverage { // been assigned a `new_index`. let mapped_expression_index = MappedExpressionIndex::from(counter_expressions.len()); - counter_expressions.push(CounterExpression::new( + let expression = CounterExpression::new( lhs_counter, match op { Op::Add => ExprKind::Add, Op::Subtract => ExprKind::Subtract, }, rhs_counter, - )); + ); + debug!( + "Adding expression {:?} = {:?} at {:?}", + mapped_expression_index, expression, region + ); + counter_expressions.push(expression); new_indexes[original_index] = mapped_expression_index; expression_regions.push((Counter::expression(mapped_expression_index), region)); } diff --git a/compiler/rustc_data_structures/src/graph/dominators/mod.rs b/compiler/rustc_data_structures/src/graph/dominators/mod.rs index 438a0d0c6ff97..1cfbce2355e3a 100644 --- a/compiler/rustc_data_structures/src/graph/dominators/mod.rs +++ b/compiler/rustc_data_structures/src/graph/dominators/mod.rs @@ -9,6 +9,7 @@ use super::iterate::reverse_post_order; use super::ControlFlowGraph; use rustc_index::vec::{Idx, IndexVec}; use std::borrow::BorrowMut; +use std::cmp::Ordering; #[cfg(test)] mod tests; @@ -108,6 +109,14 @@ impl Dominators { // FIXME -- could be optimized by using post-order-rank self.dominators(node).any(|n| n == dom) } + + /// Provide deterministic ordering of nodes such that, if any two nodes have a dominator + /// relationship, the dominator will always precede the dominated. (The relative ordering + /// of two unrelated nodes will also be consistent, but otherwise the order has no + /// meaning.) This method cannot be used to determine if either Node dominates the other. + pub fn rank_partial_cmp(&self, lhs: Node, rhs: Node) -> Option { + self.post_order_rank[lhs].partial_cmp(&self.post_order_rank[rhs]) + } } pub struct Iter<'dom, Node: Idx> { diff --git a/compiler/rustc_middle/src/mir/coverage/mod.rs b/compiler/rustc_middle/src/mir/coverage/mod.rs index ce311c2ee52bd..0421eabc2dc05 100644 --- a/compiler/rustc_middle/src/mir/coverage/mod.rs +++ b/compiler/rustc_middle/src/mir/coverage/mod.rs @@ -14,6 +14,20 @@ rustc_index::newtype_index! { } } +impl ExpressionOperandId { + /// An expression operand for a "zero counter", as described in the following references: + /// + /// * https://github.com/rust-lang/llvm-project/blob/llvmorg-8.0.0/llvm/docs/CoverageMappingFormat.rst#counter + /// * https://github.com/rust-lang/llvm-project/blob/llvmorg-8.0.0/llvm/docs/CoverageMappingFormat.rst#tag + /// * https://github.com/rust-lang/llvm-project/blob/llvmorg-8.0.0/llvm/docs/CoverageMappingFormat.rst#counter-expressions + /// + /// This operand can be used to count two or more separate code regions with a single counter, + /// if they run sequentially with no branches, by injecting the `Counter` in a `BasicBlock` for + /// one of the code regions, and inserting `CounterExpression`s ("add ZERO to the counter") in + /// the coverage map for the other code regions. + pub const ZERO: Self = Self::from_u32(0); +} + rustc_index::newtype_index! { pub struct CounterValueReference { derive [HashStable] @@ -22,6 +36,11 @@ rustc_index::newtype_index! { } } +impl CounterValueReference { + // Counters start at 1 to reserve 0 for ExpressionOperandId::ZERO. + pub const START: Self = Self::from_u32(1); +} + rustc_index::newtype_index! { pub struct InjectedExpressionIndex { derive [HashStable] diff --git a/compiler/rustc_middle/src/mir/visit.rs b/compiler/rustc_middle/src/mir/visit.rs index a008bd5f75fa0..c1f8d22c2c670 100644 --- a/compiler/rustc_middle/src/mir/visit.rs +++ b/compiler/rustc_middle/src/mir/visit.rs @@ -752,7 +752,7 @@ macro_rules! make_mir_visitor { } fn super_coverage(&mut self, - _kind: & $($mutability)? Coverage, + _coverage: & $($mutability)? Coverage, _location: Location) { } diff --git a/compiler/rustc_mir/src/transform/instrument_coverage.rs b/compiler/rustc_mir/src/transform/instrument_coverage.rs index 388fb90651c5f..babe10a0f14da 100644 --- a/compiler/rustc_mir/src/transform/instrument_coverage.rs +++ b/compiler/rustc_mir/src/transform/instrument_coverage.rs @@ -1,26 +1,34 @@ use crate::transform::MirPass; use crate::util::pretty; -use crate::util::spanview::{ - source_range_no_file, statement_kind_name, terminator_kind_name, write_spanview_document, - SpanViewable, TOOLTIP_INDENT, -}; +use crate::util::spanview::{self, SpanViewable}; use rustc_data_structures::fingerprint::Fingerprint; +use rustc_data_structures::graph::dominators::Dominators; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; +use rustc_data_structures::sync::Lrc; use rustc_index::bit_set::BitSet; +use rustc_index::vec::IndexVec; use rustc_middle::hir; +use rustc_middle::hir::map::blocks::FnLikeNode; use rustc_middle::ich::StableHashingContext; use rustc_middle::mir; use rustc_middle::mir::coverage::*; use rustc_middle::mir::visit::Visitor; use rustc_middle::mir::{ - BasicBlock, BasicBlockData, Coverage, CoverageInfo, Location, Statement, StatementKind, - TerminatorKind, + AggregateKind, BasicBlock, BasicBlockData, Coverage, CoverageInfo, FakeReadCause, Location, + Rvalue, SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, }; use rustc_middle::ty::query::Providers; use rustc_middle::ty::TyCtxt; use rustc_span::def_id::DefId; -use rustc_span::{FileName, Pos, RealFileName, Span, Symbol}; +use rustc_span::source_map::original_sp; +use rustc_span::{ + BytePos, CharPos, FileName, Pos, RealFileName, SourceFile, Span, Symbol, SyntaxContext, +}; + +use std::cmp::Ordering; + +const ID_SEPARATOR: &str = ","; /// Inserts `StatementKind::Coverage` statements that either instrument the binary with injected /// counters, via intrinsic `llvm.instrprof.increment`, and/or inject metadata used during codegen @@ -33,6 +41,21 @@ pub(crate) fn provide(providers: &mut Providers) { providers.coverageinfo = |tcx, def_id| coverageinfo_from_mir(tcx, def_id); } +/// The `num_counters` argument to `llvm.instrprof.increment` is the max counter_id + 1, or in +/// other words, the number of counter value references injected into the MIR (plus 1 for the +/// reserved `ZERO` counter, which uses counter ID `0` when included in an expression). Injected +/// counters have a counter ID from `1..num_counters-1`. +/// +/// `num_expressions` is the number of counter expressions added to the MIR body. +/// +/// Both `num_counters` and `num_expressions` are used to initialize new vectors, during backend +/// code generate, to lookup counters and expressions by simple u32 indexes. +/// +/// MIR optimization may split and duplicate some BasicBlock sequences, or optimize out some code +/// including injected counters. (It is OK if some counters are optimized out, but those counters +/// are still included in the total `num_counters` or `num_expressions`.) Simply counting the +/// calls may not work; but computing the number of counters or expressions by adding `1` to the +/// highest ID (for a given instrumented function) is valid. struct CoverageVisitor { info: CoverageInfo, } @@ -57,15 +80,6 @@ impl Visitor<'_> for CoverageVisitor { fn coverageinfo_from_mir<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> CoverageInfo { let mir_body = tcx.optimized_mir(def_id); - // The `num_counters` argument to `llvm.instrprof.increment` is the number of injected - // counters, with each counter having a counter ID from `0..num_counters-1`. MIR optimization - // may split and duplicate some BasicBlock sequences. Simply counting the calls may not - // work; but computing the num_counters by adding `1` to the highest counter_id (for a given - // instrumented function) is valid. - // - // `num_expressions` is the number of counter expressions added to the MIR body. Both - // `num_counters` and `num_expressions` are used to initialize new vectors, during backend - // code generate, to lookup counters and expressions by simple u32 indexes. let mut coverage_visitor = CoverageVisitor { info: CoverageInfo { num_counters: 0, num_expressions: 0 } }; @@ -77,25 +91,399 @@ impl<'tcx> MirPass<'tcx> for InstrumentCoverage { fn run_pass(&self, tcx: TyCtxt<'tcx>, mir_body: &mut mir::Body<'tcx>) { // If the InstrumentCoverage pass is called on promoted MIRs, skip them. // See: https://github.com/rust-lang/rust/pull/73011#discussion_r438317601 - if mir_body.source.promoted.is_none() { - Instrumentor::new(&self.name(), tcx, mir_body).inject_counters(); + if mir_body.source.promoted.is_some() { + trace!( + "InstrumentCoverage skipped for {:?} (already promoted for Miri evaluation)", + mir_body.source.def_id() + ); + return; } + + let hir_id = tcx.hir().local_def_id_to_hir_id(mir_body.source.def_id().expect_local()); + let is_fn_like = FnLikeNode::from_node(tcx.hir().get(hir_id)).is_some(); + + // Only instrument functions, methods, and closures (not constants since they are evaluated + // at compile time by Miri). + // FIXME(#73156): Handle source code coverage in const eval + if !is_fn_like { + trace!( + "InstrumentCoverage skipped for {:?} (not an FnLikeNode)", + mir_body.source.def_id(), + ); + return; + } + // FIXME(richkadel): By comparison, the MIR pass `ConstProp` includes associated constants, + // with functions, methods, and closures. I assume Miri is used for associated constants as + // well. If not, we may need to include them here too. + + trace!("InstrumentCoverage starting for {:?}", mir_body.source.def_id()); + Instrumentor::new(&self.name(), tcx, mir_body).inject_counters(); + trace!("InstrumentCoverage starting for {:?}", mir_body.source.def_id()); } } -#[derive(Clone)] -struct CoverageRegion { - pub span: Span, +/// A BasicCoverageBlock (BCB) represents the maximal-length sequence of CFG (MIR) BasicBlocks +/// without conditional branches. +/// +/// The BCB allows coverage analysis to be performed on a simplified projection of the underlying +/// MIR CFG, without altering the original CFG. Note that running the MIR `SimplifyCfg` transform, +/// is not sufficient, and therefore not necessary, since the BCB-based CFG projection is a more +/// aggressive simplification. For example: +/// +/// * The BCB CFG projection ignores (trims) branches not relevant to coverage, such as unwind- +/// related code that is injected by the Rust compiler but has no physical source code to +/// count. This also means a BasicBlock with a `Call` terminator can be merged into its +/// primary successor target block, in the same BCB. +/// * Some BasicBlock terminators support Rust-specific concerns--like borrow-checking--that are +/// not relevant to coverage analysis. `FalseUnwind`, for example, can be treated the same as +/// a `Goto`, and merged with its successor into the same BCB. +/// +/// Each BCB with at least one computed `CoverageSpan` will have no more than one `Counter`. +/// In some cases, a BCB's execution count can be computed by `CounterExpression`. Additional +/// disjoint `CoverageSpan`s in a BCB can also be counted by `CounterExpression` (by adding `ZERO` +/// to the BCB's primary counter or expression). +/// +/// Dominator/dominated relationships (which are fundamental to the coverage analysis algorithm) +/// between two BCBs can be computed using the `mir::Body` `dominators()` with any `BasicBlock` +/// member of each BCB. (For consistency, BCB's use the first `BasicBlock`, also referred to as the +/// `bcb_leader_bb`.) +/// +/// The BCB CFG projection is critical to simplifying the coverage analysis by ensuring graph +/// path-based queries (`is_dominated_by()`, `predecessors`, `successors`, etc.) have branch +/// (control flow) significance. +#[derive(Debug, Clone)] +struct BasicCoverageBlock { pub blocks: Vec, } +impl BasicCoverageBlock { + pub fn leader_bb(&self) -> BasicBlock { + self.blocks[0] + } + + pub fn id(&self) -> String { + format!( + "@{}", + self.blocks + .iter() + .map(|bb| bb.index().to_string()) + .collect::>() + .join(ID_SEPARATOR) + ) + } +} + +struct BasicCoverageBlocks { + vec: IndexVec>, +} + +impl BasicCoverageBlocks { + pub fn from_mir(mir_body: &mir::Body<'tcx>) -> Self { + let mut basic_coverage_blocks = + BasicCoverageBlocks { vec: IndexVec::from_elem_n(None, mir_body.basic_blocks().len()) }; + basic_coverage_blocks.extract_from_mir(mir_body); + basic_coverage_blocks + } + + pub fn iter(&self) -> impl Iterator { + self.vec.iter().filter_map(|option| option.as_ref()) + } + + fn extract_from_mir(&mut self, mir_body: &mir::Body<'tcx>) { + // Traverse the CFG but ignore anything following an `unwind` + let cfg_without_unwind = ShortCircuitPreorder::new(mir_body, |term_kind| { + let mut successors = term_kind.successors(); + match &term_kind { + // SwitchInt successors are never unwind, and all of them should be traversed. + + // NOTE: TerminatorKind::FalseEdge targets from SwitchInt don't appear to be + // helpful in identifying unreachable code. I did test the theory, but the following + // changes were not beneficial. (I assumed that replacing some constants with + // non-deterministic variables might effect which blocks were targeted by a + // `FalseEdge` `imaginary_target`. It did not.) + // + // Also note that, if there is a way to identify BasicBlocks that are part of the + // MIR CFG, but not actually reachable, here are some other things to consider: + // + // Injecting unreachable code regions will probably require computing the set + // difference between the basic blocks found without filtering out unreachable + // blocks, and the basic blocks found with the filter; then computing the + // `CoverageSpans` without the filter; and then injecting `Counter`s or + // `CounterExpression`s for blocks that are not unreachable, or injecting + // `Unreachable` code regions otherwise. This seems straightforward, but not + // trivial. + // + // Alternatively, we might instead want to leave the unreachable blocks in + // (bypass the filter here), and inject the counters. This will result in counter + // values of zero (0) for unreachable code (and, notably, the code will be displayed + // with a red background by `llvm-cov show`). + // + // TerminatorKind::SwitchInt { .. } => { + // let some_imaginary_target = successors.clone().find_map(|&successor| { + // let term = mir_body.basic_blocks()[successor].terminator(); + // if let TerminatorKind::FalseEdge { imaginary_target, .. } = term.kind { + // if mir_body.predecessors()[imaginary_target].len() == 1 { + // return Some(imaginary_target); + // } + // } + // None + // }); + // if let Some(imaginary_target) = some_imaginary_target { + // box successors.filter(move |&&successor| successor != imaginary_target) + // } else { + // box successors + // } + // } + // + // Note this also required changing the closure signature for the + // `ShortCurcuitPreorder` to: + // + // F: Fn(&'tcx TerminatorKind<'tcx>) -> Box + 'a>, + TerminatorKind::SwitchInt { .. } => successors, + + // For all other kinds, return only the first successor, if any, and ignore unwinds + _ => successors.next().into_iter().chain(&[]), + } + }); + + // Walk the CFG using a Preorder traversal, which starts from `START_BLOCK` and follows + // each block terminator's `successors()`. Coverage spans must map to actual source code, + // so compiler generated blocks and paths can be ignored. To that end the CFG traversal + // intentionally omits unwind paths. + let mut blocks = Vec::new(); + for (bb, data) in cfg_without_unwind { + if let Some(last) = blocks.last() { + let predecessors = &mir_body.predecessors()[bb]; + if predecessors.len() > 1 || !predecessors.contains(last) { + // The `bb` has more than one _incoming_ edge, and should start its own + // `BasicCoverageBlock`. (Note, the `blocks` vector does not yet include `bb`; + // it contains a sequence of one or more sequential blocks with no intermediate + // branches in or out. Save these as a new `BasicCoverageBlock` before starting + // the new one.) + self.add_basic_coverage_block(blocks.split_off(0)); + debug!( + " because {}", + if predecessors.len() > 1 { + "predecessors.len() > 1".to_owned() + } else { + format!("bb {} is not in precessors: {:?}", bb.index(), predecessors) + } + ); + } + } + blocks.push(bb); + + let term = data.terminator(); + + match term.kind { + TerminatorKind::Return { .. } + | TerminatorKind::Abort + | TerminatorKind::Assert { .. } + | TerminatorKind::Yield { .. } + | TerminatorKind::SwitchInt { .. } => { + // The `bb` has more than one _outgoing_ edge, or exits the function. Save the + // current sequence of `blocks` gathered to this point, as a new + // `BasicCoverageBlock`. + self.add_basic_coverage_block(blocks.split_off(0)); + debug!(" because term.kind = {:?}", term.kind); + // Note that this condition is based on `TerminatorKind`, even though it + // theoretically boils down to `successors().len() != 1`; that is, either zero + // (e.g., `Return`, `Abort`) or multiple successors (e.g., `SwitchInt`), but + // since the Coverage graph (the BCB CFG projection) ignores things like unwind + // branches (which exist in the `Terminator`s `successors()` list) checking the + // number of successors won't work. + } + TerminatorKind::Goto { .. } + | TerminatorKind::Resume + | TerminatorKind::Unreachable + | TerminatorKind::Drop { .. } + | TerminatorKind::DropAndReplace { .. } + | TerminatorKind::Call { .. } + | TerminatorKind::GeneratorDrop + | TerminatorKind::FalseEdge { .. } + | TerminatorKind::FalseUnwind { .. } + | TerminatorKind::InlineAsm { .. } => {} + } + } + + if !blocks.is_empty() { + // process any remaining blocks into a final `BasicCoverageBlock` + self.add_basic_coverage_block(blocks.split_off(0)); + debug!(" because the end of the CFG was reached while traversing"); + } + } + + fn add_basic_coverage_block(&mut self, blocks: Vec) { + let leader_bb = blocks[0]; + let bcb = BasicCoverageBlock { blocks }; + debug!("adding BCB: {:?}", bcb); + self.vec[leader_bb] = Some(bcb); + } +} + +impl std::ops::Index for BasicCoverageBlocks { + type Output = BasicCoverageBlock; + + fn index(&self, index: BasicBlock) -> &Self::Output { + self.vec[index].as_ref().expect("is_some if BasicBlock is a BasicCoverageBlock leader") + } +} + +#[derive(Debug, Copy, Clone)] +enum CoverageStatement { + Statement(BasicBlock, Span, usize), + Terminator(BasicBlock, Span), +} + +impl CoverageStatement { + pub fn format(&self, tcx: TyCtxt<'tcx>, mir_body: &'a mir::Body<'tcx>) -> String { + match *self { + Self::Statement(bb, span, stmt_index) => { + let stmt = &mir_body.basic_blocks()[bb].statements[stmt_index]; + format!( + "{}: @{}[{}]: {:?}", + spanview::source_range_no_file(tcx, &span), + bb.index(), + stmt_index, + stmt + ) + } + Self::Terminator(bb, span) => { + let term = mir_body.basic_blocks()[bb].terminator(); + format!( + "{}: @{}.{}: {:?}", + spanview::source_range_no_file(tcx, &span), + bb.index(), + term_type(&term.kind), + term.kind + ) + } + } + } + + pub fn span(&self) -> &Span { + match self { + Self::Statement(_, span, _) | Self::Terminator(_, span) => span, + } + } +} + +fn term_type(kind: &TerminatorKind<'tcx>) -> &'static str { + match kind { + TerminatorKind::Goto { .. } => "Goto", + TerminatorKind::SwitchInt { .. } => "SwitchInt", + TerminatorKind::Resume => "Resume", + TerminatorKind::Abort => "Abort", + TerminatorKind::Return => "Return", + TerminatorKind::Unreachable => "Unreachable", + TerminatorKind::Drop { .. } => "Drop", + TerminatorKind::DropAndReplace { .. } => "DropAndReplace", + TerminatorKind::Call { .. } => "Call", + TerminatorKind::Assert { .. } => "Assert", + TerminatorKind::Yield { .. } => "Yield", + TerminatorKind::GeneratorDrop => "GeneratorDrop", + TerminatorKind::FalseEdge { .. } => "FalseEdge", + TerminatorKind::FalseUnwind { .. } => "FalseUnwind", + TerminatorKind::InlineAsm { .. } => "InlineAsm", + } +} + +/// A BCB is deconstructed into one or more `Span`s. Each `Span` maps to a `CoverageSpan` that +/// references the originating BCB and one or more MIR `Statement`s and/or `Terminator`s. +/// Initially, the `Span`s come from the `Statement`s and `Terminator`s, but subsequent +/// transforms can combine adjacent `Span`s and `CoverageSpan` from the same BCB, merging the +/// `CoverageStatement` vectors, and the `Span`s to cover the extent of the combined `Span`s. +/// +/// Note: A `CoverageStatement` merged into another CoverageSpan may come from a `BasicBlock` that +/// is not part of the `CoverageSpan` bcb if the statement was included because it's `Span` matches +/// or is subsumed by the `Span` associated with this `CoverageSpan`, and it's `BasicBlock` +/// `is_dominated_by()` the `BasicBlock`s in this `CoverageSpan`. +#[derive(Debug, Clone)] +struct CoverageSpan { + span: Span, + bcb_leader_bb: BasicBlock, + coverage_statements: Vec, + is_closure: bool, +} + +impl CoverageSpan { + pub fn for_statement( + statement: &Statement<'tcx>, + span: Span, + bcb: &BasicCoverageBlock, + bb: BasicBlock, + stmt_index: usize, + ) -> Self { + let is_closure = match statement.kind { + StatementKind::Assign(box ( + _, + Rvalue::Aggregate(box AggregateKind::Closure(_, _), _), + )) => true, + _ => false, + }; + + Self { + span, + bcb_leader_bb: bcb.leader_bb(), + coverage_statements: vec![CoverageStatement::Statement(bb, span, stmt_index)], + is_closure, + } + } + + pub fn for_terminator(span: Span, bcb: &'a BasicCoverageBlock, bb: BasicBlock) -> Self { + Self { + span, + bcb_leader_bb: bcb.leader_bb(), + coverage_statements: vec![CoverageStatement::Terminator(bb, span)], + is_closure: false, + } + } + + pub fn merge_from(&mut self, mut other: CoverageSpan) { + debug_assert!(self.is_mergeable(&other)); + self.span = self.span.to(other.span); + if other.is_closure { + self.is_closure = true; + } + self.coverage_statements.append(&mut other.coverage_statements); + } + + pub fn cutoff_statements_at(&mut self, cutoff_pos: BytePos) { + self.coverage_statements.retain(|covstmt| covstmt.span().hi() <= cutoff_pos); + if let Some(highest_covstmt) = + self.coverage_statements.iter().max_by_key(|covstmt| covstmt.span().hi()) + { + self.span = self.span.with_hi(highest_covstmt.span().hi()); + } + } + + pub fn is_dominated_by( + &self, + other: &CoverageSpan, + dominators: &Dominators, + ) -> bool { + debug_assert!(!self.is_in_same_bcb(other)); + dominators.is_dominated_by(self.bcb_leader_bb, other.bcb_leader_bb) + } + + pub fn is_mergeable(&self, other: &Self) -> bool { + self.is_in_same_bcb(other) && !(self.is_closure || other.is_closure) + } + + pub fn is_in_same_bcb(&self, other: &Self) -> bool { + self.bcb_leader_bb == other.bcb_leader_bb + } +} + struct Instrumentor<'a, 'tcx> { pass_name: &'a str, tcx: TyCtxt<'tcx>, mir_body: &'a mut mir::Body<'tcx>, hir_body: &'tcx rustc_hir::Body<'tcx>, + dominators: Option>, + basic_coverage_blocks: Option, function_source_hash: Option, - num_counters: u32, + next_counter_id: u32, num_expressions: u32, } @@ -107,17 +495,19 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> { tcx, mir_body, hir_body, + dominators: None, + basic_coverage_blocks: None, function_source_hash: None, - num_counters: 0, + next_counter_id: CounterValueReference::START.as_u32(), num_expressions: 0, } } - /// Counter IDs start from zero and go up. + /// Counter IDs start from one and go up. fn next_counter(&mut self) -> CounterValueReference { - assert!(self.num_counters < u32::MAX - self.num_expressions); - let next = self.num_counters; - self.num_counters += 1; + assert!(self.next_counter_id < u32::MAX - self.num_expressions); + let next = self.next_counter_id; + self.next_counter_id += 1; CounterValueReference::from(next) } @@ -125,12 +515,22 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> { /// (add or subtract counts) of both Counter regions and CounterExpression regions. The counter /// expression operand IDs must be unique across both types. fn next_expression(&mut self) -> InjectedExpressionIndex { - assert!(self.num_counters < u32::MAX - self.num_expressions); + assert!(self.next_counter_id < u32::MAX - self.num_expressions); let next = u32::MAX - self.num_expressions; self.num_expressions += 1; InjectedExpressionIndex::from(next) } + fn dominators(&self) -> &Dominators { + self.dominators.as_ref().expect("dominators must be initialized before calling") + } + + fn basic_coverage_blocks(&self) -> &BasicCoverageBlocks { + self.basic_coverage_blocks + .as_ref() + .expect("basic_coverage_blocks must be initialized before calling") + } + fn function_source_hash(&mut self) -> u64 { match self.function_source_hash { Some(hash) => hash, @@ -144,86 +544,61 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> { fn inject_counters(&mut self) { let tcx = self.tcx; + let source_map = tcx.sess.source_map(); let def_id = self.mir_body.source.def_id(); let mir_body = &self.mir_body; - let body_span = self.hir_body.value.span; - debug!( - "instrumenting {:?}, span: {}", - def_id, - tcx.sess.source_map().span_to_string(body_span) - ); - - if !tcx.sess.opts.debugging_opts.experimental_coverage { - // Coverage at the function level should be accurate. This is the default implementation - // if `-Z experimental-coverage` is *NOT* enabled. - let block = rustc_middle::mir::START_BLOCK; - let counter = self.make_counter(); - self.inject_statement(counter, body_span, block); - return; - } - // FIXME(richkadel): else if `-Z experimental-coverage` *IS* enabled: Efforts are still in - // progress to identify the correct code region spans and associated counters to generate - // accurate Rust coverage reports. - - let block_span = |data: &BasicBlockData<'tcx>| { - // The default span will be the `Terminator` span; but until we have a smarter solution, - // the coverage region also incorporates at least the statements in this BasicBlock as - // well. Extend the span to encompass all, if possible. - // FIXME(richkadel): Assuming the terminator's span is already known to be contained in `body_span`. - let mut span = data.terminator().source_info.span; - // FIXME(richkadel): It's looking unlikely that we should compute a span from MIR - // spans, but if we do keep something like this logic, we will need a smarter way - // to combine `Statement`s and/or `Terminator`s with `Span`s from different - // files. - for statement_span in data.statements.iter().map(|statement| statement.source_info.span) - { - // Only combine Spans from the function's body_span. - if body_span.contains(statement_span) { - span = span.to(statement_span); - } - } - span + let body_span = self.body_span(); + let source_file = source_map.lookup_source_file(body_span.lo()); + let file_name = match &source_file.name { + FileName::Real(RealFileName::Named(path)) => Symbol::intern(&path.to_string_lossy()), + _ => bug!( + "source_file.name should be a RealFileName, but it was: {:?}", + source_file.name + ), }; - // Traverse the CFG but ignore anything following an `unwind` - let cfg_without_unwind = ShortCircuitPreorder::new(mir_body, |term_kind| { - let mut successors = term_kind.successors(); - match &term_kind { - // SwitchInt successors are never unwind, and all of them should be traversed - TerminatorKind::SwitchInt { .. } => successors, - // For all other kinds, return only the first successor, if any, and ignore unwinds - _ => successors.next().into_iter().chain(&[]), - } - }); + debug!("instrumenting {:?}, span: {}", def_id, source_map.span_to_string(body_span)); - let mut coverage_regions = Vec::with_capacity(cfg_without_unwind.size_hint().0); - for (bb, data) in cfg_without_unwind { - if !body_span.contains(data.terminator().source_info.span) { - continue; - } + self.dominators.replace(mir_body.dominators()); + self.basic_coverage_blocks.replace(BasicCoverageBlocks::from_mir(mir_body)); - // FIXME(richkadel): Regions will soon contain multiple blocks. - let mut blocks = Vec::new(); - blocks.push(bb); - let span = block_span(data); - coverage_regions.push(CoverageRegion { span, blocks }); - } + let coverage_spans = self.coverage_spans(); let span_viewables = if pretty::dump_enabled(tcx, self.pass_name, def_id) { - Some(self.span_viewables(&coverage_regions)) + Some(self.span_viewables(&coverage_spans)) } else { None }; - // Inject counters for the selected spans - for CoverageRegion { span, blocks } in coverage_regions { - debug!( - "Injecting counter at: {:?}:\n{}\n==========", - span, - tcx.sess.source_map().span_to_snippet(span).expect("Error getting source for span"), - ); - let counter = self.make_counter(); - self.inject_statement(counter, span, blocks[0]); + // Inject a counter for each `CoverageSpan`. There can be multiple `CoverageSpan`s for a + // given BCB, but only one actual counter needs to be incremented per BCB. `bb_counters` + // maps each `bcb_leader_bb` to its `Counter`, when injected. Subsequent `CoverageSpan`s + // for a BCB that already has a `Counter` will inject a `CounterExpression` instead, and + // compute its value by adding `ZERO` to the BCB `Counter` value. + let mut bb_counters = IndexVec::from_elem_n(None, mir_body.basic_blocks().len()); + for CoverageSpan { span, bcb_leader_bb: bb, .. } in coverage_spans { + if let Some(&counter_operand) = bb_counters[bb].as_ref() { + let expression = + self.make_expression(counter_operand, Op::Add, ExpressionOperandId::ZERO); + debug!( + "Injecting counter expression {:?} at: {:?}:\n{}\n==========", + expression, + span, + source_map.span_to_snippet(span).expect("Error getting source for span"), + ); + self.inject_statement(file_name, &source_file, expression, span, bb); + } else { + let counter = self.make_counter(); + debug!( + "Injecting counter {:?} at: {:?}:\n{}\n==========", + counter, + span, + source_map.span_to_snippet(span).expect("Error getting source for span"), + ); + let counter_operand = counter.as_operand_id(); + bb_counters[bb] = Some(counter_operand); + self.inject_statement(file_name, &source_file, counter, span, bb); + } } if let Some(span_viewables) = span_viewables { @@ -236,36 +611,12 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> { self.mir_body.source, ) .expect("Unexpected error creating MIR spanview HTML file"); - write_spanview_document(tcx, def_id, span_viewables, &mut file) + let crate_name = tcx.crate_name(def_id.krate); + let item_name = tcx.def_path(def_id).to_filename_friendly_no_crate(); + let title = format!("{}.{} - Coverage Spans", crate_name, item_name); + spanview::write_document(tcx, def_id, span_viewables, &title, &mut file) .expect("Unexpected IO error dumping coverage spans as HTML"); } - - // FIXME(richkadel): Some regions will be counted by "counter expression". Counter - // expressions are supported, but are not yet generated. When they are, remove this `fake_use` - // block. - let fake_use = false; - if fake_use { - let add = false; - let fake_counter = CoverageKind::Counter { - function_source_hash: self.function_source_hash(), - id: CounterValueReference::from_u32(1), - }; - let fake_expression = CoverageKind::Expression { - id: InjectedExpressionIndex::from(u32::MAX - 1), - lhs: ExpressionOperandId::from_u32(1), - op: Op::Add, - rhs: ExpressionOperandId::from_u32(2), - }; - - let lhs = fake_counter.as_operand_id(); - let op = if add { Op::Add } else { Op::Subtract }; - let rhs = fake_expression.as_operand_id(); - - let block = rustc_middle::mir::START_BLOCK; - - let expression = self.make_expression(lhs, op, rhs); - self.inject_statement(expression, body_span, block); - } } fn make_counter(&mut self) -> CoverageKind { @@ -284,8 +635,15 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> { CoverageKind::Expression { id: self.next_expression(), lhs, op, rhs } } - fn inject_statement(&mut self, coverage_kind: CoverageKind, span: Span, block: BasicBlock) { - let code_region = make_code_region(self.tcx, &span); + fn inject_statement( + &mut self, + file_name: Symbol, + source_file: &Lrc, + coverage_kind: CoverageKind, + span: Span, + block: BasicBlock, + ) { + let code_region = make_code_region(file_name, source_file, span); debug!(" injecting statement {:?} covering {:?}", coverage_kind, code_region); let data = &mut self.mir_body[block]; @@ -297,112 +655,548 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> { data.statements.push(statement); } - /// Converts the computed `CoverageRegion`s into `SpanViewable`s. - fn span_viewables(&self, coverage_regions: &Vec) -> Vec { + /// Converts the computed `BasicCoverageBlock`s into `SpanViewable`s. + fn span_viewables(&self, coverage_spans: &Vec) -> Vec { + let tcx = self.tcx; + let mir_body = &self.mir_body; let mut span_viewables = Vec::new(); - for coverage_region in coverage_regions { - span_viewables.push(SpanViewable { - span: coverage_region.span, - id: format!("{}", coverage_region.blocks[0].index()), - tooltip: self.make_tooltip_text(coverage_region), + for coverage_span in coverage_spans { + let bcb = self.bcb_from_coverage_span(coverage_span); + let CoverageSpan { span, bcb_leader_bb: bb, coverage_statements, .. } = coverage_span; + let id = bcb.id(); + let mut sorted_coverage_statements = coverage_statements.clone(); + sorted_coverage_statements.sort_unstable_by_key(|covstmt| match *covstmt { + CoverageStatement::Statement(bb, _, index) => (bb, index), + CoverageStatement::Terminator(bb, _) => (bb, usize::MAX), }); + let tooltip = sorted_coverage_statements + .iter() + .map(|covstmt| covstmt.format(tcx, mir_body)) + .collect::>() + .join("\n"); + span_viewables.push(SpanViewable { bb: *bb, span: *span, id, tooltip }); } span_viewables } - /// A custom tooltip renderer used in a spanview HTML+CSS document used for coverage analysis. - fn make_tooltip_text(&self, coverage_region: &CoverageRegion) -> String { - const INCLUDE_COVERAGE_STATEMENTS: bool = false; - let tcx = self.tcx; - let source_map = tcx.sess.source_map(); - let mut text = Vec::new(); - for (i, &bb) in coverage_region.blocks.iter().enumerate() { - if i > 0 { - text.push("\n".to_owned()); + #[inline(always)] + fn bcb_from_coverage_span(&self, coverage_span: &CoverageSpan) -> &BasicCoverageBlock { + &self.basic_coverage_blocks()[coverage_span.bcb_leader_bb] + } + + #[inline(always)] + fn body_span(&self) -> Span { + self.hir_body.value.span + } + + // Generate a set of `CoverageSpan`s from the filtered set of `Statement`s and `Terminator`s of + // the `BasicBlock`(s) in the given `BasicCoverageBlock`. One `CoverageSpan` is generated for + // each `Statement` and `Terminator`. (Note that subsequent stages of coverage analysis will + // merge some `CoverageSpan`s, at which point a `CoverageSpan` may represent multiple + // `Statement`s and/or `Terminator`s.) + fn extract_spans(&self, bcb: &'a BasicCoverageBlock) -> Vec { + let body_span = self.body_span(); + let mir_basic_blocks = self.mir_body.basic_blocks(); + bcb.blocks + .iter() + .map(|bbref| { + let bb = *bbref; + let data = &mir_basic_blocks[bb]; + data.statements + .iter() + .enumerate() + .filter_map(move |(index, statement)| { + filtered_statement_span(statement, body_span).map(|span| { + CoverageSpan::for_statement(statement, span, bcb, bb, index) + }) + }) + .chain( + filtered_terminator_span(data.terminator(), body_span) + .map(|span| CoverageSpan::for_terminator(span, bcb, bb)), + ) + }) + .flatten() + .collect() + } + + /// Generate a minimal set of `CoverageSpan`s, each representing a contiguous code region to be + /// counted. + /// + /// The basic steps are: + /// + /// 1. Extract an initial set of spans from the `Statement`s and `Terminator`s of each + /// `BasicCoverageBlock`. + /// 2. Sort the spans by span.lo() (starting position). Spans that start at the same position + /// are sorted with longer spans before shorter spans; and equal spans are sorted + /// (deterministically) based on "dominator" relationship (if any). + /// 3. Traverse the spans in sorted order to identify spans that can be dropped (for instance, + /// if another span or spans are already counting the same code region), or should be merged + /// into a broader combined span (because it represents a contiguous, non-branching, and + /// uninterrupted region of source code). + /// + /// Closures are exposed in their enclosing functions as `Assign` `Rvalue`s, and since + /// closures have their own MIR, their `Span` in their enclosing function should be left + /// "uncovered". + /// + /// Note the resulting vector of `CoverageSpan`s does may not be fully sorted (and does not need + /// to be). + fn coverage_spans(&self) -> Vec { + let mut initial_spans = + Vec::::with_capacity(self.mir_body.basic_blocks().len() * 2); + for bcb in self.basic_coverage_blocks().iter() { + for coverage_span in self.extract_spans(bcb) { + initial_spans.push(coverage_span); } - text.push(format!("{:?}: {}:", bb, &source_map.span_to_string(coverage_region.span))); - let data = &self.mir_body.basic_blocks()[bb]; - for statement in &data.statements { - let statement_string = match statement.kind { - StatementKind::Coverage(box ref coverage) => match coverage.kind { - CoverageKind::Counter { id, .. } => { - if !INCLUDE_COVERAGE_STATEMENTS { - continue; - } - format!("increment counter #{}", id.index()) - } - CoverageKind::Expression { id, lhs, op, rhs } => { - if !INCLUDE_COVERAGE_STATEMENTS { - continue; - } - format!( - "expression #{} = {} {} {}", - id.index(), - lhs.index(), - if op == Op::Add { "+" } else { "-" }, - rhs.index() - ) - } - CoverageKind::Unreachable => { - if !INCLUDE_COVERAGE_STATEMENTS { - continue; - } - String::from("unreachable") - } - }, - _ => format!("{:?}", statement), - }; - let source_range = source_range_no_file(tcx, &statement.source_info.span); - text.push(format!( - "\n{}{}: {}: {}", - TOOLTIP_INDENT, - source_range, - statement_kind_name(statement), - statement_string - )); + } + + if initial_spans.is_empty() { + // This can happen if, for example, the function is unreachable (contains only a + // `BasicBlock`(s) with an `Unreachable` terminator). + return initial_spans; + } + + initial_spans.sort_unstable_by(|a, b| { + if a.span.lo() == b.span.lo() { + if a.span.hi() == b.span.hi() { + if a.is_in_same_bcb(b) { + Some(Ordering::Equal) + } else { + // Sort equal spans by dominator relationship, in reverse order (so + // dominators always come after the dominated equal spans). When later + // comparing two spans in order, the first will either dominate the second, + // or they will have no dominator relationship. + self.dominators().rank_partial_cmp(b.bcb_leader_bb, a.bcb_leader_bb) + } + } else { + // Sort hi() in reverse order so shorter spans are attempted after longer spans. + // This guarantees that, if a `prev` span overlaps, and is not equal to, a `curr` + // span, the prev span either extends further left of the curr span, or they + // start at the same position and the prev span extends further right of the end + // of the curr span. + b.span.hi().partial_cmp(&a.span.hi()) + } + } else { + a.span.lo().partial_cmp(&b.span.lo()) } - let term = data.terminator(); - let source_range = source_range_no_file(tcx, &term.source_info.span); - text.push(format!( - "\n{}{}: {}: {:?}", - TOOLTIP_INDENT, - source_range, - terminator_kind_name(term), - term.kind - )); + .unwrap() + }); + + let refinery = CoverageSpanRefinery::from_sorted_spans(initial_spans, self.dominators()); + refinery.to_refined_spans() + } +} + +struct CoverageSpanRefinery<'a> { + sorted_spans_iter: std::vec::IntoIter, + dominators: &'a Dominators, + some_curr: Option, + curr_original_span: Span, + some_prev: Option, + prev_original_span: Span, + pending_dups: Vec, + refined_spans: Vec, +} + +impl<'a> CoverageSpanRefinery<'a> { + fn from_sorted_spans( + sorted_spans: Vec, + dominators: &'a Dominators, + ) -> Self { + let refined_spans = Vec::with_capacity(sorted_spans.len()); + let mut sorted_spans_iter = sorted_spans.into_iter(); + let prev = sorted_spans_iter.next().expect("at least one span"); + let prev_original_span = prev.span; + Self { + sorted_spans_iter, + dominators, + refined_spans, + some_curr: None, + curr_original_span: Span::with_root_ctxt(BytePos(0), BytePos(0)), + some_prev: Some(prev), + prev_original_span, + pending_dups: Vec::new(), + } + } + + /// Iterate through the sorted `CoverageSpan`s, and return the refined list of merged and + /// de-duplicated `CoverageSpan`s. + fn to_refined_spans(mut self) -> Vec { + while self.next_coverage_span() { + if self.curr().is_mergeable(self.prev()) { + debug!(" same bcb (and neither is a closure), merge with prev={:?}", self.prev()); + let prev = self.take_prev(); + self.curr_mut().merge_from(prev); + // Note that curr.span may now differ from curr_original_span + } else if self.prev_ends_before_curr() { + debug!( + " different bcbs and disjoint spans, so keep curr for next iter, and add \ + prev={:?}", + self.prev() + ); + let prev = self.take_prev(); + self.add_refined_span(prev); + } else if self.prev().is_closure { + // drop any equal or overlapping span (`curr`) and keep `prev` to test again in the + // next iter + debug!( + " curr overlaps a closure (prev). Drop curr and keep prev for next iter. \ + prev={:?}", + self.prev() + ); + self.discard_curr(); + } else if self.curr().is_closure { + self.carve_out_span_for_closure(); + } else if self.prev_original_span == self.curr().span { + self.hold_pending_dups_unless_dominated(); + } else { + self.cutoff_prev_at_overlapping_curr(); + } + } + debug!(" AT END, adding last prev={:?}", self.prev()); + let pending_dups = self.pending_dups.split_off(0); + for dup in pending_dups.into_iter() { + debug!(" ...adding at least one pending dup={:?}", dup); + self.add_refined_span(dup); + } + let prev = self.take_prev(); + self.add_refined_span(prev); + + // FIXME(richkadel): Replace some counters with expressions if they can be calculated based + // on branching. (For example, one branch of a SwitchInt can be computed from the counter + // for the CoverageSpan just prior to the SwitchInt minus the sum of the counters of all + // other branches). + + self.to_refined_spans_without_closures() + } + + fn add_refined_span(&mut self, coverage_span: CoverageSpan) { + self.refined_spans.push(coverage_span); + } + + /// Remove `CoverageSpan`s derived from closures, originally added to ensure the coverage + /// regions for the current function leave room for the closure's own coverage regions + /// (injected separately, from the closure's own MIR). + fn to_refined_spans_without_closures(mut self) -> Vec { + self.refined_spans.retain(|covspan| !covspan.is_closure); + self.refined_spans + } + + fn curr(&self) -> &CoverageSpan { + self.some_curr + .as_ref() + .unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_curr")) + } + + fn curr_mut(&mut self) -> &mut CoverageSpan { + self.some_curr + .as_mut() + .unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_curr")) + } + + fn prev(&self) -> &CoverageSpan { + self.some_prev + .as_ref() + .unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_prev")) + } + + fn prev_mut(&mut self) -> &mut CoverageSpan { + self.some_prev + .as_mut() + .unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_prev")) + } + + fn take_prev(&mut self) -> CoverageSpan { + self.some_prev.take().unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_prev")) + } + + /// If there are `pending_dups` but `prev` is not a matching dup (`prev.span` doesn't match the + /// `pending_dups` spans), then one of the following two things happened during the previous + /// iteration: + /// * the `span` of prev was modified (by `curr_mut().merge_from(prev)`); or + /// * the `span` of prev advanced past the end of the span of pending_dups + /// (`prev().span.hi() <= curr().span.lo()`) + /// In either case, no more spans will match the span of `pending_dups`, so + /// add the `pending_dups` if they don't overlap `curr`, and clear the list. + fn check_pending_dups(&mut self) { + if let Some(dup) = self.pending_dups.last() { + if dup.span != self.prev().span { + debug!( + " SAME spans, but pending_dups are NOT THE SAME, so BCBs matched on \ + previous iteration, or prev started a new disjoint span" + ); + if dup.span.hi() <= self.curr().span.lo() { + let pending_dups = self.pending_dups.split_off(0); + for dup in pending_dups.into_iter() { + debug!(" ...adding at least one pending={:?}", dup); + self.add_refined_span(dup); + } + } else { + self.pending_dups.clear(); + } + } + } + } + + /// Advance `prev` to `curr` (if any), and `curr` to the next `CoverageSpan` in sorted order. + fn next_coverage_span(&mut self) -> bool { + if let Some(curr) = self.some_curr.take() { + self.some_prev = Some(curr); + self.prev_original_span = self.curr_original_span; + } + while let Some(curr) = self.sorted_spans_iter.next() { + debug!("FOR curr={:?}", curr); + if self.prev_starts_after_next(&curr) { + debug!( + " prev.span starts after curr.span, so curr will be dropped (skipping past \ + closure?); prev={:?}", + self.prev() + ); + } else { + // Save a copy of the original span for `curr` in case the `CoverageSpan` is changed + // by `self.curr_mut().merge_from(prev)`. + self.curr_original_span = curr.span; + self.some_curr.replace(curr); + self.check_pending_dups(); + return true; + } + } + false + } + + /// If called, then the next call to `next_coverage_span()` will *not* update `prev` with the + /// `curr` coverage span. + fn discard_curr(&mut self) { + self.some_curr = None; + } + + /// Returns true if the curr span should be skipped because prev has already advanced beyond the + /// end of curr. This can only happen if a prior iteration updated `prev` to skip past a region + /// of code, such as skipping past a closure. + fn prev_starts_after_next(&self, next_curr: &CoverageSpan) -> bool { + self.prev().span.lo() > next_curr.span.lo() + } + + /// Returns true if the curr span starts past the end of the prev span, which means they don't + /// overlap, so we now know the prev can be added to the refined coverage spans. + fn prev_ends_before_curr(&self) -> bool { + self.prev().span.hi() <= self.curr().span.lo() + } + + /// If `prev`s span extends left of the closure (`curr`), carve out the closure's + /// span from `prev`'s span. (The closure's coverage counters will be injected when + /// processing the closure's own MIR.) Add the portion of the span to the left of the + /// closure; and if the span extends to the right of the closure, update `prev` to + /// that portion of the span. For any `pending_dups`, repeat the same process. + fn carve_out_span_for_closure(&mut self) { + let curr_span = self.curr().span; + let left_cutoff = curr_span.lo(); + let right_cutoff = curr_span.hi(); + let has_pre_closure_span = self.prev().span.lo() < right_cutoff; + let has_post_closure_span = self.prev().span.hi() > right_cutoff; + let mut pending_dups = self.pending_dups.split_off(0); + if has_pre_closure_span { + let mut pre_closure = self.prev().clone(); + pre_closure.span = pre_closure.span.with_hi(left_cutoff); + debug!(" prev overlaps a closure. Adding span for pre_closure={:?}", pre_closure); + if !pending_dups.is_empty() { + for mut dup in pending_dups.iter().cloned() { + dup.span = dup.span.with_hi(left_cutoff); + debug!(" ...and at least one pre_closure dup={:?}", dup); + self.add_refined_span(dup); + } + } + self.add_refined_span(pre_closure); + } + if has_post_closure_span { + // Update prev.span to start after the closure (and discard curr) + self.prev_mut().span = self.prev().span.with_lo(right_cutoff); + self.prev_original_span = self.prev().span; + for dup in pending_dups.iter_mut() { + dup.span = dup.span.with_lo(right_cutoff); + } + self.pending_dups.append(&mut pending_dups); + self.discard_curr(); // since self.prev() was already updated + } else { + pending_dups.clear(); + } + } + + /// When two `CoverageSpan`s have the same `Span`, dominated spans can be discarded; but if + /// neither `CoverageSpan` dominates the other, both (or possibly more than two) are held, + /// until their disposition is determined. In this latter case, the `prev` dup is moved into + /// `pending_dups` so the new `curr` dup can be moved to `prev` for the next iteration. + fn hold_pending_dups_unless_dominated(&mut self) { + // equal coverage spans are ordered by dominators before dominated (if any) + debug_assert!(!self.prev().is_dominated_by(self.curr(), self.dominators)); + + if self.curr().is_dominated_by(&self.prev(), self.dominators) { + // If one span dominates the other, assocate the span with the dominator only. + // + // For example: + // match somenum { + // x if x < 1 => { ... } + // }... + // The span for the first `x` is referenced by both the pattern block (every + // time it is evaluated) and the arm code (only when matched). The counter + // will be applied only to the dominator block. + // + // The dominator's (`prev`) execution count may be higher than the dominated + // block's execution count, so drop `curr`. + debug!( + " different bcbs but SAME spans, and prev dominates curr. Drop curr and \ + keep prev for next iter. prev={:?}", + self.prev() + ); + self.discard_curr(); + } else { + // Save `prev` in `pending_dups`. (`curr` will become `prev` in the next iteration.) + // If the `curr` CoverageSpan is later discarded, `pending_dups` can be discarded as + // well; but if `curr` is added to refined_spans, the `pending_dups` will also be added. + debug!( + " different bcbs but SAME spans, and neither dominates, so keep curr for \ + next iter, and, pending upcoming spans (unless overlapping) add prev={:?}", + self.prev() + ); + let prev = self.take_prev(); + self.pending_dups.push(prev); + } + } + + /// `curr` overlaps `prev`. If `prev`s span extends left of `curr`s span, keep _only_ + /// statements that end before `curr.lo()` (if any), and add the portion of the + /// combined span for those statements. Any other statements have overlapping spans + /// that can be ignored because `curr` and/or other upcoming statements/spans inside + /// the overlap area will produce their own counters. This disambiguation process + /// avoids injecting multiple counters for overlapping spans, and the potential for + /// double-counting. + fn cutoff_prev_at_overlapping_curr(&mut self) { + debug!( + " different bcbs, overlapping spans, so ignore/drop pending and only add prev \ + if it has statements that end before curr={:?}", + self.prev() + ); + if self.pending_dups.is_empty() { + let curr_span = self.curr().span; + self.prev_mut().cutoff_statements_at(curr_span.lo()); + if self.prev().coverage_statements.is_empty() { + debug!(" ... no non-overlapping statements to add"); + } else { + debug!(" ... adding modified prev={:?}", self.prev()); + let prev = self.take_prev(); + self.add_refined_span(prev); + } + } else { + // with `pending_dups`, `prev` cannot have any statements that don't overlap + self.pending_dups.clear(); } - text.join("") } } +fn filtered_statement_span(statement: &'a Statement<'tcx>, body_span: Span) -> Option { + match statement.kind { + // These statements have spans that are often outside the scope of the executed source code + // for their parent `BasicBlock`. + StatementKind::StorageLive(_) + | StatementKind::StorageDead(_) + // Coverage should not be encountered, but don't inject coverage coverage + | StatementKind::Coverage(_) + // Ignore `Nop`s + | StatementKind::Nop => None, + + // FIXME(richkadel): Look into a possible issue assigning the span to a + // FakeReadCause::ForGuardBinding, in this example: + // match somenum { + // x if x < 1 => { ... } + // }... + // The BasicBlock within the match arm code included one of these statements, but the span + // for it covered the `1` in this source. The actual statements have nothing to do with that + // source span: + // FakeRead(ForGuardBinding, _4); + // where `_4` is: + // _4 = &_1; (at the span for the first `x`) + // and `_1` is the `Place` for `somenum`. + // + // The arm code BasicBlock already has its own assignment for `x` itself, `_3 = 1`, and I've + // decided it's reasonable for that span (even though outside the arm code) to be part of + // the counted coverage of the arm code execution, but I can't justify including the literal + // `1` in the arm code. I'm pretty sure that, if the `FakeRead(ForGuardBinding)` has a + // purpose in codegen, it's probably in the right BasicBlock, but if so, the `Statement`s + // `source_info.span` can't be right. + // + // Consider correcting the span assignment, assuming there is a better solution, and see if + // the following pattern can be removed here: + StatementKind::FakeRead(cause, _) if cause == FakeReadCause::ForGuardBinding => None, + + // Retain spans from all other statements + StatementKind::FakeRead(_, _) // Not including `ForGuardBinding` + | StatementKind::Assign(_) + | StatementKind::SetDiscriminant { .. } + | StatementKind::LlvmInlineAsm(_) + | StatementKind::Retag(_, _) + | StatementKind::AscribeUserType(_, _) => { + Some(source_info_span(&statement.source_info, body_span)) + } + } +} + +fn filtered_terminator_span(terminator: &'a Terminator<'tcx>, body_span: Span) -> Option { + match terminator.kind { + // These terminators have spans that don't positively contribute to computing a reasonable + // span of actually executed source code. (For example, SwitchInt terminators extracted from + // an `if condition { block }` has a span that includes the executed block, if true, + // but for coverage, the code region executed, up to *and* through the SwitchInt, + // actually stops before the if's block.) + TerminatorKind::Unreachable // Unreachable blocks are not connected to the CFG + | TerminatorKind::Assert { .. } + | TerminatorKind::Drop { .. } + | TerminatorKind::DropAndReplace { .. } + | TerminatorKind::SwitchInt { .. } + | TerminatorKind::Goto { .. } + // For `FalseEdge`, only the `real` branch is taken, so it is similar to a `Goto`. + | TerminatorKind::FalseEdge { .. } => None, + + // Retain spans from all other terminators + TerminatorKind::Resume + | TerminatorKind::Abort + | TerminatorKind::Return + | TerminatorKind::Call { .. } + | TerminatorKind::Yield { .. } + | TerminatorKind::GeneratorDrop + | TerminatorKind::FalseUnwind { .. } + | TerminatorKind::InlineAsm { .. } => { + Some(source_info_span(&terminator.source_info, body_span)) + } + } +} + +#[inline(always)] +fn source_info_span(source_info: &SourceInfo, body_span: Span) -> Span { + let span = original_sp(source_info.span, body_span).with_ctxt(SyntaxContext::root()); + if body_span.contains(span) { span } else { body_span } +} + /// Convert the Span into its file name, start line and column, and end line and column -fn make_code_region<'tcx>(tcx: TyCtxt<'tcx>, span: &Span) -> CodeRegion { - let source_map = tcx.sess.source_map(); - let start = source_map.lookup_char_pos(span.lo()); - let end = if span.hi() == span.lo() { - start.clone() +fn make_code_region(file_name: Symbol, source_file: &Lrc, span: Span) -> CodeRegion { + let (start_line, mut start_col) = source_file.lookup_file_pos(span.lo()); + let (end_line, end_col) = if span.hi() == span.lo() { + let (end_line, mut end_col) = (start_line, start_col); + // Extend an empty span by one character so the region will be counted. + let CharPos(char_pos) = start_col; + if char_pos > 0 { + start_col = CharPos(char_pos - 1); + } else { + end_col = CharPos(char_pos + 1); + } + (end_line, end_col) } else { - let end = source_map.lookup_char_pos(span.hi()); - debug_assert_eq!( - start.file.name, - end.file.name, - "Region start ({:?} -> {:?}) and end ({:?} -> {:?}) don't come from the same source file!", - span.lo(), - start, - span.hi(), - end - ); - end + source_file.lookup_file_pos(span.hi()) }; - match &start.file.name { - FileName::Real(RealFileName::Named(path)) => CodeRegion { - file_name: Symbol::intern(&path.to_string_lossy()), - start_line: start.line as u32, - start_col: start.col.to_u32() + 1, - end_line: end.line as u32, - end_col: end.col.to_u32() + 1, - }, - _ => bug!("start.file.name should be a RealFileName, but it was: {:?}", start.file.name), + CodeRegion { + file_name, + start_line: start_line as u32, + start_col: start_col.to_u32() + 1, + end_line: end_line as u32, + end_col: end_col.to_u32() + 1, } } diff --git a/compiler/rustc_mir/src/util/pretty.rs b/compiler/rustc_mir/src/util/pretty.rs index c00c3b740edb3..3a83fe11d0a33 100644 --- a/compiler/rustc_mir/src/util/pretty.rs +++ b/compiler/rustc_mir/src/util/pretty.rs @@ -150,26 +150,31 @@ fn dump_matched_mir_node<'tcx, F>( if let Some(spanview) = tcx.sess.opts.debugging_opts.dump_mir_spanview { let _: io::Result<()> = try { - let mut file = - create_dump_file(tcx, "html", pass_num, pass_name, disambiguator, body.source)?; + let file_basename = + dump_file_basename(tcx, pass_num, pass_name, disambiguator, body.source); + let mut file = create_dump_file_with_basename(tcx, &file_basename, "html")?; if body.source.def_id().is_local() { - write_mir_fn_spanview(tcx, body, spanview, &mut file)?; + write_mir_fn_spanview( + tcx, + body, + spanview, + &file_basename, + &mut file, + )?; } }; } } -/// Returns the path to the filename where we should dump a given MIR. -/// Also used by other bits of code (e.g., NLL inference) that dump -/// graphviz data or other things. -fn dump_path( +/// Returns the file basename portion (without extension) of a filename path +/// where we should dump a MIR representation output files. +fn dump_file_basename( tcx: TyCtxt<'_>, - extension: &str, pass_num: Option<&dyn Display>, pass_name: &str, disambiguator: &dyn Display, source: MirSource<'tcx>, -) -> PathBuf { +) -> String { let promotion_id = match source.promoted { Some(id) => format!("-{:?}", id), None => String::new(), @@ -184,9 +189,6 @@ fn dump_path( } }; - let mut file_path = PathBuf::new(); - file_path.push(Path::new(&tcx.sess.opts.debugging_opts.dump_mir_dir)); - let crate_name = tcx.crate_name(source.def_id().krate); let item_name = tcx.def_path(source.def_id()).to_filename_friendly_no_crate(); // All drop shims have the same DefId, so we have to add the type @@ -206,23 +208,46 @@ fn dump_path( _ => String::new(), }; - let file_name = format!( - "{}.{}{}{}{}.{}.{}.{}", - crate_name, - item_name, - shim_disambiguator, - promotion_id, - pass_num, - pass_name, - disambiguator, - extension, - ); + format!( + "{}.{}{}{}{}.{}.{}", + crate_name, item_name, shim_disambiguator, promotion_id, pass_num, pass_name, disambiguator, + ) +} + +/// Returns the path to the filename where we should dump a given MIR. +/// Also used by other bits of code (e.g., NLL inference) that dump +/// graphviz data or other things. +fn dump_path(tcx: TyCtxt<'_>, basename: &str, extension: &str) -> PathBuf { + let mut file_path = PathBuf::new(); + file_path.push(Path::new(&tcx.sess.opts.debugging_opts.dump_mir_dir)); + + let file_name = format!("{}.{}", basename, extension,); file_path.push(&file_name); file_path } +/// Attempts to open the MIR dump file with the given name and extension. +fn create_dump_file_with_basename( + tcx: TyCtxt<'_>, + file_basename: &str, + extension: &str, +) -> io::Result> { + let file_path = dump_path(tcx, file_basename, extension); + if let Some(parent) = file_path.parent() { + fs::create_dir_all(parent).map_err(|e| { + io::Error::new( + e.kind(), + format!("IO error creating MIR dump directory: {:?}; {}", parent, e), + ) + })?; + } + Ok(io::BufWriter::new(fs::File::create(&file_path).map_err(|e| { + io::Error::new(e.kind(), format!("IO error creating MIR dump file: {:?}; {}", file_path, e)) + })?)) +} + /// Attempts to open a file where we should dump a given MIR or other /// bit of MIR-related data. Used by `mir-dump`, but also by other /// bits of code (e.g., NLL inference) that dump graphviz data or @@ -235,11 +260,11 @@ pub(crate) fn create_dump_file( disambiguator: &dyn Display, source: MirSource<'tcx>, ) -> io::Result> { - let file_path = dump_path(tcx, extension, pass_num, pass_name, disambiguator, source); - if let Some(parent) = file_path.parent() { - fs::create_dir_all(parent)?; - } - Ok(io::BufWriter::new(fs::File::create(&file_path)?)) + create_dump_file_with_basename( + tcx, + &dump_file_basename(tcx, pass_num, pass_name, disambiguator, source), + extension, + ) } /// Write out a human-readable textual representation for the given MIR. diff --git a/compiler/rustc_mir/src/util/spanview.rs b/compiler/rustc_mir/src/util/spanview.rs index fdc724178b68c..d3ef8c64565c6 100644 --- a/compiler/rustc_mir/src/util/spanview.rs +++ b/compiler/rustc_mir/src/util/spanview.rs @@ -16,9 +16,13 @@ const ANNOTATION_RIGHT_BRACKET: char = '\u{2989}'; // Unicode `Z NOTATION LEFT B const NEW_LINE_SPAN: &str = "\n"; const HEADER: &str = r#" - - coverage_of_if_else - Code Regions - - -"#; - -const FOOTER: &str = r#" - -"#; +"#; /// Metadata to highlight the span of a MIR BasicBlock, Statement, or Terminator. +#[derive(Clone, Debug)] pub struct SpanViewable { + pub bb: BasicBlock, pub span: Span, pub id: String, pub tooltip: String, @@ -92,6 +92,7 @@ pub fn write_mir_fn_spanview<'tcx, W>( tcx: TyCtxt<'tcx>, body: &Body<'tcx>, spanview: MirSpanview, + title: &str, w: &mut W, ) -> io::Result<()> where @@ -126,16 +127,17 @@ where } } } - write_spanview_document(tcx, def_id, span_viewables, w)?; + write_document(tcx, def_id, span_viewables, title, w)?; Ok(()) } /// Generate a spanview HTML+CSS document for the given local function `def_id`, and a pre-generated /// list `SpanViewable`s. -pub fn write_spanview_document<'tcx, W>( +pub fn write_document<'tcx, W>( tcx: TyCtxt<'tcx>, def_id: DefId, mut span_viewables: Vec, + title: &str, w: &mut W, ) -> io::Result<()> where @@ -153,6 +155,9 @@ where source_map.span_to_snippet(fn_span).expect("function should have printable source") ); writeln!(w, "{}", HEADER)?; + writeln!(w, "{}", title)?; + writeln!(w, "{}", STYLE_SECTION)?; + writeln!(w, "{}", START_BODY)?; write!( w, r#"
{}"#, @@ -182,6 +187,7 @@ where end_pos.to_usize(), ordered_viewables.len() ); + let curr_id = &ordered_viewables[0].id; let (next_from_pos, next_ordered_viewables) = write_next_viewable_with_overlaps( tcx, from_pos, @@ -204,13 +210,17 @@ where from_pos = next_from_pos; if next_ordered_viewables.len() != ordered_viewables.len() { ordered_viewables = next_ordered_viewables; - alt = !alt; + if let Some(next_ordered_viewable) = ordered_viewables.first() { + if &next_ordered_viewable.id != curr_id { + alt = !alt; + } + } } } if from_pos < end_pos { write_coverage_gap(tcx, from_pos, end_pos, w)?; } - write!(w, r#"
"#)?; + writeln!(w, r#"
"#)?; writeln!(w, "{}", FOOTER)?; Ok(()) } @@ -273,7 +283,7 @@ fn statement_span_viewable<'tcx>( } let id = format!("{}[{}]", bb.index(), i); let tooltip = tooltip(tcx, &id, span, vec![statement.clone()], &None); - Some(SpanViewable { span, id, tooltip }) + Some(SpanViewable { bb, span, id, tooltip }) } fn terminator_span_viewable<'tcx>( @@ -289,7 +299,7 @@ fn terminator_span_viewable<'tcx>( } let id = format!("{}:{}", bb.index(), terminator_kind_name(term)); let tooltip = tooltip(tcx, &id, span, vec![], &data.terminator); - Some(SpanViewable { span, id, tooltip }) + Some(SpanViewable { bb, span, id, tooltip }) } fn block_span_viewable<'tcx>( @@ -304,7 +314,7 @@ fn block_span_viewable<'tcx>( } let id = format!("{}", bb.index()); let tooltip = tooltip(tcx, &id, span, data.statements.clone(), &data.terminator); - Some(SpanViewable { span, id, tooltip }) + Some(SpanViewable { bb, span, id, tooltip }) } fn compute_block_span<'tcx>(data: &BasicBlockData<'tcx>, body_span: Span) -> Span { @@ -456,6 +466,7 @@ where remaining_viewables.len() ); // Write the overlaps (and the overlaps' overlaps, if any) up to `to_pos`. + let curr_id = &remaining_viewables[0].id; let (next_from_pos, next_remaining_viewables) = write_next_viewable_with_overlaps( tcx, from_pos, @@ -480,7 +491,11 @@ where from_pos = next_from_pos; if next_remaining_viewables.len() != remaining_viewables.len() { remaining_viewables = next_remaining_viewables; - subalt = !subalt; + if let Some(next_ordered_viewable) = remaining_viewables.first() { + if &next_ordered_viewable.id != curr_id { + subalt = !subalt; + } + } } } if from_pos <= viewable.span.hi() { @@ -649,8 +664,12 @@ fn fn_span<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Span { tcx.hir().local_def_id_to_hir_id(def_id.as_local().expect("expected DefId is local")); let fn_decl_span = tcx.hir().span(hir_id); let body_span = hir_body(tcx, def_id).value.span; - debug_assert_eq!(fn_decl_span.ctxt(), body_span.ctxt()); - fn_decl_span.to(body_span) + if fn_decl_span.ctxt() == body_span.ctxt() { + fn_decl_span.to(body_span) + } else { + // This probably occurs for functions defined via macros + body_span + } } fn hir_body<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx rustc_hir::Body<'tcx> { diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index ab96b0333f43f..231e315a22f88 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -1756,10 +1756,6 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { ); } - if debugging_opts.experimental_coverage { - debugging_opts.instrument_coverage = true; - } - if debugging_opts.instrument_coverage { if cg.profile_generate.enabled() || cg.profile_use.is_some() { early_error( diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index b705ab6d9313a..a106007c274d3 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -895,11 +895,6 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options, all statements)."), emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED], "emit a section containing stack size metadata (default: no)"), - experimental_coverage: bool = (false, parse_bool, [TRACKED], - "enable and extend the `-Z instrument-coverage` function-level coverage \ - feature, adding additional experimental (likely inaccurate) counters and \ - code regions (used by `rustc` compiler developers to test new coverage \ - counter placements) (default: no)"), fewer_names: bool = (false, parse_bool, [TRACKED], "reduce memory use by retaining fewer names within compilation artifacts (LLVM-IR) \ (default: no)"), diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 96a6956a40c54..e7cb8cb6e8876 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -60,6 +60,8 @@ use md5::Md5; use sha1::Digest; use sha1::Sha1; +use tracing::debug; + #[cfg(test)] mod tests; @@ -1462,6 +1464,88 @@ impl SourceFile { BytePos::from_u32(pos.0 - self.start_pos.0 + diff) } + + /// Converts an absolute `BytePos` to a `CharPos` relative to the `SourceFile`. + pub fn bytepos_to_file_charpos(&self, bpos: BytePos) -> CharPos { + // The number of extra bytes due to multibyte chars in the `SourceFile`. + let mut total_extra_bytes = 0; + + for mbc in self.multibyte_chars.iter() { + debug!("{}-byte char at {:?}", mbc.bytes, mbc.pos); + if mbc.pos < bpos { + // Every character is at least one byte, so we only + // count the actual extra bytes. + total_extra_bytes += mbc.bytes as u32 - 1; + // We should never see a byte position in the middle of a + // character. + assert!(bpos.to_u32() >= mbc.pos.to_u32() + mbc.bytes as u32); + } else { + break; + } + } + + assert!(self.start_pos.to_u32() + total_extra_bytes <= bpos.to_u32()); + CharPos(bpos.to_usize() - self.start_pos.to_usize() - total_extra_bytes as usize) + } + + /// Looks up the file's (1-based) line number and (0-based `CharPos`) column offset, for a + /// given `BytePos`. + pub fn lookup_file_pos(&self, pos: BytePos) -> (usize, CharPos) { + let chpos = self.bytepos_to_file_charpos(pos); + match self.lookup_line(pos) { + Some(a) => { + let line = a + 1; // Line numbers start at 1 + let linebpos = self.lines[a]; + let linechpos = self.bytepos_to_file_charpos(linebpos); + let col = chpos - linechpos; + debug!("byte pos {:?} is on the line at byte pos {:?}", pos, linebpos); + debug!("char pos {:?} is on the line at char pos {:?}", chpos, linechpos); + debug!("byte is on line: {}", line); + assert!(chpos >= linechpos); + (line, col) + } + None => (0, chpos), + } + } + + /// Looks up the file's (1-based) line number, (0-based `CharPos`) column offset, and (0-based) + /// column offset when displayed, for a given `BytePos`. + pub fn lookup_file_pos_with_col_display(&self, pos: BytePos) -> (usize, CharPos, usize) { + let (line, col_or_chpos) = self.lookup_file_pos(pos); + if line > 0 { + let col = col_or_chpos; + let linebpos = self.lines[line - 1]; + let col_display = { + let start_width_idx = self + .non_narrow_chars + .binary_search_by_key(&linebpos, |x| x.pos()) + .unwrap_or_else(|x| x); + let end_width_idx = self + .non_narrow_chars + .binary_search_by_key(&pos, |x| x.pos()) + .unwrap_or_else(|x| x); + let special_chars = end_width_idx - start_width_idx; + let non_narrow: usize = self.non_narrow_chars[start_width_idx..end_width_idx] + .iter() + .map(|x| x.width()) + .sum(); + col.0 - special_chars + non_narrow + }; + (line, col, col_display) + } else { + let chpos = col_or_chpos; + let col_display = { + let end_width_idx = self + .non_narrow_chars + .binary_search_by_key(&pos, |x| x.pos()) + .unwrap_or_else(|x| x); + let non_narrow: usize = + self.non_narrow_chars[0..end_width_idx].iter().map(|x| x.width()).sum(); + chpos.0 - end_width_idx + non_narrow + }; + (0, chpos, col_display) + } + } } /// Normalizes the source code and records the normalizations. diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index 37596b8ef6fca..fdb031fd9b3d3 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -428,58 +428,22 @@ impl SourceMap { } } + /// Return the SourceFile that contains the given `BytePos` + pub fn lookup_source_file(&self, pos: BytePos) -> Lrc { + let idx = self.lookup_source_file_idx(pos); + (*self.files.borrow().source_files)[idx].clone() + } + /// Looks up source information about a `BytePos`. pub fn lookup_char_pos(&self, pos: BytePos) -> Loc { - let chpos = self.bytepos_to_file_charpos(pos); - match self.lookup_line(pos) { - Ok(SourceFileAndLine { sf: f, line: a }) => { - let line = a + 1; // Line numbers start at 1 - let linebpos = f.lines[a]; - let linechpos = self.bytepos_to_file_charpos(linebpos); - let col = chpos - linechpos; - - let col_display = { - let start_width_idx = f - .non_narrow_chars - .binary_search_by_key(&linebpos, |x| x.pos()) - .unwrap_or_else(|x| x); - let end_width_idx = f - .non_narrow_chars - .binary_search_by_key(&pos, |x| x.pos()) - .unwrap_or_else(|x| x); - let special_chars = end_width_idx - start_width_idx; - let non_narrow: usize = f.non_narrow_chars[start_width_idx..end_width_idx] - .iter() - .map(|x| x.width()) - .sum(); - col.0 - special_chars + non_narrow - }; - debug!("byte pos {:?} is on the line at byte pos {:?}", pos, linebpos); - debug!("char pos {:?} is on the line at char pos {:?}", chpos, linechpos); - debug!("byte is on line: {}", line); - assert!(chpos >= linechpos); - Loc { file: f, line, col, col_display } - } - Err(f) => { - let col_display = { - let end_width_idx = f - .non_narrow_chars - .binary_search_by_key(&pos, |x| x.pos()) - .unwrap_or_else(|x| x); - let non_narrow: usize = - f.non_narrow_chars[0..end_width_idx].iter().map(|x| x.width()).sum(); - chpos.0 - end_width_idx + non_narrow - }; - Loc { file: f, line: 0, col: chpos, col_display } - } - } + let sf = self.lookup_source_file(pos); + let (line, col, col_display) = sf.lookup_file_pos_with_col_display(pos); + Loc { file: sf, line, col, col_display } } // If the corresponding `SourceFile` is empty, does not return a line number. pub fn lookup_line(&self, pos: BytePos) -> Result> { - let idx = self.lookup_source_file_idx(pos); - - let f = (*self.files.borrow().source_files)[idx].clone(); + let f = self.lookup_source_file(pos); match f.lookup_line(pos) { Some(line) => Ok(SourceFileAndLine { sf: f, line }), @@ -934,27 +898,8 @@ impl SourceMap { /// Converts an absolute `BytePos` to a `CharPos` relative to the `SourceFile`. pub fn bytepos_to_file_charpos(&self, bpos: BytePos) -> CharPos { let idx = self.lookup_source_file_idx(bpos); - let map = &(*self.files.borrow().source_files)[idx]; - - // The number of extra bytes due to multibyte chars in the `SourceFile`. - let mut total_extra_bytes = 0; - - for mbc in map.multibyte_chars.iter() { - debug!("{}-byte char at {:?}", mbc.bytes, mbc.pos); - if mbc.pos < bpos { - // Every character is at least one byte, so we only - // count the actual extra bytes. - total_extra_bytes += mbc.bytes as u32 - 1; - // We should never see a byte position in the middle of a - // character. - assert!(bpos.to_u32() >= mbc.pos.to_u32() + mbc.bytes as u32); - } else { - break; - } - } - - assert!(map.start_pos.to_u32() + total_extra_bytes <= bpos.to_u32()); - CharPos(bpos.to_usize() - map.start_pos.to_usize() - total_extra_bytes as usize) + let sf = &(*self.files.borrow().source_files)[idx]; + sf.bytepos_to_file_charpos(bpos) } // Returns the index of the `SourceFile` (in `self.files`) that contains `pos`. diff --git a/src/test/mir-opt/graphviz.main.mir_map.0.dot b/src/test/mir-opt/graphviz.main.mir_map.0.dot index df4f11f0f2169..8d1da7f1b9651 100644 --- a/src/test/mir-opt/graphviz.main.mir_map.0.dot +++ b/src/test/mir-opt/graphviz.main.mir_map.0.dot @@ -3,8 +3,5 @@ digraph Mir_0_3 { node [fontname="Courier, monospace"]; edge [fontname="Courier, monospace"]; label=>; - bb0__0_3 [shape="none", label=<
0
_0 = const ()
goto
>]; - bb1__0_3 [shape="none", label=<
1
resume
>]; - bb2__0_3 [shape="none", label=<
2
return
>]; - bb0__0_3 -> bb2__0_3 [label=""]; + bb0__0_3 [shape="none", label=<
0
_0 = const ()
return
>]; } diff --git a/src/test/mir-opt/graphviz.main.mir_map.0.dot.mir b/src/test/mir-opt/graphviz.main.mir_map.0.dot.mir deleted file mode 100644 index 8d1da7f1b9651..0000000000000 --- a/src/test/mir-opt/graphviz.main.mir_map.0.dot.mir +++ /dev/null @@ -1,7 +0,0 @@ -digraph Mir_0_3 { - graph [fontname="Courier, monospace"]; - node [fontname="Courier, monospace"]; - edge [fontname="Courier, monospace"]; - label=>; - bb0__0_3 [shape="none", label=<
0
_0 = const ()
return
>]; -} diff --git a/src/test/mir-opt/instrument_coverage.bar.InstrumentCoverage.diff b/src/test/mir-opt/instrument_coverage.bar.InstrumentCoverage.diff index 5b2572655ccb6..5048359e5c654 100644 --- a/src/test/mir-opt/instrument_coverage.bar.InstrumentCoverage.diff +++ b/src/test/mir-opt/instrument_coverage.bar.InstrumentCoverage.diff @@ -6,7 +6,7 @@ bb0: { _0 = const true; // scope 0 at /the/src/instrument_coverage.rs:20:5: 20:9 -+ Coverage::Counter(0) for /the/src/instrument_coverage.rs:19:18 - 21:2; // scope 0 at /the/src/instrument_coverage.rs:21:2: 21:2 ++ Coverage::Counter(1) for /the/src/instrument_coverage.rs:20:5 - 21:2; // scope 0 at /the/src/instrument_coverage.rs:21:2: 21:2 return; // scope 0 at /the/src/instrument_coverage.rs:21:2: 21:2 } } diff --git a/src/test/mir-opt/instrument_coverage.main.InstrumentCoverage.diff b/src/test/mir-opt/instrument_coverage.main.InstrumentCoverage.diff index 800754542d936..598727e677ca2 100644 --- a/src/test/mir-opt/instrument_coverage.main.InstrumentCoverage.diff +++ b/src/test/mir-opt/instrument_coverage.main.InstrumentCoverage.diff @@ -8,7 +8,7 @@ let mut _3: !; // in scope 0 at /the/src/instrument_coverage.rs:12:18: 14:10 bb0: { -+ Coverage::Counter(0) for /the/src/instrument_coverage.rs:10:11 - 16:2; // scope 0 at /the/src/instrument_coverage.rs:11:5: 15:6 ++ Coverage::Counter(1) for /the/src/instrument_coverage.rs:12:12 - 12:17; // scope 0 at /the/src/instrument_coverage.rs:11:5: 15:6 falseUnwind -> [real: bb1, cleanup: bb6]; // scope 0 at /the/src/instrument_coverage.rs:11:5: 15:6 } @@ -26,6 +26,7 @@ } bb3: { ++ Coverage::Counter(2) for /the/src/instrument_coverage.rs:13:13 - 16:2; // scope 0 at /the/src/instrument_coverage.rs:12:9: 14:10 falseEdge -> [real: bb5, imaginary: bb4]; // scope 0 at /the/src/instrument_coverage.rs:12:9: 14:10 } diff --git a/src/test/mir-opt/spanview_block.main.mir_map.0.html b/src/test/mir-opt/spanview_block.main.mir_map.0.html index 8f6b1307971b6..8e5268043e70c 100644 --- a/src/test/mir-opt/spanview_block.main.mir_map.0.html +++ b/src/test/mir-opt/spanview_block.main.mir_map.0.html @@ -1,8 +1,8 @@ - coverage_of_if_else - Code Regions - +
fn main() 0⦊{}⦉02⦊⦉2
+ 5:13-5:13: Return: return">0⦊{}⦉0
diff --git a/src/test/mir-opt/spanview_statement.main.mir_map.0.html b/src/test/mir-opt/spanview_statement.main.mir_map.0.html index 072d22473a991..abbff2270b7f8 100644 --- a/src/test/mir-opt/spanview_statement.main.mir_map.0.html +++ b/src/test/mir-opt/spanview_statement.main.mir_map.0.html @@ -1,8 +1,8 @@ - coverage_of_if_else - Code Regions - +
fn main() 0[0]⦊{}⦉0[0]0:Goto⦊⦉0:Goto2:Return⦊⦉2:Return
+ 5:11-5:13: Assign: _0 = const ()">0[0]⦊{}⦉0[0]0:Return⦊⦉0:Return diff --git a/src/test/mir-opt/spanview_terminator.main.mir_map.0.html b/src/test/mir-opt/spanview_terminator.main.mir_map.0.html index e023f0f8aeac9..55fafd90b0aa2 100644 --- a/src/test/mir-opt/spanview_terminator.main.mir_map.0.html +++ b/src/test/mir-opt/spanview_terminator.main.mir_map.0.html @@ -1,8 +1,8 @@ - coverage_of_if_else - Code Regions - + -
fn main() {}0:Goto⦊⦉0:Goto2:Return⦊⦉2:Return
+
fn main() {}0:Return⦊⦉0:Return
diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/Makefile b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/Makefile index cb081fb641b0e..0e05df72c4cdf 100644 --- a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/Makefile +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/Makefile @@ -20,25 +20,29 @@ all: $(patsubst $(SOURCEDIR)/%.rs,%,$(wildcard $(SOURCEDIR)/*.rs)) clear_expected_if_blessed: ifdef RUSTC_BLESS_TEST rm -f expected_export_coverage.*.json - rm -f typical_show_coverage.*.txt + rm -f expected_show_coverage.*.txt endif -include clear_expected_if_blessed %: $(SOURCEDIR)/%.rs - # Compile the test program with "experimental" coverage instrumentation and generate relevant MIR. - # - # FIXME(richkadel): `-Zexperimental-coverage` to `-Zinstrument-coverage` once we are - # satisfied with the branch-level instrumentation. + # Compile the test program with coverage instrumentation and generate relevant MIR. $(RUSTC) $(SOURCEDIR)/$@.rs \ - -Zexperimental-coverage \ + -Zinstrument-coverage \ -Clink-dead-code=$(LINK_DEAD_CODE) # Run it in order to generate some profiling data, # with `LLVM_PROFILE_FILE=` environment variable set to # output the coverage stats for this run. LLVM_PROFILE_FILE="$(TMPDIR)"/$@.profraw \ - $(call RUN,$@) + $(call RUN,$@) || \ + ( \ + status=$$?; \ + grep -q "^\/\/ expect-exit-status-$$status" $(SOURCEDIR)/$@.rs || \ + ( >&2 echo "program exited with an unexpected exit status: $$status"; \ + false \ + ) \ + ) # Postprocess the profiling data so it can be used by the llvm-cov tool "$(LLVM_BIN_DIR)"/llvm-profdata merge --sparse \ @@ -57,11 +61,20 @@ endif > "$(TMPDIR)"/actual_show_coverage.$@.txt ifdef RUSTC_BLESS_TEST - cp "$(TMPDIR)"/actual_show_coverage.$@.txt typical_show_coverage.$@.txt + cp "$(TMPDIR)"/actual_show_coverage.$@.txt expected_show_coverage.$@.txt else # Compare the show coverage output (`--bless` refreshes `typical` files) - $(DIFF) typical_show_coverage.$@.txt "$(TMPDIR)"/actual_show_coverage.$@.txt || \ - >&2 echo 'diff failed for `llvm-cov show` on $@ (might not be an error)' + # Note `llvm-cov show` output for some programs can vary, but can be ignored + # by inserting `// ignore-llvm-cov-show-diffs` at the top of the source file. + + $(DIFF) expected_show_coverage.$@.txt "$(TMPDIR)"/actual_show_coverage.$@.txt || \ + ( grep -q '^\/\/ ignore-llvm-cov-show-diffs' $(SOURCEDIR)/$@.rs && \ + >&2 echo 'diff failed, but suppressed with `// ignore-llvm-cov-show-diffs` in $(SOURCEDIR)/$@.rs' \ + ) || \ + ( >&2 echo 'diff failed, and not suppressed without `// ignore-llvm-cov-show-diffs` in $(SOURCEDIR)/$@.rs'; \ + false \ + ) + endif # Generate a coverage report in JSON, using `llvm-cov export`, and fail if diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.closure.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.closure.json new file mode 100644 index 0000000000000..9dbd0c4e5d6ad --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.closure.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/closure.rs", + "summary": { + "functions": { + "count": 5, + "covered": 3, + "percent": 60 + }, + "instantiations": { + "count": 5, + "covered": 3, + "percent": 60 + }, + "lines": { + "count": 91, + "covered": 75, + "percent": 82.41758241758241 + }, + "regions": { + "count": 21, + "covered": 11, + "notcovered": 10, + "percent": 52.38095238095239 + } + } + } + ], + "totals": { + "functions": { + "count": 5, + "covered": 3, + "percent": 60 + }, + "instantiations": { + "count": 5, + "covered": 3, + "percent": 60 + }, + "lines": { + "count": 91, + "covered": 75, + "percent": 82.41758241758241 + }, + "regions": { + "count": 21, + "covered": 11, + "notcovered": 10, + "percent": 52.38095238095239 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.drop_trait.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.drop_trait.json new file mode 100644 index 0000000000000..cdfdca990fa48 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.drop_trait.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/drop_trait.rs", + "summary": { + "functions": { + "count": 2, + "covered": 2, + "percent": 100 + }, + "instantiations": { + "count": 2, + "covered": 2, + "percent": 100 + }, + "lines": { + "count": 10, + "covered": 10, + "percent": 100 + }, + "regions": { + "count": 5, + "covered": 5, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "totals": { + "functions": { + "count": 2, + "covered": 2, + "percent": 100 + }, + "instantiations": { + "count": 2, + "covered": 2, + "percent": 100 + }, + "lines": { + "count": 10, + "covered": 10, + "percent": 100 + }, + "regions": { + "count": 5, + "covered": 5, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.generics.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.generics.json new file mode 100644 index 0000000000000..ae405afedc159 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.generics.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/generics.rs", + "summary": { + "functions": { + "count": 3, + "covered": 3, + "percent": 100 + }, + "instantiations": { + "count": 5, + "covered": 5, + "percent": 100 + }, + "lines": { + "count": 16, + "covered": 16, + "percent": 100 + }, + "regions": { + "count": 6, + "covered": 6, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "totals": { + "functions": { + "count": 3, + "covered": 3, + "percent": 100 + }, + "instantiations": { + "count": 5, + "covered": 5, + "percent": 100 + }, + "lines": { + "count": 16, + "covered": 16, + "percent": 100 + }, + "regions": { + "count": 6, + "covered": 6, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.if.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.if.json new file mode 100644 index 0000000000000..5231593d8a0eb --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.if.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/if.rs", + "summary": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 19, + "covered": 19, + "percent": 100 + }, + "regions": { + "count": 4, + "covered": 4, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "totals": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 19, + "covered": 19, + "percent": 100 + }, + "regions": { + "count": 4, + "covered": 4, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.if_else.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.if_else.json new file mode 100644 index 0000000000000..abc1afc9ab5c7 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.if_else.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/if_else.rs", + "summary": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 28, + "covered": 19, + "percent": 67.85714285714286 + }, + "regions": { + "count": 7, + "covered": 5, + "notcovered": 2, + "percent": 71.42857142857143 + } + } + } + ], + "totals": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 28, + "covered": 19, + "percent": 67.85714285714286 + }, + "regions": { + "count": 7, + "covered": 5, + "notcovered": 2, + "percent": 71.42857142857143 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.inner_items.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.inner_items.json new file mode 100644 index 0000000000000..366f2afc6d7ae --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.inner_items.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/inner_items.rs", + "summary": { + "functions": { + "count": 4, + "covered": 4, + "percent": 100 + }, + "instantiations": { + "count": 4, + "covered": 4, + "percent": 100 + }, + "lines": { + "count": 26, + "covered": 26, + "percent": 100 + }, + "regions": { + "count": 13, + "covered": 13, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "totals": { + "functions": { + "count": 4, + "covered": 4, + "percent": 100 + }, + "instantiations": { + "count": 4, + "covered": 4, + "percent": 100 + }, + "lines": { + "count": 26, + "covered": 26, + "percent": 100 + }, + "regions": { + "count": 13, + "covered": 13, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.lazy_boolean.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.lazy_boolean.json new file mode 100644 index 0000000000000..3b052ad910656 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.lazy_boolean.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/lazy_boolean.rs", + "summary": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 21, + "covered": 19, + "percent": 90.47619047619048 + }, + "regions": { + "count": 16, + "covered": 14, + "notcovered": 2, + "percent": 87.5 + } + } + } + ], + "totals": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 21, + "covered": 19, + "percent": 90.47619047619048 + }, + "regions": { + "count": 16, + "covered": 14, + "notcovered": 2, + "percent": 87.5 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.loop_break_value.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.loop_break_value.json new file mode 100644 index 0000000000000..0b9344f86f481 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.loop_break_value.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/loop_break_value.rs", + "summary": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 11, + "covered": 11, + "percent": 100 + }, + "regions": { + "count": 1, + "covered": 1, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "totals": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 11, + "covered": 11, + "percent": 100 + }, + "regions": { + "count": 1, + "covered": 1, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.question_mark_error_result.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.question_mark_error_result.json new file mode 100644 index 0000000000000..f2e95e4a83663 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.question_mark_error_result.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/question_mark_error_result.rs", + "summary": { + "functions": { + "count": 2, + "covered": 2, + "percent": 100 + }, + "instantiations": { + "count": 2, + "covered": 2, + "percent": 100 + }, + "lines": { + "count": 16, + "covered": 15, + "percent": 93.75 + }, + "regions": { + "count": 13, + "covered": 12, + "notcovered": 1, + "percent": 92.3076923076923 + } + } + } + ], + "totals": { + "functions": { + "count": 2, + "covered": 2, + "percent": 100 + }, + "instantiations": { + "count": 2, + "covered": 2, + "percent": 100 + }, + "lines": { + "count": 16, + "covered": 15, + "percent": 93.75 + }, + "regions": { + "count": 13, + "covered": 12, + "notcovered": 1, + "percent": 92.3076923076923 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.simple_loop.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.simple_loop.json new file mode 100644 index 0000000000000..5701575189a8c --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.simple_loop.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/simple_loop.rs", + "summary": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 18, + "covered": 18, + "percent": 100 + }, + "regions": { + "count": 7, + "covered": 7, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "totals": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 18, + "covered": 18, + "percent": 100 + }, + "regions": { + "count": 7, + "covered": 7, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.simple_match.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.simple_match.json new file mode 100644 index 0000000000000..df43cb9640dc6 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.simple_match.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/simple_match.rs", + "summary": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 26, + "covered": 26, + "percent": 100 + }, + "regions": { + "count": 9, + "covered": 9, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "totals": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 26, + "covered": 26, + "percent": 100 + }, + "regions": { + "count": 9, + "covered": 9, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.coverage_of_if_else.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.various_conditions.json similarity index 69% rename from src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.coverage_of_if_else.json rename to src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.various_conditions.json index 051250d90a24a..82c80024df6d5 100644 --- a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.coverage_of_if_else.json +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.various_conditions.json @@ -3,7 +3,7 @@ { "files": [ { - "filename": "../instrument-coverage/coverage_of_if_else.rs", + "filename": "../instrument-coverage/various_conditions.rs", "summary": { "functions": { "count": 1, @@ -16,15 +16,15 @@ "percent": 100 }, "lines": { - "count": 40, - "covered": 19, - "percent": 47.5 + "count": 49, + "covered": 23, + "percent": 46.93877551020408 }, "regions": { - "count": 71, - "covered": 23, - "notcovered": 48, - "percent": 32.3943661971831 + "count": 51, + "covered": 19, + "notcovered": 32, + "percent": 37.254901960784316 } } } @@ -41,15 +41,15 @@ "percent": 100 }, "lines": { - "count": 40, - "covered": 19, - "percent": 47.5 + "count": 49, + "covered": 23, + "percent": 46.93877551020408 }, "regions": { - "count": 71, - "covered": 23, - "notcovered": 48, - "percent": 32.3943661971831 + "count": 51, + "covered": 19, + "notcovered": 32, + "percent": 37.254901960784316 } } } diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.while_early_return.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.while_early_return.json new file mode 100644 index 0000000000000..aa4e9a3c5dda7 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_export_coverage.while_early_return.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/while_early_return.rs", + "summary": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 18, + "covered": 16, + "percent": 88.88888888888889 + }, + "regions": { + "count": 9, + "covered": 7, + "notcovered": 2, + "percent": 77.77777777777779 + } + } + } + ], + "totals": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 18, + "covered": 16, + "percent": 88.88888888888889 + }, + "regions": { + "count": 9, + "covered": 7, + "notcovered": 2, + "percent": 77.77777777777779 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.closure.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.closure.txt new file mode 100644 index 0000000000000..17054490e9b3c --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.closure.txt @@ -0,0 +1,94 @@ + 1| |#![allow(unused_assignments, unused_variables)] + 2| | + 3| 1|fn main() { + 4| 1| // Initialize test constants in a way that cannot be determined at compile time, to ensure + 5| 1| // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + 6| 1| // dependent conditions. + 7| 1| let is_true = std::env::args().len() == 1; + 8| 1| let is_false = ! is_true; + 9| 1| + 10| 1| let mut some_string = Some(String::from("the string content")); + 11| 1| println!( + 12| 1| "The string or alt: {}" + 13| 1| , + 14| 1| some_string + 15| 1| . + 16| 1| unwrap_or_else + 17| 1| ( + 18| 1| || + 19| | { + 20| 0| let mut countdown = 0; + 21| 0| if is_false { + 22| 0| countdown = 10; + 23| 0| } + 24| 0| "alt string 1".to_owned() + 25| 1| } + 26| 1| ) + 27| 1| ); + 28| 1| + 29| 1| some_string = Some(String::from("the string content")); + 30| 1| let + 31| 1| a + 32| 1| = + 33| 1| || + 34| | { + 35| 0| let mut countdown = 0; + 36| 0| if is_false { + 37| 0| countdown = 10; + 38| 0| } + 39| 0| "alt string 2".to_owned() + 40| 1| }; + 41| 1| println!( + 42| 1| "The string or alt: {}" + 43| 1| , + 44| 1| some_string + 45| 1| . + 46| 1| unwrap_or_else + 47| 1| ( + 48| 1| a + 49| 1| ) + 50| 1| ); + 51| 1| + 52| 1| some_string = None; + 53| 1| println!( + 54| 1| "The string or alt: {}" + 55| 1| , + 56| 1| some_string + 57| 1| . + 58| 1| unwrap_or_else + 59| 1| ( + 60| 1| || + 61| | { + 62| 1| let mut countdown = 0; + 63| 1| if is_false { + 64| 0| countdown = 10; + 65| 0| } + 66| 1| "alt string 3".to_owned() + 67| 1| } + 68| 1| ) + 69| 1| ); + 70| 1| + 71| 1| some_string = None; + 72| 1| let + 73| 1| a + 74| 1| = + 75| 1| || + 76| | { + 77| 1| let mut countdown = 0; + 78| 1| if is_false { + 79| 0| countdown = 10; + 80| 0| } + 81| 1| "alt string 4".to_owned() + 82| 1| }; + 83| 1| println!( + 84| 1| "The string or alt: {}" + 85| 1| , + 86| 1| some_string + 87| 1| . + 88| 1| unwrap_or_else + 89| 1| ( + 90| 1| a + 91| 1| ) + 92| 1| ); + 93| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.drop_trait.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.drop_trait.txt new file mode 100644 index 0000000000000..72aa020ca1691 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.drop_trait.txt @@ -0,0 +1,34 @@ + 1| |#![allow(unused_assignments)] + 2| |// expect-exit-status-1 + 3| | + 4| |struct Firework { + 5| | strength: i32, + 6| |} + 7| | + 8| |impl Drop for Firework { + 9| 2| fn drop(&mut self) { + 10| 2| println!("BOOM times {}!!!", self.strength); + 11| 2| } + 12| |} + 13| | + 14| |fn main() -> Result<(),u8> { + 15| 1| let _firecracker = Firework { strength: 1 }; + 16| 1| + 17| 1| let _tnt = Firework { strength: 100 }; + 18| | + 19| 1| if true { + 20| 1| println!("Exiting with error..."); + 21| 1| return Err(1); + 22| | } + 23| | + 24| | let _ = Firework { strength: 1000 }; + 25| | + 26| | Ok(()) + 27| 1|} + 28| | + 29| |// Expected program output: + 30| |// Exiting with error... + 31| |// BOOM times 100!!! + 32| |// BOOM times 1!!! + 33| |// Error: 1 + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.generics.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.generics.txt new file mode 100644 index 0000000000000..86199d7476302 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.generics.txt @@ -0,0 +1,67 @@ + 1| |#![allow(unused_assignments)] + 2| |// expect-exit-status-1 + 3| | + 4| |struct Firework where T: Copy + std::fmt::Display { + 5| | strength: T, + 6| |} + 7| | + 8| |impl Firework where T: Copy + std::fmt::Display { + 9| | #[inline(always)] + 10| 3| fn set_strength(&mut self, new_strength: T) { + 11| 3| self.strength = new_strength; + 12| 3| } + ------------------ + | >::set_strength: + | 10| 2| fn set_strength(&mut self, new_strength: T) { + | 11| 2| self.strength = new_strength; + | 12| 2| } + ------------------ + | >::set_strength: + | 10| 1| fn set_strength(&mut self, new_strength: T) { + | 11| 1| self.strength = new_strength; + | 12| 1| } + ------------------ + 13| |} + 14| | + 15| |impl Drop for Firework where T: Copy + std::fmt::Display { + 16| | #[inline(always)] + 17| 2| fn drop(&mut self) { + 18| 2| println!("BOOM times {}!!!", self.strength); + 19| 2| } + ------------------ + | as core::ops::drop::Drop>::drop: + | 17| 1| fn drop(&mut self) { + | 18| 1| println!("BOOM times {}!!!", self.strength); + | 19| 1| } + ------------------ + | as core::ops::drop::Drop>::drop: + | 17| 1| fn drop(&mut self) { + | 18| 1| println!("BOOM times {}!!!", self.strength); + | 19| 1| } + ------------------ + 20| |} + 21| | + 22| |fn main() -> Result<(),u8> { + 23| 1| let mut firecracker = Firework { strength: 1 }; + 24| 1| firecracker.set_strength(2); + 25| 1| + 26| 1| let mut tnt = Firework { strength: 100.1 }; + 27| 1| tnt.set_strength(200.1); + 28| 1| tnt.set_strength(300.3); + 29| | + 30| 1| if true { + 31| 1| println!("Exiting with error..."); + 32| 1| return Err(1); + 33| | } + 34| | + 35| | let _ = Firework { strength: 1000 }; + 36| | + 37| | Ok(()) + 38| 1|} + 39| | + 40| |// Expected program output: + 41| |// Exiting with error... + 42| |// BOOM times 100!!! + 43| |// BOOM times 1!!! + 44| |// Error: 1 + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.if.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.if.txt new file mode 100644 index 0000000000000..bc2f9b108b2f3 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.if.txt @@ -0,0 +1,29 @@ + 1| |#![allow(unused_assignments, unused_variables)] + 2| | + 3| |fn main() { + 4| | // Initialize test constants in a way that cannot be determined at compile time, to ensure + 5| | // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + 6| | // dependent conditions. + 7| | let + 8| 1| is_true + 9| 1| = + 10| 1| std::env::args().len() + 11| 1| == + 12| 1| 1 + 13| 1| ; + 14| 1| let + 15| 1| mut + 16| 1| countdown + 17| 1| = + 18| 1| 0 + 19| | ; + 20| | if + 21| 1| is_true + 22| 1| { + 23| 1| countdown + 24| 1| = + 25| 1| 10 + 26| 1| ; + 27| 1| } + 28| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.if_else.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.if_else.txt new file mode 100644 index 0000000000000..5f899723e2554 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.if_else.txt @@ -0,0 +1,41 @@ + 1| |#![allow(unused_assignments)] + 2| | + 3| |fn main() { + 4| | // Initialize test constants in a way that cannot be determined at compile time, to ensure + 5| | // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + 6| | // dependent conditions. + 7| 1| let is_true = std::env::args().len() == 1; + 8| 1| + 9| 1| let mut countdown = 0; + 10| 1| if + 11| 1| is_true + 12| 1| { + 13| 1| countdown + 14| 1| = + 15| 1| 10 + 16| 1| ; + 17| 1| } + 18| | else // Note coverage region difference without semicolon + 19| | { + 20| 0| countdown + 21| 0| = + 22| 0| 100 + 23| | } + 24| | + 25| | if + 26| 1| is_true + 27| 1| { + 28| 1| countdown + 29| 1| = + 30| 1| 10 + 31| 1| ; + 32| 1| } + 33| | else + 34| 0| { + 35| 0| countdown + 36| 0| = + 37| 0| 100 + 38| 0| ; + 39| 0| } + 40| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.inner_items.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.inner_items.txt new file mode 100644 index 0000000000000..364d25b1646f4 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.inner_items.txt @@ -0,0 +1,58 @@ + 1| |#![allow(unused_assignments, unused_variables)] + 2| | + 3| |fn main() { + 4| | // Initialize test constants in a way that cannot be determined at compile time, to ensure + 5| | // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + 6| | // dependent conditions. + 7| 1| let is_true = std::env::args().len() == 1; + 8| 1| + 9| 1| let mut countdown = 0; + 10| 1| if is_true { + 11| 1| countdown = 10; + 12| 1| } + 13| | + 14| | mod inner_mod { + 15| | const INNER_MOD_CONST: u32 = 1000; + 16| | } + 17| | + 18| | fn inner_function(a: u32) { + 19| 3| let b = 1; + 20| 3| let c = a + b; + 21| 3| println!("c = {}", c) + 22| 3| } + 23| | + 24| | struct InnerStruct { + 25| | inner_struct_field: u32, + 26| | } + 27| | + 28| | const INNER_CONST: u32 = 1234; + 29| | + 30| | trait InnerTrait { + 31| | fn inner_trait_func(&mut self, incr: u32); + 32| | + 33| 1| fn default_trait_func(&mut self) { + 34| 1| inner_function(INNER_CONST); + 35| 1| self.inner_trait_func(INNER_CONST); + 36| 1| } + 37| | } + 38| | + 39| | impl InnerTrait for InnerStruct { + 40| | fn inner_trait_func(&mut self, incr: u32) { + 41| 1| self.inner_struct_field += incr; + 42| 1| inner_function(self.inner_struct_field); + 43| 1| } + 44| | } + 45| | + 46| | type InnerType = String; + 47| | + 48| 1| if is_true { + 49| 1| inner_function(countdown); + 50| 1| } + 51| | + 52| 1| let mut val = InnerStruct { + 53| 1| inner_struct_field: 101, + 54| 1| }; + 55| 1| + 56| 1| val.default_trait_func(); + 57| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.lazy_boolean.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.lazy_boolean.txt new file mode 100644 index 0000000000000..ded4369751587 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.lazy_boolean.txt @@ -0,0 +1,44 @@ + 1| |#![allow(unused_assignments, unused_variables)] + 2| | + 3| |fn main() { + 4| | // Initialize test constants in a way that cannot be determined at compile time, to ensure + 5| | // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + 6| | // dependent conditions. + 7| 1| let is_true = std::env::args().len() == 1; + 8| 1| + 9| 1| let (mut a, mut b, mut c) = (0, 0, 0); + 10| 1| if is_true { + 11| 1| a = 1; + 12| 1| b = 10; + 13| 1| c = 100; + 14| 1| } + 15| | let + 16| 1| somebool + 17| | = + 18| 1| a < b + 19| | || + 20| 0| b < c + 21| | ; + 22| | let + 23| 1| somebool + 24| | = + 25| 1| b < a + 26| | || + 27| 1| b < c + 28| | ; + 29| | let + 30| 1| somebool + 31| | = + 32| 1| a < b + 33| | && + 34| 1| b < c + 35| | ; + 36| | let + 37| 1| somebool + 38| | = + 39| 1| b < a + 40| | && + 41| 0| b < c + 42| | ; + 43| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.loop_break_value.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.loop_break_value.txt new file mode 100644 index 0000000000000..b0d668c6d76da --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.loop_break_value.txt @@ -0,0 +1,14 @@ + 1| |#![allow(unused_assignments)] + 2| | + 3| 1|fn main() { + 4| 1| let result + 5| 1| = + 6| 1| loop + 7| 1| { + 8| 1| break + 9| 1| 10 + 10| 1| ; + 11| 1| } + 12| 1| ; + 13| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.question_mark_error_result.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.question_mark_error_result.txt new file mode 100644 index 0000000000000..ae288d7d7a000 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.question_mark_error_result.txt @@ -0,0 +1,36 @@ + 1| |#![allow(unused_assignments)] + 2| |// expect-exit-status-1 + 3| | + 4| |fn call(return_error: bool) -> Result<(),()> { + 5| 6| if return_error { + 6| 1| Err(()) + 7| | } else { + 8| 5| Ok(()) + 9| | } + 10| 6|} + 11| | + 12| |fn main() -> Result<(),()> { + 13| 1| let mut + 14| 1| countdown = 10 + 15| | ; + 16| 6| for + 17| 6| _ + 18| | in + 19| 1| 0..10 + 20| | { + 21| 6| countdown + 22| 6| -= 1 + 23| | ; + 24| | if + 25| 6| countdown < 5 + 26| | { + 27| 1| call(/*return_error=*/ true)?; + 28| | } + 29| | else + 30| | { + 31| 5| call(/*return_error=*/ false)?; + 32| | } + 33| | } + 34| 0| Ok(()) + 35| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.simple_loop.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.simple_loop.txt new file mode 100644 index 0000000000000..f1acb7c545940 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.simple_loop.txt @@ -0,0 +1,36 @@ + 1| |#![allow(unused_assignments)] + 2| | + 3| |fn main() { + 4| | // Initialize test constants in a way that cannot be determined at compile time, to ensure + 5| | // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + 6| | // dependent conditions. + 7| 1| let is_true = std::env::args().len() == 1; + 8| 1| + 9| 1| let mut countdown = 0; + 10| | + 11| | if + 12| 1| is_true + 13| 1| { + 14| 1| countdown + 15| 1| = + 16| 1| 10 + 17| 1| ; + 18| 1| } + 19| | + 20| | loop + 21| | { + 22| | if + 23| 11| countdown + 24| 11| == + 25| 11| 0 + 26| | { + 27| 1| break + 28| | ; + 29| | } + 30| 10| countdown + 31| 10| -= + 32| 10| 1 + 33| | ; + 34| | } + 35| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.simple_match.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.simple_match.txt new file mode 100644 index 0000000000000..e42f22cd047fc --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.simple_match.txt @@ -0,0 +1,44 @@ + 1| |#![allow(unused_assignments)] + 2| | + 3| |fn main() { + 4| | // Initialize test constants in a way that cannot be determined at compile time, to ensure + 5| | // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + 6| | // dependent conditions. + 7| 1| let is_true = std::env::args().len() == 1; + 8| 1| + 9| 1| let mut countdown = 1; + 10| 1| if is_true { + 11| 1| countdown = 0; + 12| 1| } + 13| | + 14| 3| for + 15| 3| _ + 16| | in + 17| 1| 0..2 + 18| | { + 19| | let z + 20| | ; + 21| | match + 22| 2| countdown + 23| 2| { + 24| 2| x + 25| 2| if + 26| 2| x + 27| 2| < + 28| 2| 1 + 29| | => + 30| 1| { + 31| 1| z = countdown + 32| 1| ; + 33| 1| let y = countdown + 34| 1| ; + 35| 1| countdown = 10 + 36| 1| ; + 37| 1| } + 38| | _ + 39| | => + 40| 1| {} + 41| | } + 42| | } + 43| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.various_conditions.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.various_conditions.txt new file mode 100644 index 0000000000000..173ff4aa4c481 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.various_conditions.txt @@ -0,0 +1,69 @@ + 1| |#![allow(unused_assignments, unused_variables)] + 2| | + 3| |fn main() { + 4| 1| let mut countdown = 0; + 5| 1| if true { + 6| 1| countdown = 10; + 7| 1| } + 8| | + 9| | const B: u32 = 100; + 10| 1| let x = if countdown > 7 { + 11| 1| countdown -= 4; + 12| 1| B + 13| 0| } else if countdown > 2 { + 14| 0| if countdown < 1 || countdown > 5 || countdown != 9 { + 15| 0| countdown = 0; + 16| 0| } + 17| 0| countdown -= 5; + 18| 0| countdown + 19| | } else { + 20| 0| return; + 21| | }; + 22| | + 23| 1| let mut countdown = 0; + 24| 1| if true { + 25| 1| countdown = 10; + 26| 1| } + 27| | + 28| 1| if countdown > 7 { + 29| 1| countdown -= 4; + 30| 0| } else if countdown > 2 { + 31| 0| if countdown < 1 || countdown > 5 || countdown != 9 { + 32| 0| countdown = 0; + 33| 0| } + 34| 0| countdown -= 5; + 35| | } else { + 36| 0| return; + 37| | } + 38| | + 39| 1| let mut countdown = 0; + 40| 1| if true { + 41| 1| countdown = 1; + 42| 1| } + 43| | + 44| 1| let z = if countdown > 7 { + ^0 + 45| 0| countdown -= 4; + 46| 1| } else if countdown > 2 { + 47| 0| if countdown < 1 || countdown > 5 || countdown != 9 { + 48| 0| countdown = 0; + 49| 0| } + 50| 0| countdown -= 5; + 51| | } else { + 52| 1| let should_be_reachable = countdown; + 53| 1| println!("reached"); + 54| 1| return; + 55| | }; + 56| | + 57| 0| let w = if countdown > 7 { + 58| 0| countdown -= 4; + 59| 0| } else if countdown > 2 { + 60| 0| if countdown < 1 || countdown > 5 || countdown != 9 { + 61| 0| countdown = 0; + 62| 0| } + 63| 0| countdown -= 5; + 64| | } else { + 65| 0| return; + 66| | }; + 67| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.while_early_return.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.while_early_return.txt new file mode 100644 index 0000000000000..7dce94f25f304 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/expected_show_coverage.while_early_return.txt @@ -0,0 +1,48 @@ + 1| |#![allow(unused_assignments)] + 2| |// expect-exit-status-1 + 3| | + 4| |fn main() -> Result<(),u8> { + 5| 1| let mut countdown = 10; + 6| 7| while + 7| 7| countdown + 8| 7| > + 9| 7| 0 + 10| | { + 11| | if + 12| 7| countdown + 13| 7| < + 14| 7| 5 + 15| | { + 16| | return + 17| | if + 18| 1| countdown + 19| 1| > + 20| 1| 8 + 21| | { + 22| 0| Ok(()) + 23| | } + 24| | else + 25| | { + 26| 1| Err(1) + 27| | } + 28| | ; + 29| | } + 30| 6| countdown + 31| 6| -= + 32| 6| 1 + 33| | ; + 34| | } + 35| 0| Ok(()) + 36| 1|} + 37| | + 38| |// ISSUE(77553): Originally, this test had `Err(1)` on line 22 (instead of `Ok(())`) and + 39| |// `std::process::exit(2)` on line 26 (instead of `Err(1)`); and this worked as expected on Linux + 40| |// and MacOS. But on Windows (MSVC, at least), the call to `std::process::exit()` exits the program + 41| |// without saving the InstrProf coverage counters. The use of `std::process:exit()` is not critical + 42| |// to the coverage test for early returns, but this is a limitation that should be fixed. + 43| |// + 44| |// FIXME(richkadel): Consider creating a new tests for coverage when calling `std::process::exit()`, + 45| |// move the `ISSUE` comment to that test, and implement a new test directive that supports skipping + 46| |// coverage tests when targeting specific platforms (at least skipping Windows, or MSVC if the + 47| |// problem exists on MSVC only). + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/typical_show_coverage.coverage_of_if_else.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/typical_show_coverage.coverage_of_if_else.txt deleted file mode 100644 index 87ce3b4048f2a..0000000000000 --- a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-base/typical_show_coverage.coverage_of_if_else.txt +++ /dev/null @@ -1,64 +0,0 @@ - 1| |#![allow(unused_assignments)] - 2| | - 3| |fn main() { - 4| | let mut countdown = 0; - 5| 2| if true { - ^1 - 6| 2| countdown = 10; - 7| 2| } - 8| | - 9| 2| if countdown > 7 { - ^1 - 10| 2| countdown -= 4; - ^1 - 11| 2| } else if countdown > 2 { - ^0 ^0 - 12| 0| if countdown < 1 || countdown > 5 || countdown != 9 { - 13| 0| countdown = 0; - 14| 0| } - 15| 0| countdown -= 5; - 16| 0| } else { - 17| 0| return; - 18| 0| } - 19| | - 20| | let mut countdown = 0; - 21| 2| if true { - ^1 - 22| 2| countdown = 10; - 23| 2| } - 24| | - 25| 2| if countdown > 7 { - ^1 - 26| 2| countdown -= 4; - ^1 - 27| 2| } else if countdown > 2 { - ^0 ^0 - 28| 0| if countdown < 1 || countdown > 5 || countdown != 9 { - 29| 0| countdown = 0; - 30| 0| } - 31| 0| countdown -= 5; - 32| 0| } else { - 33| 0| return; - 34| 0| } - 35| | - 36| | let mut countdown = 0; - 37| 2| if true { - ^1 - 38| 2| countdown = 10; - 39| 2| } - 40| | - 41| 2| if countdown > 7 { - ^1 - 42| 2| countdown -= 4; - ^1 - 43| 2| } else if countdown > 2 { - ^0 ^0 - 44| 0| if countdown < 1 || countdown > 5 || countdown != 9 { - 45| 0| countdown = 0; - 46| 0| } - 47| 0| countdown -= 5; - 48| 0| } else { - 49| 0| return; - 50| 0| } - 51| 1|} - diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.closure.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.closure.json new file mode 100644 index 0000000000000..9dbd0c4e5d6ad --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.closure.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/closure.rs", + "summary": { + "functions": { + "count": 5, + "covered": 3, + "percent": 60 + }, + "instantiations": { + "count": 5, + "covered": 3, + "percent": 60 + }, + "lines": { + "count": 91, + "covered": 75, + "percent": 82.41758241758241 + }, + "regions": { + "count": 21, + "covered": 11, + "notcovered": 10, + "percent": 52.38095238095239 + } + } + } + ], + "totals": { + "functions": { + "count": 5, + "covered": 3, + "percent": 60 + }, + "instantiations": { + "count": 5, + "covered": 3, + "percent": 60 + }, + "lines": { + "count": 91, + "covered": 75, + "percent": 82.41758241758241 + }, + "regions": { + "count": 21, + "covered": 11, + "notcovered": 10, + "percent": 52.38095238095239 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.drop_trait.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.drop_trait.json new file mode 100644 index 0000000000000..cdfdca990fa48 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.drop_trait.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/drop_trait.rs", + "summary": { + "functions": { + "count": 2, + "covered": 2, + "percent": 100 + }, + "instantiations": { + "count": 2, + "covered": 2, + "percent": 100 + }, + "lines": { + "count": 10, + "covered": 10, + "percent": 100 + }, + "regions": { + "count": 5, + "covered": 5, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "totals": { + "functions": { + "count": 2, + "covered": 2, + "percent": 100 + }, + "instantiations": { + "count": 2, + "covered": 2, + "percent": 100 + }, + "lines": { + "count": 10, + "covered": 10, + "percent": 100 + }, + "regions": { + "count": 5, + "covered": 5, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.generics.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.generics.json new file mode 100644 index 0000000000000..ae405afedc159 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.generics.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/generics.rs", + "summary": { + "functions": { + "count": 3, + "covered": 3, + "percent": 100 + }, + "instantiations": { + "count": 5, + "covered": 5, + "percent": 100 + }, + "lines": { + "count": 16, + "covered": 16, + "percent": 100 + }, + "regions": { + "count": 6, + "covered": 6, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "totals": { + "functions": { + "count": 3, + "covered": 3, + "percent": 100 + }, + "instantiations": { + "count": 5, + "covered": 5, + "percent": 100 + }, + "lines": { + "count": 16, + "covered": 16, + "percent": 100 + }, + "regions": { + "count": 6, + "covered": 6, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.if.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.if.json new file mode 100644 index 0000000000000..5231593d8a0eb --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.if.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/if.rs", + "summary": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 19, + "covered": 19, + "percent": 100 + }, + "regions": { + "count": 4, + "covered": 4, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "totals": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 19, + "covered": 19, + "percent": 100 + }, + "regions": { + "count": 4, + "covered": 4, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.if_else.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.if_else.json new file mode 100644 index 0000000000000..abc1afc9ab5c7 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.if_else.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/if_else.rs", + "summary": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 28, + "covered": 19, + "percent": 67.85714285714286 + }, + "regions": { + "count": 7, + "covered": 5, + "notcovered": 2, + "percent": 71.42857142857143 + } + } + } + ], + "totals": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 28, + "covered": 19, + "percent": 67.85714285714286 + }, + "regions": { + "count": 7, + "covered": 5, + "notcovered": 2, + "percent": 71.42857142857143 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.inner_items.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.inner_items.json new file mode 100644 index 0000000000000..366f2afc6d7ae --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.inner_items.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/inner_items.rs", + "summary": { + "functions": { + "count": 4, + "covered": 4, + "percent": 100 + }, + "instantiations": { + "count": 4, + "covered": 4, + "percent": 100 + }, + "lines": { + "count": 26, + "covered": 26, + "percent": 100 + }, + "regions": { + "count": 13, + "covered": 13, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "totals": { + "functions": { + "count": 4, + "covered": 4, + "percent": 100 + }, + "instantiations": { + "count": 4, + "covered": 4, + "percent": 100 + }, + "lines": { + "count": 26, + "covered": 26, + "percent": 100 + }, + "regions": { + "count": 13, + "covered": 13, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.lazy_boolean.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.lazy_boolean.json new file mode 100644 index 0000000000000..3b052ad910656 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.lazy_boolean.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/lazy_boolean.rs", + "summary": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 21, + "covered": 19, + "percent": 90.47619047619048 + }, + "regions": { + "count": 16, + "covered": 14, + "notcovered": 2, + "percent": 87.5 + } + } + } + ], + "totals": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 21, + "covered": 19, + "percent": 90.47619047619048 + }, + "regions": { + "count": 16, + "covered": 14, + "notcovered": 2, + "percent": 87.5 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.loop_break_value.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.loop_break_value.json new file mode 100644 index 0000000000000..0b9344f86f481 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.loop_break_value.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/loop_break_value.rs", + "summary": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 11, + "covered": 11, + "percent": 100 + }, + "regions": { + "count": 1, + "covered": 1, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "totals": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 11, + "covered": 11, + "percent": 100 + }, + "regions": { + "count": 1, + "covered": 1, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.question_mark_error_result.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.question_mark_error_result.json new file mode 100644 index 0000000000000..f2e95e4a83663 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.question_mark_error_result.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/question_mark_error_result.rs", + "summary": { + "functions": { + "count": 2, + "covered": 2, + "percent": 100 + }, + "instantiations": { + "count": 2, + "covered": 2, + "percent": 100 + }, + "lines": { + "count": 16, + "covered": 15, + "percent": 93.75 + }, + "regions": { + "count": 13, + "covered": 12, + "notcovered": 1, + "percent": 92.3076923076923 + } + } + } + ], + "totals": { + "functions": { + "count": 2, + "covered": 2, + "percent": 100 + }, + "instantiations": { + "count": 2, + "covered": 2, + "percent": 100 + }, + "lines": { + "count": 16, + "covered": 15, + "percent": 93.75 + }, + "regions": { + "count": 13, + "covered": 12, + "notcovered": 1, + "percent": 92.3076923076923 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.simple_loop.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.simple_loop.json new file mode 100644 index 0000000000000..5701575189a8c --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.simple_loop.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/simple_loop.rs", + "summary": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 18, + "covered": 18, + "percent": 100 + }, + "regions": { + "count": 7, + "covered": 7, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "totals": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 18, + "covered": 18, + "percent": 100 + }, + "regions": { + "count": 7, + "covered": 7, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.simple_match.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.simple_match.json new file mode 100644 index 0000000000000..df43cb9640dc6 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.simple_match.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/simple_match.rs", + "summary": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 26, + "covered": 26, + "percent": 100 + }, + "regions": { + "count": 9, + "covered": 9, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "totals": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 26, + "covered": 26, + "percent": 100 + }, + "regions": { + "count": 9, + "covered": 9, + "notcovered": 0, + "percent": 100 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.coverage_of_if_else.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.various_conditions.json similarity index 69% rename from src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.coverage_of_if_else.json rename to src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.various_conditions.json index 051250d90a24a..82c80024df6d5 100644 --- a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.coverage_of_if_else.json +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.various_conditions.json @@ -3,7 +3,7 @@ { "files": [ { - "filename": "../instrument-coverage/coverage_of_if_else.rs", + "filename": "../instrument-coverage/various_conditions.rs", "summary": { "functions": { "count": 1, @@ -16,15 +16,15 @@ "percent": 100 }, "lines": { - "count": 40, - "covered": 19, - "percent": 47.5 + "count": 49, + "covered": 23, + "percent": 46.93877551020408 }, "regions": { - "count": 71, - "covered": 23, - "notcovered": 48, - "percent": 32.3943661971831 + "count": 51, + "covered": 19, + "notcovered": 32, + "percent": 37.254901960784316 } } } @@ -41,15 +41,15 @@ "percent": 100 }, "lines": { - "count": 40, - "covered": 19, - "percent": 47.5 + "count": 49, + "covered": 23, + "percent": 46.93877551020408 }, "regions": { - "count": 71, - "covered": 23, - "notcovered": 48, - "percent": 32.3943661971831 + "count": 51, + "covered": 19, + "notcovered": 32, + "percent": 37.254901960784316 } } } diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.while_early_return.json b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.while_early_return.json new file mode 100644 index 0000000000000..aa4e9a3c5dda7 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_export_coverage.while_early_return.json @@ -0,0 +1,59 @@ +{ + "data": [ + { + "files": [ + { + "filename": "../instrument-coverage/while_early_return.rs", + "summary": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 18, + "covered": 16, + "percent": 88.88888888888889 + }, + "regions": { + "count": 9, + "covered": 7, + "notcovered": 2, + "percent": 77.77777777777779 + } + } + } + ], + "totals": { + "functions": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "instantiations": { + "count": 1, + "covered": 1, + "percent": 100 + }, + "lines": { + "count": 18, + "covered": 16, + "percent": 88.88888888888889 + }, + "regions": { + "count": 9, + "covered": 7, + "notcovered": 2, + "percent": 77.77777777777779 + } + } + } + ], + "type": "llvm.coverage.json.export", + "version": "2.0.1" +} diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.closure.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.closure.txt new file mode 100644 index 0000000000000..17054490e9b3c --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.closure.txt @@ -0,0 +1,94 @@ + 1| |#![allow(unused_assignments, unused_variables)] + 2| | + 3| 1|fn main() { + 4| 1| // Initialize test constants in a way that cannot be determined at compile time, to ensure + 5| 1| // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + 6| 1| // dependent conditions. + 7| 1| let is_true = std::env::args().len() == 1; + 8| 1| let is_false = ! is_true; + 9| 1| + 10| 1| let mut some_string = Some(String::from("the string content")); + 11| 1| println!( + 12| 1| "The string or alt: {}" + 13| 1| , + 14| 1| some_string + 15| 1| . + 16| 1| unwrap_or_else + 17| 1| ( + 18| 1| || + 19| | { + 20| 0| let mut countdown = 0; + 21| 0| if is_false { + 22| 0| countdown = 10; + 23| 0| } + 24| 0| "alt string 1".to_owned() + 25| 1| } + 26| 1| ) + 27| 1| ); + 28| 1| + 29| 1| some_string = Some(String::from("the string content")); + 30| 1| let + 31| 1| a + 32| 1| = + 33| 1| || + 34| | { + 35| 0| let mut countdown = 0; + 36| 0| if is_false { + 37| 0| countdown = 10; + 38| 0| } + 39| 0| "alt string 2".to_owned() + 40| 1| }; + 41| 1| println!( + 42| 1| "The string or alt: {}" + 43| 1| , + 44| 1| some_string + 45| 1| . + 46| 1| unwrap_or_else + 47| 1| ( + 48| 1| a + 49| 1| ) + 50| 1| ); + 51| 1| + 52| 1| some_string = None; + 53| 1| println!( + 54| 1| "The string or alt: {}" + 55| 1| , + 56| 1| some_string + 57| 1| . + 58| 1| unwrap_or_else + 59| 1| ( + 60| 1| || + 61| | { + 62| 1| let mut countdown = 0; + 63| 1| if is_false { + 64| 0| countdown = 10; + 65| 0| } + 66| 1| "alt string 3".to_owned() + 67| 1| } + 68| 1| ) + 69| 1| ); + 70| 1| + 71| 1| some_string = None; + 72| 1| let + 73| 1| a + 74| 1| = + 75| 1| || + 76| | { + 77| 1| let mut countdown = 0; + 78| 1| if is_false { + 79| 0| countdown = 10; + 80| 0| } + 81| 1| "alt string 4".to_owned() + 82| 1| }; + 83| 1| println!( + 84| 1| "The string or alt: {}" + 85| 1| , + 86| 1| some_string + 87| 1| . + 88| 1| unwrap_or_else + 89| 1| ( + 90| 1| a + 91| 1| ) + 92| 1| ); + 93| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.drop_trait.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.drop_trait.txt new file mode 100644 index 0000000000000..72aa020ca1691 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.drop_trait.txt @@ -0,0 +1,34 @@ + 1| |#![allow(unused_assignments)] + 2| |// expect-exit-status-1 + 3| | + 4| |struct Firework { + 5| | strength: i32, + 6| |} + 7| | + 8| |impl Drop for Firework { + 9| 2| fn drop(&mut self) { + 10| 2| println!("BOOM times {}!!!", self.strength); + 11| 2| } + 12| |} + 13| | + 14| |fn main() -> Result<(),u8> { + 15| 1| let _firecracker = Firework { strength: 1 }; + 16| 1| + 17| 1| let _tnt = Firework { strength: 100 }; + 18| | + 19| 1| if true { + 20| 1| println!("Exiting with error..."); + 21| 1| return Err(1); + 22| | } + 23| | + 24| | let _ = Firework { strength: 1000 }; + 25| | + 26| | Ok(()) + 27| 1|} + 28| | + 29| |// Expected program output: + 30| |// Exiting with error... + 31| |// BOOM times 100!!! + 32| |// BOOM times 1!!! + 33| |// Error: 1 + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.generics.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.generics.txt new file mode 100644 index 0000000000000..86199d7476302 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.generics.txt @@ -0,0 +1,67 @@ + 1| |#![allow(unused_assignments)] + 2| |// expect-exit-status-1 + 3| | + 4| |struct Firework where T: Copy + std::fmt::Display { + 5| | strength: T, + 6| |} + 7| | + 8| |impl Firework where T: Copy + std::fmt::Display { + 9| | #[inline(always)] + 10| 3| fn set_strength(&mut self, new_strength: T) { + 11| 3| self.strength = new_strength; + 12| 3| } + ------------------ + | >::set_strength: + | 10| 2| fn set_strength(&mut self, new_strength: T) { + | 11| 2| self.strength = new_strength; + | 12| 2| } + ------------------ + | >::set_strength: + | 10| 1| fn set_strength(&mut self, new_strength: T) { + | 11| 1| self.strength = new_strength; + | 12| 1| } + ------------------ + 13| |} + 14| | + 15| |impl Drop for Firework where T: Copy + std::fmt::Display { + 16| | #[inline(always)] + 17| 2| fn drop(&mut self) { + 18| 2| println!("BOOM times {}!!!", self.strength); + 19| 2| } + ------------------ + | as core::ops::drop::Drop>::drop: + | 17| 1| fn drop(&mut self) { + | 18| 1| println!("BOOM times {}!!!", self.strength); + | 19| 1| } + ------------------ + | as core::ops::drop::Drop>::drop: + | 17| 1| fn drop(&mut self) { + | 18| 1| println!("BOOM times {}!!!", self.strength); + | 19| 1| } + ------------------ + 20| |} + 21| | + 22| |fn main() -> Result<(),u8> { + 23| 1| let mut firecracker = Firework { strength: 1 }; + 24| 1| firecracker.set_strength(2); + 25| 1| + 26| 1| let mut tnt = Firework { strength: 100.1 }; + 27| 1| tnt.set_strength(200.1); + 28| 1| tnt.set_strength(300.3); + 29| | + 30| 1| if true { + 31| 1| println!("Exiting with error..."); + 32| 1| return Err(1); + 33| | } + 34| | + 35| | let _ = Firework { strength: 1000 }; + 36| | + 37| | Ok(()) + 38| 1|} + 39| | + 40| |// Expected program output: + 41| |// Exiting with error... + 42| |// BOOM times 100!!! + 43| |// BOOM times 1!!! + 44| |// Error: 1 + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.if.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.if.txt new file mode 100644 index 0000000000000..bc2f9b108b2f3 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.if.txt @@ -0,0 +1,29 @@ + 1| |#![allow(unused_assignments, unused_variables)] + 2| | + 3| |fn main() { + 4| | // Initialize test constants in a way that cannot be determined at compile time, to ensure + 5| | // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + 6| | // dependent conditions. + 7| | let + 8| 1| is_true + 9| 1| = + 10| 1| std::env::args().len() + 11| 1| == + 12| 1| 1 + 13| 1| ; + 14| 1| let + 15| 1| mut + 16| 1| countdown + 17| 1| = + 18| 1| 0 + 19| | ; + 20| | if + 21| 1| is_true + 22| 1| { + 23| 1| countdown + 24| 1| = + 25| 1| 10 + 26| 1| ; + 27| 1| } + 28| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.if_else.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.if_else.txt new file mode 100644 index 0000000000000..5f899723e2554 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.if_else.txt @@ -0,0 +1,41 @@ + 1| |#![allow(unused_assignments)] + 2| | + 3| |fn main() { + 4| | // Initialize test constants in a way that cannot be determined at compile time, to ensure + 5| | // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + 6| | // dependent conditions. + 7| 1| let is_true = std::env::args().len() == 1; + 8| 1| + 9| 1| let mut countdown = 0; + 10| 1| if + 11| 1| is_true + 12| 1| { + 13| 1| countdown + 14| 1| = + 15| 1| 10 + 16| 1| ; + 17| 1| } + 18| | else // Note coverage region difference without semicolon + 19| | { + 20| 0| countdown + 21| 0| = + 22| 0| 100 + 23| | } + 24| | + 25| | if + 26| 1| is_true + 27| 1| { + 28| 1| countdown + 29| 1| = + 30| 1| 10 + 31| 1| ; + 32| 1| } + 33| | else + 34| 0| { + 35| 0| countdown + 36| 0| = + 37| 0| 100 + 38| 0| ; + 39| 0| } + 40| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.inner_items.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.inner_items.txt new file mode 100644 index 0000000000000..364d25b1646f4 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.inner_items.txt @@ -0,0 +1,58 @@ + 1| |#![allow(unused_assignments, unused_variables)] + 2| | + 3| |fn main() { + 4| | // Initialize test constants in a way that cannot be determined at compile time, to ensure + 5| | // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + 6| | // dependent conditions. + 7| 1| let is_true = std::env::args().len() == 1; + 8| 1| + 9| 1| let mut countdown = 0; + 10| 1| if is_true { + 11| 1| countdown = 10; + 12| 1| } + 13| | + 14| | mod inner_mod { + 15| | const INNER_MOD_CONST: u32 = 1000; + 16| | } + 17| | + 18| | fn inner_function(a: u32) { + 19| 3| let b = 1; + 20| 3| let c = a + b; + 21| 3| println!("c = {}", c) + 22| 3| } + 23| | + 24| | struct InnerStruct { + 25| | inner_struct_field: u32, + 26| | } + 27| | + 28| | const INNER_CONST: u32 = 1234; + 29| | + 30| | trait InnerTrait { + 31| | fn inner_trait_func(&mut self, incr: u32); + 32| | + 33| 1| fn default_trait_func(&mut self) { + 34| 1| inner_function(INNER_CONST); + 35| 1| self.inner_trait_func(INNER_CONST); + 36| 1| } + 37| | } + 38| | + 39| | impl InnerTrait for InnerStruct { + 40| | fn inner_trait_func(&mut self, incr: u32) { + 41| 1| self.inner_struct_field += incr; + 42| 1| inner_function(self.inner_struct_field); + 43| 1| } + 44| | } + 45| | + 46| | type InnerType = String; + 47| | + 48| 1| if is_true { + 49| 1| inner_function(countdown); + 50| 1| } + 51| | + 52| 1| let mut val = InnerStruct { + 53| 1| inner_struct_field: 101, + 54| 1| }; + 55| 1| + 56| 1| val.default_trait_func(); + 57| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.lazy_boolean.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.lazy_boolean.txt new file mode 100644 index 0000000000000..ded4369751587 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.lazy_boolean.txt @@ -0,0 +1,44 @@ + 1| |#![allow(unused_assignments, unused_variables)] + 2| | + 3| |fn main() { + 4| | // Initialize test constants in a way that cannot be determined at compile time, to ensure + 5| | // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + 6| | // dependent conditions. + 7| 1| let is_true = std::env::args().len() == 1; + 8| 1| + 9| 1| let (mut a, mut b, mut c) = (0, 0, 0); + 10| 1| if is_true { + 11| 1| a = 1; + 12| 1| b = 10; + 13| 1| c = 100; + 14| 1| } + 15| | let + 16| 1| somebool + 17| | = + 18| 1| a < b + 19| | || + 20| 0| b < c + 21| | ; + 22| | let + 23| 1| somebool + 24| | = + 25| 1| b < a + 26| | || + 27| 1| b < c + 28| | ; + 29| | let + 30| 1| somebool + 31| | = + 32| 1| a < b + 33| | && + 34| 1| b < c + 35| | ; + 36| | let + 37| 1| somebool + 38| | = + 39| 1| b < a + 40| | && + 41| 0| b < c + 42| | ; + 43| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.loop_break_value.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.loop_break_value.txt new file mode 100644 index 0000000000000..b0d668c6d76da --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.loop_break_value.txt @@ -0,0 +1,14 @@ + 1| |#![allow(unused_assignments)] + 2| | + 3| 1|fn main() { + 4| 1| let result + 5| 1| = + 6| 1| loop + 7| 1| { + 8| 1| break + 9| 1| 10 + 10| 1| ; + 11| 1| } + 12| 1| ; + 13| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.question_mark_error_result.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.question_mark_error_result.txt new file mode 100644 index 0000000000000..ae288d7d7a000 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.question_mark_error_result.txt @@ -0,0 +1,36 @@ + 1| |#![allow(unused_assignments)] + 2| |// expect-exit-status-1 + 3| | + 4| |fn call(return_error: bool) -> Result<(),()> { + 5| 6| if return_error { + 6| 1| Err(()) + 7| | } else { + 8| 5| Ok(()) + 9| | } + 10| 6|} + 11| | + 12| |fn main() -> Result<(),()> { + 13| 1| let mut + 14| 1| countdown = 10 + 15| | ; + 16| 6| for + 17| 6| _ + 18| | in + 19| 1| 0..10 + 20| | { + 21| 6| countdown + 22| 6| -= 1 + 23| | ; + 24| | if + 25| 6| countdown < 5 + 26| | { + 27| 1| call(/*return_error=*/ true)?; + 28| | } + 29| | else + 30| | { + 31| 5| call(/*return_error=*/ false)?; + 32| | } + 33| | } + 34| 0| Ok(()) + 35| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.simple_loop.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.simple_loop.txt new file mode 100644 index 0000000000000..f1acb7c545940 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.simple_loop.txt @@ -0,0 +1,36 @@ + 1| |#![allow(unused_assignments)] + 2| | + 3| |fn main() { + 4| | // Initialize test constants in a way that cannot be determined at compile time, to ensure + 5| | // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + 6| | // dependent conditions. + 7| 1| let is_true = std::env::args().len() == 1; + 8| 1| + 9| 1| let mut countdown = 0; + 10| | + 11| | if + 12| 1| is_true + 13| 1| { + 14| 1| countdown + 15| 1| = + 16| 1| 10 + 17| 1| ; + 18| 1| } + 19| | + 20| | loop + 21| | { + 22| | if + 23| 11| countdown + 24| 11| == + 25| 11| 0 + 26| | { + 27| 1| break + 28| | ; + 29| | } + 30| 10| countdown + 31| 10| -= + 32| 10| 1 + 33| | ; + 34| | } + 35| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.simple_match.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.simple_match.txt new file mode 100644 index 0000000000000..e42f22cd047fc --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.simple_match.txt @@ -0,0 +1,44 @@ + 1| |#![allow(unused_assignments)] + 2| | + 3| |fn main() { + 4| | // Initialize test constants in a way that cannot be determined at compile time, to ensure + 5| | // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + 6| | // dependent conditions. + 7| 1| let is_true = std::env::args().len() == 1; + 8| 1| + 9| 1| let mut countdown = 1; + 10| 1| if is_true { + 11| 1| countdown = 0; + 12| 1| } + 13| | + 14| 3| for + 15| 3| _ + 16| | in + 17| 1| 0..2 + 18| | { + 19| | let z + 20| | ; + 21| | match + 22| 2| countdown + 23| 2| { + 24| 2| x + 25| 2| if + 26| 2| x + 27| 2| < + 28| 2| 1 + 29| | => + 30| 1| { + 31| 1| z = countdown + 32| 1| ; + 33| 1| let y = countdown + 34| 1| ; + 35| 1| countdown = 10 + 36| 1| ; + 37| 1| } + 38| | _ + 39| | => + 40| 1| {} + 41| | } + 42| | } + 43| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.various_conditions.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.various_conditions.txt new file mode 100644 index 0000000000000..173ff4aa4c481 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.various_conditions.txt @@ -0,0 +1,69 @@ + 1| |#![allow(unused_assignments, unused_variables)] + 2| | + 3| |fn main() { + 4| 1| let mut countdown = 0; + 5| 1| if true { + 6| 1| countdown = 10; + 7| 1| } + 8| | + 9| | const B: u32 = 100; + 10| 1| let x = if countdown > 7 { + 11| 1| countdown -= 4; + 12| 1| B + 13| 0| } else if countdown > 2 { + 14| 0| if countdown < 1 || countdown > 5 || countdown != 9 { + 15| 0| countdown = 0; + 16| 0| } + 17| 0| countdown -= 5; + 18| 0| countdown + 19| | } else { + 20| 0| return; + 21| | }; + 22| | + 23| 1| let mut countdown = 0; + 24| 1| if true { + 25| 1| countdown = 10; + 26| 1| } + 27| | + 28| 1| if countdown > 7 { + 29| 1| countdown -= 4; + 30| 0| } else if countdown > 2 { + 31| 0| if countdown < 1 || countdown > 5 || countdown != 9 { + 32| 0| countdown = 0; + 33| 0| } + 34| 0| countdown -= 5; + 35| | } else { + 36| 0| return; + 37| | } + 38| | + 39| 1| let mut countdown = 0; + 40| 1| if true { + 41| 1| countdown = 1; + 42| 1| } + 43| | + 44| 1| let z = if countdown > 7 { + ^0 + 45| 0| countdown -= 4; + 46| 1| } else if countdown > 2 { + 47| 0| if countdown < 1 || countdown > 5 || countdown != 9 { + 48| 0| countdown = 0; + 49| 0| } + 50| 0| countdown -= 5; + 51| | } else { + 52| 1| let should_be_reachable = countdown; + 53| 1| println!("reached"); + 54| 1| return; + 55| | }; + 56| | + 57| 0| let w = if countdown > 7 { + 58| 0| countdown -= 4; + 59| 0| } else if countdown > 2 { + 60| 0| if countdown < 1 || countdown > 5 || countdown != 9 { + 61| 0| countdown = 0; + 62| 0| } + 63| 0| countdown -= 5; + 64| | } else { + 65| 0| return; + 66| | }; + 67| 1|} + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.while_early_return.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.while_early_return.txt new file mode 100644 index 0000000000000..7dce94f25f304 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/expected_show_coverage.while_early_return.txt @@ -0,0 +1,48 @@ + 1| |#![allow(unused_assignments)] + 2| |// expect-exit-status-1 + 3| | + 4| |fn main() -> Result<(),u8> { + 5| 1| let mut countdown = 10; + 6| 7| while + 7| 7| countdown + 8| 7| > + 9| 7| 0 + 10| | { + 11| | if + 12| 7| countdown + 13| 7| < + 14| 7| 5 + 15| | { + 16| | return + 17| | if + 18| 1| countdown + 19| 1| > + 20| 1| 8 + 21| | { + 22| 0| Ok(()) + 23| | } + 24| | else + 25| | { + 26| 1| Err(1) + 27| | } + 28| | ; + 29| | } + 30| 6| countdown + 31| 6| -= + 32| 6| 1 + 33| | ; + 34| | } + 35| 0| Ok(()) + 36| 1|} + 37| | + 38| |// ISSUE(77553): Originally, this test had `Err(1)` on line 22 (instead of `Ok(())`) and + 39| |// `std::process::exit(2)` on line 26 (instead of `Err(1)`); and this worked as expected on Linux + 40| |// and MacOS. But on Windows (MSVC, at least), the call to `std::process::exit()` exits the program + 41| |// without saving the InstrProf coverage counters. The use of `std::process:exit()` is not critical + 42| |// to the coverage test for early returns, but this is a limitation that should be fixed. + 43| |// + 44| |// FIXME(richkadel): Consider creating a new tests for coverage when calling `std::process::exit()`, + 45| |// move the `ISSUE` comment to that test, and implement a new test directive that supports skipping + 46| |// coverage tests when targeting specific platforms (at least skipping Windows, or MSVC if the + 47| |// problem exists on MSVC only). + diff --git a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/typical_show_coverage.coverage_of_if_else.txt b/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/typical_show_coverage.coverage_of_if_else.txt deleted file mode 100644 index 87ce3b4048f2a..0000000000000 --- a/src/test/run-make-fulldeps/instrument-coverage-cov-reports-link-dead-code/typical_show_coverage.coverage_of_if_else.txt +++ /dev/null @@ -1,64 +0,0 @@ - 1| |#![allow(unused_assignments)] - 2| | - 3| |fn main() { - 4| | let mut countdown = 0; - 5| 2| if true { - ^1 - 6| 2| countdown = 10; - 7| 2| } - 8| | - 9| 2| if countdown > 7 { - ^1 - 10| 2| countdown -= 4; - ^1 - 11| 2| } else if countdown > 2 { - ^0 ^0 - 12| 0| if countdown < 1 || countdown > 5 || countdown != 9 { - 13| 0| countdown = 0; - 14| 0| } - 15| 0| countdown -= 5; - 16| 0| } else { - 17| 0| return; - 18| 0| } - 19| | - 20| | let mut countdown = 0; - 21| 2| if true { - ^1 - 22| 2| countdown = 10; - 23| 2| } - 24| | - 25| 2| if countdown > 7 { - ^1 - 26| 2| countdown -= 4; - ^1 - 27| 2| } else if countdown > 2 { - ^0 ^0 - 28| 0| if countdown < 1 || countdown > 5 || countdown != 9 { - 29| 0| countdown = 0; - 30| 0| } - 31| 0| countdown -= 5; - 32| 0| } else { - 33| 0| return; - 34| 0| } - 35| | - 36| | let mut countdown = 0; - 37| 2| if true { - ^1 - 38| 2| countdown = 10; - 39| 2| } - 40| | - 41| 2| if countdown > 7 { - ^1 - 42| 2| countdown -= 4; - ^1 - 43| 2| } else if countdown > 2 { - ^0 ^0 - 44| 0| if countdown < 1 || countdown > 5 || countdown != 9 { - 45| 0| countdown = 0; - 46| 0| } - 47| 0| countdown -= 5; - 48| 0| } else { - 49| 0| return; - 50| 0| } - 51| 1|} - diff --git a/src/test/run-make-fulldeps/instrument-coverage-llvm-ir-link-dead-code/Makefile b/src/test/run-make-fulldeps/instrument-coverage-llvm-ir-link-dead-code/Makefile index ba2126a6b3f1d..c42530e233e82 100644 --- a/src/test/run-make-fulldeps/instrument-coverage-llvm-ir-link-dead-code/Makefile +++ b/src/test/run-make-fulldeps/instrument-coverage-llvm-ir-link-dead-code/Makefile @@ -8,4 +8,4 @@ LINK_DEAD_CODE=yes # ISSUE(76038): When targeting MSVC, Rust binaries built with both `-Z instrument-coverage` and # `-C link-dead-code` typically crash (with a seg-fault) or at best generate an empty `*.profraw`. -# See ../instrument-coverage/coverage_tools.mk for more information. \ No newline at end of file +# See ../instrument-coverage/coverage_tools.mk for more information. diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/Makefile b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/Makefile index 5cd425979ea49..f59c72dd399df 100644 --- a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/Makefile +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/Makefile @@ -20,12 +20,9 @@ endif -include clear_expected_if_blessed %: $(SOURCEDIR)/%.rs - # Compile the test program with "experimental" coverage instrumentation and generate relevant MIR. - # - # FIXME(richkadel): `-Zexperimental-coverage` to `-Zinstrument-coverage` once we are - # satisfied with the branch-level instrumentation. + # Compile the test program with coverage instrumentation and generate relevant MIR. $(RUSTC) $(SOURCEDIR)/$@.rs \ - -Zexperimental-coverage \ + -Zinstrument-coverage \ -Clink-dead-code=$(LINK_DEAD_CODE) \ -Zdump-mir=InstrumentCoverage \ -Zdump-mir-dir="$(TMPDIR)"/mir_dump.$@ diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main-{closure#0}.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main-{closure#0}.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..43f75c574d0ee --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main-{closure#0}.-------.InstrumentCoverage.0.html @@ -0,0 +1,82 @@ + + + +closure.main-{closure#0} - Coverage Spans + + + +
|| + { + let @0⦊mut countdown = 0⦉@0; + if @0⦊is_false⦉@0 @1,3⦊{ + countdown = 10; + }⦉@1,3 + @4,5⦊"alt string 2".to_owned() + }⦉@4,5
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main-{closure#1}.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main-{closure#1}.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..8f07ec5fcde66 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main-{closure#1}.-------.InstrumentCoverage.0.html @@ -0,0 +1,82 @@ + + + +closure.main-{closure#1} - Coverage Spans + + + +
|| + { + let @0⦊mut countdown = 0⦉@0; + if @0⦊is_false⦉@0 @1,3⦊{ + countdown = 10; + }⦉@1,3 + @4,5⦊"alt string 4".to_owned() + }⦉@4,5
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main-{closure#2}.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main-{closure#2}.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..ca9031a1094a4 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main-{closure#2}.-------.InstrumentCoverage.0.html @@ -0,0 +1,82 @@ + + + +closure.main-{closure#2} - Coverage Spans + + + +
|| + { + let @0⦊mut countdown = 0⦉@0; + if @0⦊is_false⦉@0 @1,3⦊{ + countdown = 10; + }⦉@1,3 + @4,5⦊"alt string 1".to_owned() + }⦉@4,5
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main-{closure#3}.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main-{closure#3}.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..820f8d9c6cf8f --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main-{closure#3}.-------.InstrumentCoverage.0.html @@ -0,0 +1,82 @@ + + + +closure.main-{closure#3} - Coverage Spans + + + +
|| + { + let @0⦊mut countdown = 0⦉@0; + if @0⦊is_false⦉@0 @1,3⦊{ + countdown = 10; + }⦉@1,3 + @4,5⦊"alt string 3".to_owned() + }⦉@4,5
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..740db3658646f --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.closure/closure.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,4505 @@ + + + +closure.main - Coverage Spans + + + +
fn main() @0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34⦊{ + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let is_true = std::env::args().len() == 1; + let is_false = ! is_true; + + let mut some_string = Some(String::from("the string content")); + println!( + "The string or alt: {}" + , + some_string + . + unwrap_or_else + ( + ⦉@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34|| + { + let mut countdown = 0; + if is_false { + countdown = 10; + } + "alt string 1".to_owned() + }@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34⦊ + ) + ); + + some_string = Some(String::from("the string content")); + let + a + = + ⦉@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34|| + { + let mut countdown = 0; + if is_false { + countdown = 10; + } + "alt string 2".to_owned() + }@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34⦊; + println!( + "The string or alt: {}" + , + some_string + . + unwrap_or_else + ( + a + ) + ); + + some_string = None; + println!( + "The string or alt: {}" + , + some_string + . + unwrap_or_else + ( + ⦉@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34|| + { + let mut countdown = 0; + if is_false { + countdown = 10; + } + "alt string 3".to_owned() + }@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34⦊ + ) + ); + + some_string = None; + let + a + = + ⦉@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34|| + { + let mut countdown = 0; + if is_false { + countdown = 10; + } + "alt string 4".to_owned() + }@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34⦊; + println!( + "The string or alt: {}" + , + some_string + . + unwrap_or_else + ( + a + ) + ); +}⦉@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.coverage_of_if_else/coverage_of_if_else.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.coverage_of_if_else/coverage_of_if_else.main.-------.InstrumentCoverage.0.html deleted file mode 100644 index fcb6afb263684..0000000000000 --- a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.coverage_of_if_else/coverage_of_if_else.main.-------.InstrumentCoverage.0.html +++ /dev/null @@ -1,641 +0,0 @@ - - - - coverage_of_if_else - Code Regions - - - -
fn main() { - let mut countdown = 0; - 1⦊3⦊2⦊if 0⦊true⦉0 { - countdown = 10; - }⦉2⦉3⦉1 - - 5⦊8⦊24⦊if 4⦊countdown > 7⦉4 { - 7⦊countdown -= 4⦉7; - } else 9⦊if 6⦊countdown > 2⦉6 { - 20⦊22⦊21⦊if 14⦊11⦊13⦊19⦊15⦊12⦊16⦊17⦊18⦊countdown < 1 || countdown > 5⦉18⦉17⦉16 || countdown != 9⦉12⦉15⦉19⦉13⦉11⦉14 { - countdown = 0; - 23⦊}⦉20⦉22⦉21⦉21 - countdown -= 5⦉23; - } else { - return; - }⦉9⦉24⦉8⦉5 - - let mut countdown = 0; - 27⦊28⦊26⦊if 25⦊true⦉25 { - countdown = 10; - }⦉26⦉28⦉27 - - 49⦊33⦊30⦊if 29⦊countdown > 7⦉29 { - 32⦊countdown -= 4⦉32; - } else 34⦊if 31⦊countdown > 2⦉31 { - 46⦊47⦊45⦊if 44⦊38⦊39⦊40⦊36⦊37⦊41⦊42⦊43⦊countdown < 1 || countdown > 5⦉43⦉42⦉41 || countdown != 9⦉37⦉36⦉40⦉39⦉38⦉44 { - countdown = 0; - 48⦊}⦉46⦉47⦉45⦉45 - countdown -= 5⦉48; - } else { - return; - }⦉34⦉30⦉33⦉49 - - let mut countdown = 0; - 52⦊51⦊53⦊if 50⦊true⦉50 { - countdown = 10; - }⦉53⦉51⦉52 - - 74⦊55⦊58⦊if 54⦊countdown > 7⦉54 { - 57⦊countdown -= 4⦉57; - } else 59⦊if 56⦊countdown > 2⦉56 { - 71⦊72⦊70⦊if 63⦊64⦊65⦊62⦊69⦊61⦊67⦊68⦊66⦊countdown < 1 || countdown > 5⦉66⦉68⦉67 || countdown != 9⦉61⦉69⦉62⦉65⦉64⦉63 { - countdown = 0; - 73⦊}⦉71⦉72⦉70⦉70 - countdown -= 5⦉73; - } else { - return; - }⦉59⦉58⦉55⦉74 -75⦊}⦉7577⦊⦉77
- - diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.drop_trait/drop_trait.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.drop_trait/drop_trait.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..494e6f20ea763 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.drop_trait/drop_trait.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,119 @@ + + + +drop_trait.main - Coverage Spans + + + +
fn main() -> Result<(),u8> { + let @0⦊_firecracker = Firework { strength: 1 }; + + let _tnt = Firework { strength: 100 }⦉@0; + + if @0⦊true⦉@0 { + @1,3,4,5,9,10⦊println!("Exiting with error..."); + return Err(1)⦉@1,3,4,5,9,10; + } + + let _ = @2,6,7,8⦊Firework { strength: 1000 }; + + Ok(())⦉@2,6,7,8 +}@11⦊⦉@11
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.drop_trait/drop_trait.{impl#0}-drop.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.drop_trait/drop_trait.{impl#0}-drop.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..9530d12fb493e --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.drop_trait/drop_trait.{impl#0}-drop.-------.InstrumentCoverage.0.html @@ -0,0 +1,123 @@ + + + +drop_trait.{impl#0}-drop - Coverage Spans + + + +
fn drop(&mut self) @0,1,2,3⦊{ + println!("BOOM times {}!!!", self.strength); + }⦉@0,1,2,3
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.generics/generics.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.generics/generics.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..6dc893d28ff52 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.generics/generics.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,167 @@ + + + +generics.main - Coverage Spans + + + +
fn main() -> Result<(),u8> { + let @0,1,2,3⦊mut firecracker = Firework { strength: 1 }; + firecracker.set_strength(2); + + let mut tnt = Firework { strength: 100.1 }; + tnt.set_strength(200.1); + tnt.set_strength(300.3)⦉@0,1,2,3; + + if @0,1,2,3⦊true⦉@0,1,2,3 { + @4,6,7,8,12,13⦊println!("Exiting with error..."); + return Err(1)⦉@4,6,7,8,12,13; + } + + let _ = @5,9,10,11⦊Firework { strength: 1000 }; + + Ok(())⦉@5,9,10,11 +}@14⦊⦉@14
+ + diff --git a/src/test/mir-opt/spanview_block.main.mir_map.0.html.mir b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.generics/generics.{impl#0}-set_strength.-------.InstrumentCoverage.0.html similarity index 59% rename from src/test/mir-opt/spanview_block.main.mir_map.0.html.mir rename to src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.generics/generics.{impl#0}-set_strength.-------.InstrumentCoverage.0.html index f8ecf14376723..e31e47b81d4fc 100644 --- a/src/test/mir-opt/spanview_block.main.mir_map.0.html.mir +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.generics/generics.{impl#0}-set_strength.-------.InstrumentCoverage.0.html @@ -1,8 +1,8 @@ - coverage_of_if_else - Code Regions - + -
fn main() 0⦊{}⦉0
+
fn set_strength(&mut self, new_strength: T) @0⦊{ + self.strength = new_strength; + }⦉@0
diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.generics/generics.{impl#1}-drop.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.generics/generics.{impl#1}-drop.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..99a7df4a67053 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.generics/generics.{impl#1}-drop.-------.InstrumentCoverage.0.html @@ -0,0 +1,123 @@ + + + +generics.{impl#1}-drop - Coverage Spans + + + +
fn drop(&mut self) @0,1,2,3⦊{ + println!("BOOM times {}!!!", self.strength); + }⦉@0,1,2,3
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.if/if.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.if/if.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..0379d900e9409 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.if/if.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,162 @@ + + + +if.main - Coverage Spans + + + +
fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let + @0,1,2,3⦊is_true + = + std::env::args().len() + == + 1 + ; + let + mut + countdown + = + 0⦉@0,1,2,3 + ; + if + @0,1,2,3⦊is_true⦉@0,1,2,3 + @4,6⦊{ + countdown + = + 10 + ; + }⦉@4,6 +}@7⦊⦉@7
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.if_else/if_else.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.if_else/if_else.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..b51c5c84c0d6e --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.if_else/if_else.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,163 @@ + + + +if_else.main - Coverage Spans + + + +
fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let @0,1,2,3⦊is_true = std::env::args().len() == 1; + + let mut countdown = 0; + if + is_true⦉@0,1,2,3 + @4,6⦊{ + countdown + = + 10 + ; + }⦉@4,6 + else // Note coverage region difference without semicolon + { + @5⦊countdown + = + 100⦉@5 + } + + if + @7⦊is_true⦉@7 + @8,10⦊{ + countdown + = + 10 + ; + }⦉@8,10 + else + @9⦊{ + countdown + = + 100 + ; + }⦉@9 +}@11⦊⦉@11
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.inner_items/inner_items.main-InnerTrait-default_trait_func.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.inner_items/inner_items.main-InnerTrait-default_trait_func.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..6998bb32a3f56 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.inner_items/inner_items.main-InnerTrait-default_trait_func.-------.InstrumentCoverage.0.html @@ -0,0 +1,83 @@ + + + +inner_items.main-InnerTrait-default_trait_func - Coverage Spans + + + +
fn default_trait_func(&mut self) @0,1,2⦊{ + inner_function(INNER_CONST); + self.inner_trait_func(INNER_CONST); + }⦉@0,1,2
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.inner_items/inner_items.main-inner_function.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.inner_items/inner_items.main-inner_function.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..c87ff329f3fe8 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.inner_items/inner_items.main-inner_function.-------.InstrumentCoverage.0.html @@ -0,0 +1,107 @@ + + + +inner_items.main-inner_function - Coverage Spans + + + +
fn inner_function(a: u32) { + let @0⦊b = 1⦉@0; + let @1,2,3,4⦊c⦉@1,2,3,4 = @0⦊a + b⦉@0; + @1,2,3,4⦊println!("c = {}", c) + }⦉@1,2,3,4
+ + diff --git a/src/test/mir-opt/spanview_terminator.main.mir_map.0.html.mir b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.inner_items/inner_items.main-{impl#0}-inner_trait_func.-------.InstrumentCoverage.0.html similarity index 53% rename from src/test/mir-opt/spanview_terminator.main.mir_map.0.html.mir rename to src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.inner_items/inner_items.main-{impl#0}-inner_trait_func.-------.InstrumentCoverage.0.html index 984b021384b08..1dc29d400d8e0 100644 --- a/src/test/mir-opt/spanview_terminator.main.mir_map.0.html.mir +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.inner_items/inner_items.main-{impl#0}-inner_trait_func.-------.InstrumentCoverage.0.html @@ -1,8 +1,8 @@ - coverage_of_if_else - Code Regions - + -
fn main() {}0:Return⦊⦉0:Return
+
fn inner_trait_func(&mut self, incr: u32) { + @0⦊self.inner_struct_field += incr⦉@0; + @1,2⦊inner_function(self.inner_struct_field); + }⦉@1,2
diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.inner_items/inner_items.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.inner_items/inner_items.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..9a1256025d971 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.inner_items/inner_items.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,171 @@ + + + +inner_items.main - Coverage Spans + + + +
fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let @0,1,2,3⦊is_true = std::env::args().len() == 1; + + let mut countdown = 0⦉@0,1,2,3; + if @0,1,2,3⦊is_true⦉@0,1,2,3 @4,6⦊{ + countdown = 10; + }⦉@4,6 + + mod inner_mod { + const INNER_MOD_CONST: u32 = 1000; + } + + fn inner_function(a: u32) { + let b = 1; + let c = a + b; + println!("c = {}", c) + } + + struct InnerStruct { + inner_struct_field: u32, + } + + const INNER_CONST: u32 = 1234; + + trait InnerTrait { + fn inner_trait_func(&mut self, incr: u32); + + fn default_trait_func(&mut self) { + inner_function(INNER_CONST); + self.inner_trait_func(INNER_CONST); + } + } + + impl InnerTrait for InnerStruct { + fn inner_trait_func(&mut self, incr: u32) { + self.inner_struct_field += incr; + inner_function(self.inner_struct_field); + } + } + + type InnerType = String; + + if @7⦊is_true⦉@7 @8,10,11⦊{ + inner_function(countdown); + }⦉@8,10,11 + + let @12,13⦊mut val = InnerStruct { + inner_struct_field: 101, + }; + + val.default_trait_func(); +}⦉@12,13
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.lazy_boolean/lazy_boolean.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.lazy_boolean/lazy_boolean.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..defe743df608f --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.lazy_boolean/lazy_boolean.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,160 @@ + + + +lazy_boolean.main - Coverage Spans + + + +
fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let @0,1,2,3⦊is_true = std::env::args().len() == 1; + + let (mut a, mut b, mut c) = (0, 0, 0)⦉@0,1,2,3; + if @0,1,2,3⦊is_true⦉@0,1,2,3 @4,6⦊{ + a = 1; + b = 10; + c = 100; + }⦉@4,6 + let + @11⦊somebool⦉@11 + = + @7⦊a < b⦉@7 + || + @10⦊b < c⦉@10 + ; + let + @15⦊somebool⦉@15 + = + @11⦊b < a⦉@11 + || + @14⦊b < c⦉@14 + ; + let + @19⦊somebool⦉@19 + = + @15⦊a < b⦉@15 + && + @18⦊b < c⦉@18 + ; + let + @23⦊somebool⦉@23 + = + @19⦊b < a⦉@19 + && + @22⦊b < c⦉@22 + ; +}@23⦊⦉@23
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.loop_break_value/loop_break_value.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.loop_break_value/loop_break_value.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..dc26c796637cc --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.loop_break_value/loop_break_value.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,118 @@ + + + +loop_break_value.main - Coverage Spans + + + +
fn main() @0,1⦊{ + let result + = + loop + { + break + 10 + ; + } + ; +}⦉@0,1
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.question_mark_error_result/question_mark_error_result.call.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.question_mark_error_result/question_mark_error_result.call.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..0c253d5e5c253 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.question_mark_error_result/question_mark_error_result.call.-------.InstrumentCoverage.0.html @@ -0,0 +1,73 @@ + + + +question_mark_error_result.call - Coverage Spans + + + +
fn call(return_error: bool) -> Result<(),()> { + if @0⦊return_error⦉@0 { + @1,3⦊Err(())⦉@1,3 + } else { + @2⦊Ok(())⦉@2 + } +}@4⦊⦉@4
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.question_mark_error_result/question_mark_error_result.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.question_mark_error_result/question_mark_error_result.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..2589b0454da2e --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.question_mark_error_result/question_mark_error_result.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,101 @@ + + + +question_mark_error_result.main - Coverage Spans + + + +
fn main() -> Result<(),()> { + let @0,1⦊mut + countdown = 10⦉@0,1 + ; + @2,3,4⦊for + _⦉@2,3,4 + in + @0,1⦊0..10⦉@0,1 + { + @6,8⦊countdown + -= 1⦉@6,8 + ; + if + @9⦊countdown < 5⦉@9 + { + @10,12,13,14⦊call(/*return_error=*/ true)?⦉@10,12,13,14; + } + else + { + @11,21,22⦊call(/*return_error=*/ false)?⦉@11,21,22; + } + } + @5⦊Ok(())⦉@5 +}@31⦊⦉@31
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.simple_loop/simple_loop.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.simple_loop/simple_loop.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..4b21d3fc263a3 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.simple_loop/simple_loop.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,127 @@ + + + +simple_loop.main - Coverage Spans + + + +
fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let @0,1,2,3⦊is_true = std::env::args().len() == 1; + + let mut countdown = 0⦉@0,1,2,3; + + if + @0,1,2,3⦊is_true⦉@0,1,2,3 + @4,6⦊{ + countdown + = + 10 + ; + }⦉@4,6 + + loop + { + if + @8,9⦊countdown + == + 0⦉@8,9 + { + @10,12⦊break⦉@10,12 + ; + } + @11⦊countdown + -= + 1⦉@11 + ; + } +}@10,12⦊⦉@10,12
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.simple_match/simple_match.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.simple_match/simple_match.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..5ba770ef6078e --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.simple_match/simple_match.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,190 @@ + + + +simple_match.main - Coverage Spans + + + +
fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let @0,1,2,3⦊is_true = std::env::args().len() == 1; + + let mut countdown = 1⦉@0,1,2,3; + if @0,1,2,3⦊is_true⦉@0,1,2,3 @4,6⦊{ + countdown = 0; + }⦉@4,6 + + @9,10,11⦊for + _⦉@9,10,11 + in + @7,8⦊0..2⦉@7,8 + { + let z + ; + match + @13,15,17⦊countdown + { + x + if + x + < + 1⦉@13,15,17 + => + @18⦊{ + z = countdown + ; + let y = countdown + ; + countdown = 10 + ; + }⦉@18 + _ + => + @16⦊{}⦉@16 + } + } +}@12⦊⦉@12
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.various_conditions/various_conditions.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.various_conditions/various_conditions.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..28f1d013c835f --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.various_conditions/various_conditions.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,228 @@ + + + +various_conditions.main - Coverage Spans + + + +
fn main() { + let @0⦊mut countdown = 0⦉@0; + if @0⦊true⦉@0 @1,3⦊{ + countdown = 10; + }⦉@1,3 + + const B: u32 = 100; + let @25⦊x⦉@25 = if @4⦊countdown > 7⦉@4 { + @5,7⦊countdown -= 4⦉@5,7; + @8⦊B⦉@8 + } else if @6⦊countdown > 2⦉@6 { + if @9,11⦊countdown < 1⦉@9,11 || @18⦊countdown > 5⦉@18 || @14⦊countdown != 9⦉@14 @20,22⦊{ + countdown = 0; + }⦉@20,22 + @23⦊countdown -= 5⦉@23; + @24⦊countdown⦉@24 + } else { + @10⦊return⦉@10; + }; + + let @25⦊mut countdown = 0⦉@25; + if @25⦊true⦉@25 @26,28⦊{ + countdown = 10; + }⦉@26,28 + + if @29⦊countdown > 7⦉@29 { + @30,32⦊countdown -= 4⦉@30,32; + } else if @31⦊countdown > 2⦉@31 { + if @34,36⦊countdown < 1⦉@34,36 || @43⦊countdown > 5⦉@43 || @39⦊countdown != 9⦉@39 @45,47⦊{ + countdown = 0; + }⦉@45,47 + @48⦊countdown -= 5⦉@48; + } else { + @35⦊return⦉@35; + } + + let @50⦊mut countdown = 0⦉@50; + if @50⦊true⦉@50 @51,53⦊{ + countdown = 1; + }⦉@51,53 + + let @77⦊z⦉@77 = if @54⦊countdown > 7⦉@54 { + @55,57⦊countdown -= 4⦉@55,57; + } else if @56⦊countdown > 2⦉@56 { + if @59,61⦊countdown < 1⦉@59,61 || @68⦊countdown > 5⦉@68 || @64⦊countdown != 9⦉@64 @70,72⦊{ + countdown = 0; + }⦉@70,72 + @73⦊countdown -= 5⦉@73; + } else { + let @60,75,76⦊should_be_reachable = countdown; + println!("reached"); + return⦉@60,75,76; + }; + + let @98⦊w⦉@98 = if @77⦊countdown > 7⦉@77 { + @78,80⦊countdown -= 4⦉@78,80; + } else if @79⦊countdown > 2⦉@79 { + if @82,84⦊countdown < 1⦉@82,84 || @91⦊countdown > 5⦉@91 || @87⦊countdown != 9⦉@87 @93,95⦊{ + countdown = 0; + }⦉@93,95 + @96⦊countdown -= 5⦉@96; + } else { + @83⦊return⦉@83; + }; +}@102⦊⦉@102
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.while_early_return/while_early_return.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.while_early_return/while_early_return.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..b96789a92194d --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-base/expected_mir_dump.while_early_return/while_early_return.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,119 @@ + + + +while_early_return.main - Coverage Spans + + + +
fn main() -> Result<(),u8> { + let @0⦊mut countdown = 10⦉@0; + @1,2⦊while + countdown + > + 0⦉@1,2 + { + if + @3,5⦊countdown + < + 5⦉@3,5 + { + return + if + @6,8⦊countdown + > + 8⦉@6,8 + { + @9,11⦊Ok(())⦉@9,11 + } + else + { + @10⦊Err(1)⦉@10 + } + ; + } + @7⦊countdown + -= + 1⦉@7 + ; + } + @4⦊Ok(())⦉@4 +}@14⦊⦉@14
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/Makefile b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/Makefile index 0578949b3c820..2d82c3b345309 100644 --- a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/Makefile +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/Makefile @@ -8,4 +8,4 @@ LINK_DEAD_CODE=yes # ISSUE(76038): When targeting MSVC, Rust binaries built with both `-Z instrument-coverage` and # `-C link-dead-code` typically crash (with a seg-fault) or at best generate an empty `*.profraw`. -# See ../instrument-coverage/coverage_tools.mk for more information. \ No newline at end of file +# See ../instrument-coverage/coverage_tools.mk for more information. diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main-{closure#0}.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main-{closure#0}.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..43f75c574d0ee --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main-{closure#0}.-------.InstrumentCoverage.0.html @@ -0,0 +1,82 @@ + + + +closure.main-{closure#0} - Coverage Spans + + + +
|| + { + let @0⦊mut countdown = 0⦉@0; + if @0⦊is_false⦉@0 @1,3⦊{ + countdown = 10; + }⦉@1,3 + @4,5⦊"alt string 2".to_owned() + }⦉@4,5
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main-{closure#1}.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main-{closure#1}.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..8f07ec5fcde66 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main-{closure#1}.-------.InstrumentCoverage.0.html @@ -0,0 +1,82 @@ + + + +closure.main-{closure#1} - Coverage Spans + + + +
|| + { + let @0⦊mut countdown = 0⦉@0; + if @0⦊is_false⦉@0 @1,3⦊{ + countdown = 10; + }⦉@1,3 + @4,5⦊"alt string 4".to_owned() + }⦉@4,5
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main-{closure#2}.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main-{closure#2}.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..ca9031a1094a4 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main-{closure#2}.-------.InstrumentCoverage.0.html @@ -0,0 +1,82 @@ + + + +closure.main-{closure#2} - Coverage Spans + + + +
|| + { + let @0⦊mut countdown = 0⦉@0; + if @0⦊is_false⦉@0 @1,3⦊{ + countdown = 10; + }⦉@1,3 + @4,5⦊"alt string 1".to_owned() + }⦉@4,5
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main-{closure#3}.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main-{closure#3}.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..820f8d9c6cf8f --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main-{closure#3}.-------.InstrumentCoverage.0.html @@ -0,0 +1,82 @@ + + + +closure.main-{closure#3} - Coverage Spans + + + +
|| + { + let @0⦊mut countdown = 0⦉@0; + if @0⦊is_false⦉@0 @1,3⦊{ + countdown = 10; + }⦉@1,3 + @4,5⦊"alt string 3".to_owned() + }⦉@4,5
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..740db3658646f --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.closure/closure.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,4505 @@ + + + +closure.main - Coverage Spans + + + +
fn main() @0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34⦊{ + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let is_true = std::env::args().len() == 1; + let is_false = ! is_true; + + let mut some_string = Some(String::from("the string content")); + println!( + "The string or alt: {}" + , + some_string + . + unwrap_or_else + ( + ⦉@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34|| + { + let mut countdown = 0; + if is_false { + countdown = 10; + } + "alt string 1".to_owned() + }@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34⦊ + ) + ); + + some_string = Some(String::from("the string content")); + let + a + = + ⦉@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34|| + { + let mut countdown = 0; + if is_false { + countdown = 10; + } + "alt string 2".to_owned() + }@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34⦊; + println!( + "The string or alt: {}" + , + some_string + . + unwrap_or_else + ( + a + ) + ); + + some_string = None; + println!( + "The string or alt: {}" + , + some_string + . + unwrap_or_else + ( + ⦉@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34|| + { + let mut countdown = 0; + if is_false { + countdown = 10; + } + "alt string 3".to_owned() + }@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34⦊ + ) + ); + + some_string = None; + let + a + = + ⦉@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34|| + { + let mut countdown = 0; + if is_false { + countdown = 10; + } + "alt string 4".to_owned() + }@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34⦊; + println!( + "The string or alt: {}" + , + some_string + . + unwrap_or_else + ( + a + ) + ); +}⦉@0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.coverage_of_if_else/coverage_of_if_else.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.coverage_of_if_else/coverage_of_if_else.main.-------.InstrumentCoverage.0.html deleted file mode 100644 index fcb6afb263684..0000000000000 --- a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.coverage_of_if_else/coverage_of_if_else.main.-------.InstrumentCoverage.0.html +++ /dev/null @@ -1,641 +0,0 @@ - - - - coverage_of_if_else - Code Regions - - - -
fn main() { - let mut countdown = 0; - 1⦊3⦊2⦊if 0⦊true⦉0 { - countdown = 10; - }⦉2⦉3⦉1 - - 5⦊8⦊24⦊if 4⦊countdown > 7⦉4 { - 7⦊countdown -= 4⦉7; - } else 9⦊if 6⦊countdown > 2⦉6 { - 20⦊22⦊21⦊if 14⦊11⦊13⦊19⦊15⦊12⦊16⦊17⦊18⦊countdown < 1 || countdown > 5⦉18⦉17⦉16 || countdown != 9⦉12⦉15⦉19⦉13⦉11⦉14 { - countdown = 0; - 23⦊}⦉20⦉22⦉21⦉21 - countdown -= 5⦉23; - } else { - return; - }⦉9⦉24⦉8⦉5 - - let mut countdown = 0; - 27⦊28⦊26⦊if 25⦊true⦉25 { - countdown = 10; - }⦉26⦉28⦉27 - - 49⦊33⦊30⦊if 29⦊countdown > 7⦉29 { - 32⦊countdown -= 4⦉32; - } else 34⦊if 31⦊countdown > 2⦉31 { - 46⦊47⦊45⦊if 44⦊38⦊39⦊40⦊36⦊37⦊41⦊42⦊43⦊countdown < 1 || countdown > 5⦉43⦉42⦉41 || countdown != 9⦉37⦉36⦉40⦉39⦉38⦉44 { - countdown = 0; - 48⦊}⦉46⦉47⦉45⦉45 - countdown -= 5⦉48; - } else { - return; - }⦉34⦉30⦉33⦉49 - - let mut countdown = 0; - 52⦊51⦊53⦊if 50⦊true⦉50 { - countdown = 10; - }⦉53⦉51⦉52 - - 74⦊55⦊58⦊if 54⦊countdown > 7⦉54 { - 57⦊countdown -= 4⦉57; - } else 59⦊if 56⦊countdown > 2⦉56 { - 71⦊72⦊70⦊if 63⦊64⦊65⦊62⦊69⦊61⦊67⦊68⦊66⦊countdown < 1 || countdown > 5⦉66⦉68⦉67 || countdown != 9⦉61⦉69⦉62⦉65⦉64⦉63 { - countdown = 0; - 73⦊}⦉71⦉72⦉70⦉70 - countdown -= 5⦉73; - } else { - return; - }⦉59⦉58⦉55⦉74 -75⦊}⦉7577⦊⦉77
- - diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.drop_trait/drop_trait.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.drop_trait/drop_trait.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..494e6f20ea763 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.drop_trait/drop_trait.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,119 @@ + + + +drop_trait.main - Coverage Spans + + + +
fn main() -> Result<(),u8> { + let @0⦊_firecracker = Firework { strength: 1 }; + + let _tnt = Firework { strength: 100 }⦉@0; + + if @0⦊true⦉@0 { + @1,3,4,5,9,10⦊println!("Exiting with error..."); + return Err(1)⦉@1,3,4,5,9,10; + } + + let _ = @2,6,7,8⦊Firework { strength: 1000 }; + + Ok(())⦉@2,6,7,8 +}@11⦊⦉@11
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.drop_trait/drop_trait.{impl#0}-drop.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.drop_trait/drop_trait.{impl#0}-drop.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..9530d12fb493e --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.drop_trait/drop_trait.{impl#0}-drop.-------.InstrumentCoverage.0.html @@ -0,0 +1,123 @@ + + + +drop_trait.{impl#0}-drop - Coverage Spans + + + +
fn drop(&mut self) @0,1,2,3⦊{ + println!("BOOM times {}!!!", self.strength); + }⦉@0,1,2,3
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.generics/generics.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.generics/generics.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..6dc893d28ff52 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.generics/generics.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,167 @@ + + + +generics.main - Coverage Spans + + + +
fn main() -> Result<(),u8> { + let @0,1,2,3⦊mut firecracker = Firework { strength: 1 }; + firecracker.set_strength(2); + + let mut tnt = Firework { strength: 100.1 }; + tnt.set_strength(200.1); + tnt.set_strength(300.3)⦉@0,1,2,3; + + if @0,1,2,3⦊true⦉@0,1,2,3 { + @4,6,7,8,12,13⦊println!("Exiting with error..."); + return Err(1)⦉@4,6,7,8,12,13; + } + + let _ = @5,9,10,11⦊Firework { strength: 1000 }; + + Ok(())⦉@5,9,10,11 +}@14⦊⦉@14
+ + diff --git a/src/test/mir-opt/spanview_statement.main.mir_map.0.html.mir b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.generics/generics.{impl#0}-set_strength.-------.InstrumentCoverage.0.html similarity index 59% rename from src/test/mir-opt/spanview_statement.main.mir_map.0.html.mir rename to src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.generics/generics.{impl#0}-set_strength.-------.InstrumentCoverage.0.html index 8a34b8b5daecb..e31e47b81d4fc 100644 --- a/src/test/mir-opt/spanview_statement.main.mir_map.0.html.mir +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.generics/generics.{impl#0}-set_strength.-------.InstrumentCoverage.0.html @@ -1,8 +1,8 @@ - coverage_of_if_else - Code Regions - + -
fn main() 0[0]⦊{}⦉0[0]0:Return⦊⦉0:Return
+
fn set_strength(&mut self, new_strength: T) @0⦊{ + self.strength = new_strength; + }⦉@0
diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.generics/generics.{impl#1}-drop.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.generics/generics.{impl#1}-drop.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..99a7df4a67053 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.generics/generics.{impl#1}-drop.-------.InstrumentCoverage.0.html @@ -0,0 +1,123 @@ + + + +generics.{impl#1}-drop - Coverage Spans + + + +
fn drop(&mut self) @0,1,2,3⦊{ + println!("BOOM times {}!!!", self.strength); + }⦉@0,1,2,3
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.if/if.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.if/if.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..0379d900e9409 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.if/if.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,162 @@ + + + +if.main - Coverage Spans + + + +
fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let + @0,1,2,3⦊is_true + = + std::env::args().len() + == + 1 + ; + let + mut + countdown + = + 0⦉@0,1,2,3 + ; + if + @0,1,2,3⦊is_true⦉@0,1,2,3 + @4,6⦊{ + countdown + = + 10 + ; + }⦉@4,6 +}@7⦊⦉@7
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.if_else/if_else.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.if_else/if_else.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..b51c5c84c0d6e --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.if_else/if_else.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,163 @@ + + + +if_else.main - Coverage Spans + + + +
fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let @0,1,2,3⦊is_true = std::env::args().len() == 1; + + let mut countdown = 0; + if + is_true⦉@0,1,2,3 + @4,6⦊{ + countdown + = + 10 + ; + }⦉@4,6 + else // Note coverage region difference without semicolon + { + @5⦊countdown + = + 100⦉@5 + } + + if + @7⦊is_true⦉@7 + @8,10⦊{ + countdown + = + 10 + ; + }⦉@8,10 + else + @9⦊{ + countdown + = + 100 + ; + }⦉@9 +}@11⦊⦉@11
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.inner_items/inner_items.main-InnerTrait-default_trait_func.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.inner_items/inner_items.main-InnerTrait-default_trait_func.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..6998bb32a3f56 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.inner_items/inner_items.main-InnerTrait-default_trait_func.-------.InstrumentCoverage.0.html @@ -0,0 +1,83 @@ + + + +inner_items.main-InnerTrait-default_trait_func - Coverage Spans + + + +
fn default_trait_func(&mut self) @0,1,2⦊{ + inner_function(INNER_CONST); + self.inner_trait_func(INNER_CONST); + }⦉@0,1,2
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.inner_items/inner_items.main-inner_function.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.inner_items/inner_items.main-inner_function.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..c87ff329f3fe8 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.inner_items/inner_items.main-inner_function.-------.InstrumentCoverage.0.html @@ -0,0 +1,107 @@ + + + +inner_items.main-inner_function - Coverage Spans + + + +
fn inner_function(a: u32) { + let @0⦊b = 1⦉@0; + let @1,2,3,4⦊c⦉@1,2,3,4 = @0⦊a + b⦉@0; + @1,2,3,4⦊println!("c = {}", c) + }⦉@1,2,3,4
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.inner_items/inner_items.main-{impl#0}-inner_trait_func.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.inner_items/inner_items.main-{impl#0}-inner_trait_func.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..1dc29d400d8e0 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.inner_items/inner_items.main-{impl#0}-inner_trait_func.-------.InstrumentCoverage.0.html @@ -0,0 +1,72 @@ + + + +inner_items.main-{impl#0}-inner_trait_func - Coverage Spans + + + +
fn inner_trait_func(&mut self, incr: u32) { + @0⦊self.inner_struct_field += incr⦉@0; + @1,2⦊inner_function(self.inner_struct_field); + }⦉@1,2
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.inner_items/inner_items.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.inner_items/inner_items.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..9a1256025d971 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.inner_items/inner_items.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,171 @@ + + + +inner_items.main - Coverage Spans + + + +
fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let @0,1,2,3⦊is_true = std::env::args().len() == 1; + + let mut countdown = 0⦉@0,1,2,3; + if @0,1,2,3⦊is_true⦉@0,1,2,3 @4,6⦊{ + countdown = 10; + }⦉@4,6 + + mod inner_mod { + const INNER_MOD_CONST: u32 = 1000; + } + + fn inner_function(a: u32) { + let b = 1; + let c = a + b; + println!("c = {}", c) + } + + struct InnerStruct { + inner_struct_field: u32, + } + + const INNER_CONST: u32 = 1234; + + trait InnerTrait { + fn inner_trait_func(&mut self, incr: u32); + + fn default_trait_func(&mut self) { + inner_function(INNER_CONST); + self.inner_trait_func(INNER_CONST); + } + } + + impl InnerTrait for InnerStruct { + fn inner_trait_func(&mut self, incr: u32) { + self.inner_struct_field += incr; + inner_function(self.inner_struct_field); + } + } + + type InnerType = String; + + if @7⦊is_true⦉@7 @8,10,11⦊{ + inner_function(countdown); + }⦉@8,10,11 + + let @12,13⦊mut val = InnerStruct { + inner_struct_field: 101, + }; + + val.default_trait_func(); +}⦉@12,13
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.lazy_boolean/lazy_boolean.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.lazy_boolean/lazy_boolean.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..defe743df608f --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.lazy_boolean/lazy_boolean.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,160 @@ + + + +lazy_boolean.main - Coverage Spans + + + +
fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let @0,1,2,3⦊is_true = std::env::args().len() == 1; + + let (mut a, mut b, mut c) = (0, 0, 0)⦉@0,1,2,3; + if @0,1,2,3⦊is_true⦉@0,1,2,3 @4,6⦊{ + a = 1; + b = 10; + c = 100; + }⦉@4,6 + let + @11⦊somebool⦉@11 + = + @7⦊a < b⦉@7 + || + @10⦊b < c⦉@10 + ; + let + @15⦊somebool⦉@15 + = + @11⦊b < a⦉@11 + || + @14⦊b < c⦉@14 + ; + let + @19⦊somebool⦉@19 + = + @15⦊a < b⦉@15 + && + @18⦊b < c⦉@18 + ; + let + @23⦊somebool⦉@23 + = + @19⦊b < a⦉@19 + && + @22⦊b < c⦉@22 + ; +}@23⦊⦉@23
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.loop_break_value/loop_break_value.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.loop_break_value/loop_break_value.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..dc26c796637cc --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.loop_break_value/loop_break_value.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,118 @@ + + + +loop_break_value.main - Coverage Spans + + + +
fn main() @0,1⦊{ + let result + = + loop + { + break + 10 + ; + } + ; +}⦉@0,1
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.question_mark_error_result/question_mark_error_result.call.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.question_mark_error_result/question_mark_error_result.call.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..0c253d5e5c253 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.question_mark_error_result/question_mark_error_result.call.-------.InstrumentCoverage.0.html @@ -0,0 +1,73 @@ + + + +question_mark_error_result.call - Coverage Spans + + + +
fn call(return_error: bool) -> Result<(),()> { + if @0⦊return_error⦉@0 { + @1,3⦊Err(())⦉@1,3 + } else { + @2⦊Ok(())⦉@2 + } +}@4⦊⦉@4
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.question_mark_error_result/question_mark_error_result.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.question_mark_error_result/question_mark_error_result.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..2589b0454da2e --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.question_mark_error_result/question_mark_error_result.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,101 @@ + + + +question_mark_error_result.main - Coverage Spans + + + +
fn main() -> Result<(),()> { + let @0,1⦊mut + countdown = 10⦉@0,1 + ; + @2,3,4⦊for + _⦉@2,3,4 + in + @0,1⦊0..10⦉@0,1 + { + @6,8⦊countdown + -= 1⦉@6,8 + ; + if + @9⦊countdown < 5⦉@9 + { + @10,12,13,14⦊call(/*return_error=*/ true)?⦉@10,12,13,14; + } + else + { + @11,21,22⦊call(/*return_error=*/ false)?⦉@11,21,22; + } + } + @5⦊Ok(())⦉@5 +}@31⦊⦉@31
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.simple_loop/simple_loop.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.simple_loop/simple_loop.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..4b21d3fc263a3 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.simple_loop/simple_loop.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,127 @@ + + + +simple_loop.main - Coverage Spans + + + +
fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let @0,1,2,3⦊is_true = std::env::args().len() == 1; + + let mut countdown = 0⦉@0,1,2,3; + + if + @0,1,2,3⦊is_true⦉@0,1,2,3 + @4,6⦊{ + countdown + = + 10 + ; + }⦉@4,6 + + loop + { + if + @8,9⦊countdown + == + 0⦉@8,9 + { + @10,12⦊break⦉@10,12 + ; + } + @11⦊countdown + -= + 1⦉@11 + ; + } +}@10,12⦊⦉@10,12
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.simple_match/simple_match.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.simple_match/simple_match.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..5ba770ef6078e --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.simple_match/simple_match.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,190 @@ + + + +simple_match.main - Coverage Spans + + + +
fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let @0,1,2,3⦊is_true = std::env::args().len() == 1; + + let mut countdown = 1⦉@0,1,2,3; + if @0,1,2,3⦊is_true⦉@0,1,2,3 @4,6⦊{ + countdown = 0; + }⦉@4,6 + + @9,10,11⦊for + _⦉@9,10,11 + in + @7,8⦊0..2⦉@7,8 + { + let z + ; + match + @13,15,17⦊countdown + { + x + if + x + < + 1⦉@13,15,17 + => + @18⦊{ + z = countdown + ; + let y = countdown + ; + countdown = 10 + ; + }⦉@18 + _ + => + @16⦊{}⦉@16 + } + } +}@12⦊⦉@12
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.various_conditions/various_conditions.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.various_conditions/various_conditions.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..28f1d013c835f --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.various_conditions/various_conditions.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,228 @@ + + + +various_conditions.main - Coverage Spans + + + +
fn main() { + let @0⦊mut countdown = 0⦉@0; + if @0⦊true⦉@0 @1,3⦊{ + countdown = 10; + }⦉@1,3 + + const B: u32 = 100; + let @25⦊x⦉@25 = if @4⦊countdown > 7⦉@4 { + @5,7⦊countdown -= 4⦉@5,7; + @8⦊B⦉@8 + } else if @6⦊countdown > 2⦉@6 { + if @9,11⦊countdown < 1⦉@9,11 || @18⦊countdown > 5⦉@18 || @14⦊countdown != 9⦉@14 @20,22⦊{ + countdown = 0; + }⦉@20,22 + @23⦊countdown -= 5⦉@23; + @24⦊countdown⦉@24 + } else { + @10⦊return⦉@10; + }; + + let @25⦊mut countdown = 0⦉@25; + if @25⦊true⦉@25 @26,28⦊{ + countdown = 10; + }⦉@26,28 + + if @29⦊countdown > 7⦉@29 { + @30,32⦊countdown -= 4⦉@30,32; + } else if @31⦊countdown > 2⦉@31 { + if @34,36⦊countdown < 1⦉@34,36 || @43⦊countdown > 5⦉@43 || @39⦊countdown != 9⦉@39 @45,47⦊{ + countdown = 0; + }⦉@45,47 + @48⦊countdown -= 5⦉@48; + } else { + @35⦊return⦉@35; + } + + let @50⦊mut countdown = 0⦉@50; + if @50⦊true⦉@50 @51,53⦊{ + countdown = 1; + }⦉@51,53 + + let @77⦊z⦉@77 = if @54⦊countdown > 7⦉@54 { + @55,57⦊countdown -= 4⦉@55,57; + } else if @56⦊countdown > 2⦉@56 { + if @59,61⦊countdown < 1⦉@59,61 || @68⦊countdown > 5⦉@68 || @64⦊countdown != 9⦉@64 @70,72⦊{ + countdown = 0; + }⦉@70,72 + @73⦊countdown -= 5⦉@73; + } else { + let @60,75,76⦊should_be_reachable = countdown; + println!("reached"); + return⦉@60,75,76; + }; + + let @98⦊w⦉@98 = if @77⦊countdown > 7⦉@77 { + @78,80⦊countdown -= 4⦉@78,80; + } else if @79⦊countdown > 2⦉@79 { + if @82,84⦊countdown < 1⦉@82,84 || @91⦊countdown > 5⦉@91 || @87⦊countdown != 9⦉@87 @93,95⦊{ + countdown = 0; + }⦉@93,95 + @96⦊countdown -= 5⦉@96; + } else { + @83⦊return⦉@83; + }; +}@102⦊⦉@102
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.while_early_return/while_early_return.main.-------.InstrumentCoverage.0.html b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.while_early_return/while_early_return.main.-------.InstrumentCoverage.0.html new file mode 100644 index 0000000000000..b96789a92194d --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage-mir-cov-html-link-dead-code/expected_mir_dump.while_early_return/while_early_return.main.-------.InstrumentCoverage.0.html @@ -0,0 +1,119 @@ + + + +while_early_return.main - Coverage Spans + + + +
fn main() -> Result<(),u8> { + let @0⦊mut countdown = 10⦉@0; + @1,2⦊while + countdown + > + 0⦉@1,2 + { + if + @3,5⦊countdown + < + 5⦉@3,5 + { + return + if + @6,8⦊countdown + > + 8⦉@6,8 + { + @9,11⦊Ok(())⦉@9,11 + } + else + { + @10⦊Err(1)⦉@10 + } + ; + } + @7⦊countdown + -= + 1⦉@7 + ; + } + @4⦊Ok(())⦉@4 +}@14⦊⦉@14
+ + diff --git a/src/test/run-make-fulldeps/instrument-coverage/closure.rs b/src/test/run-make-fulldeps/instrument-coverage/closure.rs new file mode 100644 index 0000000000000..66bbbc55399fe --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage/closure.rs @@ -0,0 +1,93 @@ +#![allow(unused_assignments, unused_variables)] + +fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let is_true = std::env::args().len() == 1; + let is_false = ! is_true; + + let mut some_string = Some(String::from("the string content")); + println!( + "The string or alt: {}" + , + some_string + . + unwrap_or_else + ( + || + { + let mut countdown = 0; + if is_false { + countdown = 10; + } + "alt string 1".to_owned() + } + ) + ); + + some_string = Some(String::from("the string content")); + let + a + = + || + { + let mut countdown = 0; + if is_false { + countdown = 10; + } + "alt string 2".to_owned() + }; + println!( + "The string or alt: {}" + , + some_string + . + unwrap_or_else + ( + a + ) + ); + + some_string = None; + println!( + "The string or alt: {}" + , + some_string + . + unwrap_or_else + ( + || + { + let mut countdown = 0; + if is_false { + countdown = 10; + } + "alt string 3".to_owned() + } + ) + ); + + some_string = None; + let + a + = + || + { + let mut countdown = 0; + if is_false { + countdown = 10; + } + "alt string 4".to_owned() + }; + println!( + "The string or alt: {}" + , + some_string + . + unwrap_or_else + ( + a + ) + ); +} diff --git a/src/test/run-make-fulldeps/instrument-coverage/compiletest-ignore-dir b/src/test/run-make-fulldeps/instrument-coverage/compiletest-ignore-dir index d57f66a44898e..abf8df8fdc9e6 100644 --- a/src/test/run-make-fulldeps/instrument-coverage/compiletest-ignore-dir +++ b/src/test/run-make-fulldeps/instrument-coverage/compiletest-ignore-dir @@ -1,3 +1,3 @@ # Directory "instrument-coverage" supports the tests at prefix ../instrument-coverage-* -# Use ./x.py [options] test src/test/run-make-fulldeps/instrument-coverage to run all related tests. \ No newline at end of file +# Use ./x.py [options] test src/test/run-make-fulldeps/instrument-coverage to run all related tests. diff --git a/src/test/run-make-fulldeps/instrument-coverage/drop_trait.rs b/src/test/run-make-fulldeps/instrument-coverage/drop_trait.rs new file mode 100644 index 0000000000000..d15bfc0f87719 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage/drop_trait.rs @@ -0,0 +1,33 @@ +#![allow(unused_assignments)] +// expect-exit-status-1 + +struct Firework { + strength: i32, +} + +impl Drop for Firework { + fn drop(&mut self) { + println!("BOOM times {}!!!", self.strength); + } +} + +fn main() -> Result<(),u8> { + let _firecracker = Firework { strength: 1 }; + + let _tnt = Firework { strength: 100 }; + + if true { + println!("Exiting with error..."); + return Err(1); + } + + let _ = Firework { strength: 1000 }; + + Ok(()) +} + +// Expected program output: +// Exiting with error... +// BOOM times 100!!! +// BOOM times 1!!! +// Error: 1 diff --git a/src/test/run-make-fulldeps/instrument-coverage/generics.rs b/src/test/run-make-fulldeps/instrument-coverage/generics.rs new file mode 100644 index 0000000000000..f4e6402694480 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage/generics.rs @@ -0,0 +1,44 @@ +#![allow(unused_assignments)] +// expect-exit-status-1 + +struct Firework where T: Copy + std::fmt::Display { + strength: T, +} + +impl Firework where T: Copy + std::fmt::Display { + #[inline(always)] + fn set_strength(&mut self, new_strength: T) { + self.strength = new_strength; + } +} + +impl Drop for Firework where T: Copy + std::fmt::Display { + #[inline(always)] + fn drop(&mut self) { + println!("BOOM times {}!!!", self.strength); + } +} + +fn main() -> Result<(),u8> { + let mut firecracker = Firework { strength: 1 }; + firecracker.set_strength(2); + + let mut tnt = Firework { strength: 100.1 }; + tnt.set_strength(200.1); + tnt.set_strength(300.3); + + if true { + println!("Exiting with error..."); + return Err(1); + } + + let _ = Firework { strength: 1000 }; + + Ok(()) +} + +// Expected program output: +// Exiting with error... +// BOOM times 100!!! +// BOOM times 1!!! +// Error: 1 diff --git a/src/test/run-make-fulldeps/instrument-coverage/if.rs b/src/test/run-make-fulldeps/instrument-coverage/if.rs new file mode 100644 index 0000000000000..8ad5042ff7baf --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage/if.rs @@ -0,0 +1,28 @@ +#![allow(unused_assignments, unused_variables)] + +fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let + is_true + = + std::env::args().len() + == + 1 + ; + let + mut + countdown + = + 0 + ; + if + is_true + { + countdown + = + 10 + ; + } +} diff --git a/src/test/run-make-fulldeps/instrument-coverage/if_else.rs b/src/test/run-make-fulldeps/instrument-coverage/if_else.rs new file mode 100644 index 0000000000000..3ae4b7a7316bd --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage/if_else.rs @@ -0,0 +1,40 @@ +#![allow(unused_assignments)] + +fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let is_true = std::env::args().len() == 1; + + let mut countdown = 0; + if + is_true + { + countdown + = + 10 + ; + } + else // Note coverage region difference without semicolon + { + countdown + = + 100 + } + + if + is_true + { + countdown + = + 10 + ; + } + else + { + countdown + = + 100 + ; + } +} diff --git a/src/test/run-make-fulldeps/instrument-coverage/inner_items.rs b/src/test/run-make-fulldeps/instrument-coverage/inner_items.rs new file mode 100644 index 0000000000000..1e2fe5a9353a3 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage/inner_items.rs @@ -0,0 +1,57 @@ +#![allow(unused_assignments, unused_variables)] + +fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let is_true = std::env::args().len() == 1; + + let mut countdown = 0; + if is_true { + countdown = 10; + } + + mod inner_mod { + const INNER_MOD_CONST: u32 = 1000; + } + + fn inner_function(a: u32) { + let b = 1; + let c = a + b; + println!("c = {}", c) + } + + struct InnerStruct { + inner_struct_field: u32, + } + + const INNER_CONST: u32 = 1234; + + trait InnerTrait { + fn inner_trait_func(&mut self, incr: u32); + + fn default_trait_func(&mut self) { + inner_function(INNER_CONST); + self.inner_trait_func(INNER_CONST); + } + } + + impl InnerTrait for InnerStruct { + fn inner_trait_func(&mut self, incr: u32) { + self.inner_struct_field += incr; + inner_function(self.inner_struct_field); + } + } + + type InnerType = String; + + if is_true { + inner_function(countdown); + } + + let mut val = InnerStruct { + inner_struct_field: 101, + }; + + val.default_trait_func(); +} diff --git a/src/test/run-make-fulldeps/instrument-coverage/lazy_boolean.rs b/src/test/run-make-fulldeps/instrument-coverage/lazy_boolean.rs new file mode 100644 index 0000000000000..1d83eb30dea24 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage/lazy_boolean.rs @@ -0,0 +1,43 @@ +#![allow(unused_assignments, unused_variables)] + +fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let is_true = std::env::args().len() == 1; + + let (mut a, mut b, mut c) = (0, 0, 0); + if is_true { + a = 1; + b = 10; + c = 100; + } + let + somebool + = + a < b + || + b < c + ; + let + somebool + = + b < a + || + b < c + ; + let + somebool + = + a < b + && + b < c + ; + let + somebool + = + b < a + && + b < c + ; +} diff --git a/src/test/run-make-fulldeps/instrument-coverage/loop_break_value.rs b/src/test/run-make-fulldeps/instrument-coverage/loop_break_value.rs new file mode 100644 index 0000000000000..ba66d136de1e3 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage/loop_break_value.rs @@ -0,0 +1,13 @@ +#![allow(unused_assignments)] + +fn main() { + let result + = + loop + { + break + 10 + ; + } + ; +} diff --git a/src/test/run-make-fulldeps/instrument-coverage/question_mark_error_result.rs b/src/test/run-make-fulldeps/instrument-coverage/question_mark_error_result.rs new file mode 100644 index 0000000000000..3a8f30e048228 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage/question_mark_error_result.rs @@ -0,0 +1,35 @@ +#![allow(unused_assignments)] +// expect-exit-status-1 + +fn call(return_error: bool) -> Result<(),()> { + if return_error { + Err(()) + } else { + Ok(()) + } +} + +fn main() -> Result<(),()> { + let mut + countdown = 10 + ; + for + _ + in + 0..10 + { + countdown + -= 1 + ; + if + countdown < 5 + { + call(/*return_error=*/ true)?; + } + else + { + call(/*return_error=*/ false)?; + } + } + Ok(()) +} diff --git a/src/test/run-make-fulldeps/instrument-coverage/simple_loop.rs b/src/test/run-make-fulldeps/instrument-coverage/simple_loop.rs new file mode 100644 index 0000000000000..6f7f23475b826 --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage/simple_loop.rs @@ -0,0 +1,35 @@ +#![allow(unused_assignments)] + +fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let is_true = std::env::args().len() == 1; + + let mut countdown = 0; + + if + is_true + { + countdown + = + 10 + ; + } + + loop + { + if + countdown + == + 0 + { + break + ; + } + countdown + -= + 1 + ; + } +} diff --git a/src/test/run-make-fulldeps/instrument-coverage/simple_match.rs b/src/test/run-make-fulldeps/instrument-coverage/simple_match.rs new file mode 100644 index 0000000000000..c9a24f7a9d35d --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage/simple_match.rs @@ -0,0 +1,43 @@ +#![allow(unused_assignments)] + +fn main() { + // Initialize test constants in a way that cannot be determined at compile time, to ensure + // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from + // dependent conditions. + let is_true = std::env::args().len() == 1; + + let mut countdown = 1; + if is_true { + countdown = 0; + } + + for + _ + in + 0..2 + { + let z + ; + match + countdown + { + x + if + x + < + 1 + => + { + z = countdown + ; + let y = countdown + ; + countdown = 10 + ; + } + _ + => + {} + } + } +} diff --git a/src/test/run-make-fulldeps/instrument-coverage/coverage_of_if_else.rs b/src/test/run-make-fulldeps/instrument-coverage/various_conditions.rs similarity index 62% rename from src/test/run-make-fulldeps/instrument-coverage/coverage_of_if_else.rs rename to src/test/run-make-fulldeps/instrument-coverage/various_conditions.rs index 91741cf8f0dcc..da206e28f31da 100644 --- a/src/test/run-make-fulldeps/instrument-coverage/coverage_of_if_else.rs +++ b/src/test/run-make-fulldeps/instrument-coverage/various_conditions.rs @@ -1,4 +1,4 @@ -#![allow(unused_assignments)] +#![allow(unused_assignments, unused_variables)] fn main() { let mut countdown = 0; @@ -6,16 +6,19 @@ fn main() { countdown = 10; } - if countdown > 7 { + const B: u32 = 100; + let x = if countdown > 7 { countdown -= 4; + B } else if countdown > 2 { if countdown < 1 || countdown > 5 || countdown != 9 { countdown = 0; } countdown -= 5; + countdown } else { return; - } + }; let mut countdown = 0; if true { @@ -35,10 +38,10 @@ fn main() { let mut countdown = 0; if true { - countdown = 10; + countdown = 1; } - if countdown > 7 { + let z = if countdown > 7 { countdown -= 4; } else if countdown > 2 { if countdown < 1 || countdown > 5 || countdown != 9 { @@ -46,6 +49,19 @@ fn main() { } countdown -= 5; } else { + let should_be_reachable = countdown; + println!("reached"); return; - } + }; + + let w = if countdown > 7 { + countdown -= 4; + } else if countdown > 2 { + if countdown < 1 || countdown > 5 || countdown != 9 { + countdown = 0; + } + countdown -= 5; + } else { + return; + }; } diff --git a/src/test/run-make-fulldeps/instrument-coverage/while_early_return.rs b/src/test/run-make-fulldeps/instrument-coverage/while_early_return.rs new file mode 100644 index 0000000000000..14ba36238d62f --- /dev/null +++ b/src/test/run-make-fulldeps/instrument-coverage/while_early_return.rs @@ -0,0 +1,47 @@ +#![allow(unused_assignments)] +// expect-exit-status-1 + +fn main() -> Result<(),u8> { + let mut countdown = 10; + while + countdown + > + 0 + { + if + countdown + < + 5 + { + return + if + countdown + > + 8 + { + Ok(()) + } + else + { + Err(1) + } + ; + } + countdown + -= + 1 + ; + } + Ok(()) +} + +// ISSUE(77553): Originally, this test had `Err(1)` on line 22 (instead of `Ok(())`) and +// `std::process::exit(2)` on line 26 (instead of `Err(1)`); and this worked as expected on Linux +// and MacOS. But on Windows (MSVC, at least), the call to `std::process::exit()` exits the program +// without saving the InstrProf coverage counters. The use of `std::process:exit()` is not critical +// to the coverage test for early returns, but this is a limitation that should be fixed. +// +// FIXME(richkadel): Consider creating a new tests for coverage when calling `std::process::exit()`, +// move the `ISSUE` comment to that test, and implement a new test directive that supports skipping +// coverage tests when targeting specific platforms (at least skipping Windows, or MSVC if the +// problem exists on MSVC only). diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index acad316d807a3..d85558ea2f5f0 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -3199,8 +3199,18 @@ impl<'test> TestCx<'test> { from_file = format!("{}.{}.mir", test_name, first_pass); to_file = Some(second_file); } else { - expected_file = - format!("{}{}.mir", test_name.trim_end_matches(".mir"), bit_width); + let ext_re = Regex::new(r#"(\.(mir|dot|html))$"#).unwrap(); + let cap = ext_re + .captures_iter(test_name) + .next() + .expect("test_name has an invalid extension"); + let extension = cap.get(1).unwrap().as_str(); + expected_file = format!( + "{}{}{}", + test_name.trim_end_matches(extension), + bit_width, + extension, + ); from_file = test_name.to_string(); assert!( test_names.next().is_none(), diff --git a/src/tools/rust-demangler/Cargo.toml b/src/tools/rust-demangler/Cargo.toml index 0b8d974d2558a..c978bbe20e8c7 100644 --- a/src/tools/rust-demangler/Cargo.toml +++ b/src/tools/rust-demangler/Cargo.toml @@ -1,10 +1,11 @@ [package] authors = ["The Rust Project Developers"] name = "rust-demangler" -version = "0.0.0" +version = "0.0.1" edition = "2018" [dependencies] +regex = "1.0" rustc-demangle = "0.1" [[bin]] diff --git a/src/tools/rust-demangler/main.rs b/src/tools/rust-demangler/main.rs index a9f1011c450a9..e1e49230ad146 100644 --- a/src/tools/rust-demangler/main.rs +++ b/src/tools/rust-demangler/main.rs @@ -22,18 +22,51 @@ //! --instr-profile=main.profdata ./main --show-line-counts-or-regions //! ``` +use regex::Regex; use rustc_demangle::demangle; use std::io::{self, Read, Write}; +const REPLACE_COLONS: &str = "::"; + fn main() -> io::Result<()> { + let mut strip_crate_disambiguators = Some(Regex::new(r"\[[a-f0-9]{16}\]::").unwrap()); + + let mut args = std::env::args(); + let progname = args.next().unwrap(); + for arg in args { + if arg == "--disambiguators" || arg == "-d" { + strip_crate_disambiguators = None; + } else { + eprintln!(); + eprintln!("Usage: {} [-d|--disambiguators]", progname); + eprintln!(); + eprintln!( + "This tool converts a list of Rust mangled symbols (one per line) into a\n + corresponding list of demangled symbols." + ); + eprintln!(); + eprintln!( + "With -d (--disambiguators), Rust symbols mangled with the v0 symbol mangler may\n\ + include crate disambiguators (a 16 character hex value in square brackets).\n\ + Crate disambiguators are removed by default." + ); + eprintln!(); + std::process::exit(1) + } + } + let mut buffer = String::new(); io::stdin().read_to_string(&mut buffer)?; let lines = buffer.lines(); - let mut demangled = Vec::new(); + let mut demangled_lines = Vec::new(); for mangled in lines { - demangled.push(demangle(mangled).to_string()); + let mut demangled = demangle(mangled).to_string(); + if let Some(re) = &strip_crate_disambiguators { + demangled = re.replace_all(&demangled, REPLACE_COLONS).to_string(); + } + demangled_lines.push(demangled); } - demangled.push("".to_string()); - io::stdout().write_all(demangled.join("\n").as_bytes())?; + demangled_lines.push("".to_string()); + io::stdout().write_all(demangled_lines.join("\n").as_bytes())?; Ok(()) }