about summary refs log tree commit diff
path: root/tests/coverage/attr/nested.rs
diff options
context:
space:
mode:
authorZalathar <Zalathar@users.noreply.github.com>2024-06-18 20:02:21 +1000
committerZalathar <Zalathar@users.noreply.github.com>2024-06-18 21:27:34 +1000
commit50936586323eefb6504945f444fe9e4928f2bb86 (patch)
tree34b8dfa7acec64719901dfa26765776a6d95c3ab /tests/coverage/attr/nested.rs
parent9a084e6310671aa0e3e35efb73953eeef8092d7d (diff)
downloadrust-50936586323eefb6504945f444fe9e4928f2bb86.tar.gz
rust-50936586323eefb6504945f444fe9e4928f2bb86.zip
Add more thorough coverage tests for `#[coverage(..)]` in nested functions
These tests reflect the current implementation behaviour, which is not
necessarily the desired behaviour.
Diffstat (limited to 'tests/coverage/attr/nested.rs')
-rw-r--r--tests/coverage/attr/nested.rs110
1 files changed, 110 insertions, 0 deletions
diff --git a/tests/coverage/attr/nested.rs b/tests/coverage/attr/nested.rs
new file mode 100644
index 00000000000..c7ff835f44f
--- /dev/null
+++ b/tests/coverage/attr/nested.rs
@@ -0,0 +1,110 @@
+#![feature(coverage_attribute, stmt_expr_attributes)]
+//@ edition: 2021
+
+// Demonstrates the interaction between #[coverage(off)] and various kinds of
+// nested function.
+
+// FIXME(#126625): Coverage attributes should apply recursively to nested functions.
+// FIXME(#126626): When an inner (non-closure) function has `#[coverage(off)]`,
+// its lines can still be marked with misleading execution counts from its enclosing
+// function.
+
+#[coverage(off)]
+fn do_stuff() {}
+
+#[coverage(off)]
+fn outer_fn() {
+    fn middle_fn() {
+        fn inner_fn() {
+            do_stuff();
+        }
+        do_stuff();
+    }
+    do_stuff();
+}
+
+struct MyOuter;
+impl MyOuter {
+    #[coverage(off)]
+    fn outer_method(&self) {
+        struct MyMiddle;
+        impl MyMiddle {
+            fn middle_method(&self) {
+                struct MyInner;
+                impl MyInner {
+                    fn inner_method(&self) {
+                        do_stuff();
+                    }
+                }
+                do_stuff();
+            }
+        }
+        do_stuff();
+    }
+}
+
+trait MyTrait {
+    fn trait_method(&self);
+}
+impl MyTrait for MyOuter {
+    #[coverage(off)]
+    fn trait_method(&self) {
+        struct MyMiddle;
+        impl MyTrait for MyMiddle {
+            fn trait_method(&self) {
+                struct MyInner;
+                impl MyTrait for MyInner {
+                    fn trait_method(&self) {
+                        do_stuff();
+                    }
+                }
+                do_stuff();
+            }
+        }
+        do_stuff();
+    }
+}
+
+fn closure_expr() {
+    let _outer = #[coverage(off)]
+    || {
+        let _middle = || {
+            let _inner = || {
+                do_stuff();
+            };
+            do_stuff();
+        };
+        do_stuff();
+    };
+    do_stuff();
+}
+
+// This syntax is allowed, even without #![feature(stmt_expr_attributes)].
+fn closure_tail() {
+    let _outer = {
+        #[coverage(off)]
+        || {
+            let _middle = {
+                || {
+                    let _inner = {
+                        || {
+                            do_stuff();
+                        }
+                    };
+                    do_stuff();
+                }
+            };
+            do_stuff();
+        }
+    };
+    do_stuff();
+}
+
+#[coverage(off)]
+fn main() {
+    outer_fn();
+    MyOuter.outer_method();
+    MyOuter.trait_method();
+    closure_expr();
+    closure_tail();
+}