Files
rust/tests/coverage/attr/nested.coverage
Zalathar f521dd4ed9 Switch over to hybrid HIR-aware coverage spans
Instead of trying to heuristically recover source-code spans from MIR soup, we
can now take advantage of the `CoverageKind::Point` statements injected for
specific HIR expressions.

This is a transitional implementation that lets us remove the old MIR-soup
code, but still relies heavily on the existing span-refinement heuristics, and
does not take advantage of the full possibilities of HIR-aware coverage
instrumentation.

In order to avoid annoying never-executed spans in code that contains
assertions, a heuristic has been added that ignores any expression within the
arguments of a macro-expanded function call that returns `!`.

Some coverage tests now require `//@ min-llvm-version: 23`, due to changes in
how LLVM sorts the coverage mapping entries.
2026-09-06 10:24:48 +10:00

108 lines
3.6 KiB
Plaintext

LL| |#![feature(coverage_attribute, stmt_expr_attributes)]
LL| |//@ edition: 2021
LL| |//@ reference: attributes.coverage.nesting
LL| |
LL| |// Demonstrates the interaction between #[coverage(off)] and various kinds of
LL| |// nested function.
LL| |
LL| |#[coverage(off)]
LL| |fn do_stuff() {}
LL| |
LL| |#[coverage(off)]
LL| |fn outer_fn() {
LL| | fn middle_fn() {
LL| | fn inner_fn() {
LL| | do_stuff();
LL| | }
LL| | do_stuff();
LL| | }
LL| | do_stuff();
LL| |}
LL| |
LL| |struct MyOuter;
LL| |impl MyOuter {
LL| | #[coverage(off)]
LL| | fn outer_method(&self) {
LL| | struct MyMiddle;
LL| | impl MyMiddle {
LL| | fn middle_method(&self) {
LL| | struct MyInner;
LL| | impl MyInner {
LL| | fn inner_method(&self) {
LL| | do_stuff();
LL| | }
LL| | }
LL| | do_stuff();
LL| | }
LL| | }
LL| | do_stuff();
LL| | }
LL| |}
LL| |
LL| |trait MyTrait {
LL| | fn trait_method(&self);
LL| |}
LL| |impl MyTrait for MyOuter {
LL| | #[coverage(off)]
LL| | fn trait_method(&self) {
LL| | struct MyMiddle;
LL| | impl MyTrait for MyMiddle {
LL| | fn trait_method(&self) {
LL| | struct MyInner;
LL| | impl MyTrait for MyInner {
LL| | fn trait_method(&self) {
LL| | do_stuff();
LL| | }
LL| | }
LL| | do_stuff();
LL| | }
LL| | }
LL| | do_stuff();
LL| | }
LL| |}
LL| |
LL| 1|fn closure_expr() {
LL| | let _outer = #[coverage(off)]
LL| | || {
LL| | let _middle = || {
LL| | let _inner = || {
LL| | do_stuff();
LL| | };
LL| | do_stuff();
LL| | };
LL| | do_stuff();
LL| | };
LL| 1| do_stuff();
LL| 1|}
LL| |
LL| |// This syntax is allowed, even without #![feature(stmt_expr_attributes)].
LL| 1|fn closure_tail() {
LL| | let _outer = {
LL| | #[coverage(off)]
LL| | || {
LL| | let _middle = {
LL| | || {
LL| | let _inner = {
LL| | || {
LL| | do_stuff();
LL| | }
LL| | };
LL| | do_stuff();
LL| | }
LL| | };
LL| | do_stuff();
LL| | }
LL| | };
LL| 1| do_stuff();
LL| 1|}
LL| |
LL| |#[coverage(off)]
LL| |fn main() {
LL| | outer_fn();
LL| | MyOuter.outer_method();
LL| | MyOuter.trait_method();
LL| | closure_expr();
LL| | closure_tail();
LL| |}