]> git.lizzy.rs Git - rust.git/blob - src/test/codegen/coverage-experiments/src/drop_trait_with_comments_prints.rs
explained lang_item function body (count_code_region)
[rust.git] / src / test / codegen / coverage-experiments / src / drop_trait_with_comments_prints.rs
1 //
2 //
3 //
4 // It's interesting to speculate if there is a way to leverage the Drop trait functionality
5 // to increment counters when a scope is closed, but I don't think it would help "out of the box".
6 //
7 // A `return` or `break` with expression might not need a temp value expression wrapper
8 // such as `return { let _t = result_expression; __incr_counter(...); _t };`
9 //
10 //    ... **if** the __incr_counter() was somehow called from a "drop()" trait function.
11 //
12 // The problem is, since the drop call is automatic, there is no way to have argument variants
13 // depending on where the drop() occurs (e.g., from a `return` statement vs. from the end of
14 // the function). We need 2 different code regions though.
15 //
16 //
17 //
18 //
19
20 #[inline(always)]
21 pub fn __incr_cov<T>(_region_loc: &str, /*index: u32,*/ result: T) -> T {
22     // println!("from: {}", _region_loc);
23     result
24 }
25
26 struct Firework {
27     strength: i32,
28 }
29
30 impl Drop for Firework {
31     fn drop(&mut self) {
32         println!("BOOM times {}!!!", self.strength);
33         __incr_cov("start of drop()", ());
34     }
35 }
36
37 fn main() -> Result<(),u8> {
38     let _firecracker = Firework { strength: 1 };
39
40     if __incr_cov("start of main()", true) {
41         return __incr_cov("if true", { let _t = Err(1); println!("computing return value"); _t });
42     }
43
44     let _tnt = Firework { strength: 100 };
45     // __incr_cov("after if block", Ok(())) // CAN USE COUNTER EXPRESSION: "start of drop()" - "if true"
46     Ok(())
47 }
48
49 // OUTPUT WHEN RUNNING THIS PROGRAM IS AS EXPECTED:
50
51 // computing return value
52 // BOOM times 1!!!
53 // Error: 1