fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
3.6 KiB69 linesraw
1   LL|       |#![feature(c_unwind)]
2   LL|       |#![allow(unused_assignments)]
3   LL|       |
4   LL|     12|extern "C" fn might_abort(should_abort: bool) {
5   LL|     12|    if should_abort {
6   LL|      0|        println!("aborting...");
7   LL|      0|        panic!("panics and aborts");
8   LL|     12|    } else {
9   LL|     12|        println!("Don't Panic");
10   LL|     12|    }
11   LL|     12|}
12   LL|       |
13   LL|      1|fn main() -> Result<(), u8> {
14   LL|      1|    let mut countdown = 10;
15   LL|     11|    while countdown > 0 {
16   LL|     10|        if countdown < 5 {
17   LL|      4|            might_abort(false);
18   LL|      6|        }
19   LL|       |        // See discussion (below the `Notes` section) on coverage results for the closing brace.
20   LL|     10|        if countdown < 5 { might_abort(false); } // Counts for different regions on one line.
21                                       ^4                     ^6
22   LL|       |        // For the following example, the closing brace is the last character on the line.
23   LL|       |        // This shows the character after the closing brace is highlighted, even if that next
24   LL|       |        // character is a newline.
25   LL|     10|        if countdown < 5 { might_abort(false); }
26                                       ^4                     ^6
27   LL|     10|        countdown -= 1;
28   LL|       |    }
29   LL|      1|    Ok(())
30   LL|      1|}
31   LL|       |
32   LL|       |// Notes:
33   LL|       |//   1. Compare this program and its coverage results to those of the similar tests
34   LL|       |//      `panic_unwind.rs` and `try_error_result.rs`.
35   LL|       |//   2. This test confirms the coverage generated when a program includes `UnwindAction::Terminate`.
36   LL|       |//   3. The test does not invoke the abort. By executing to a successful completion, the coverage
37   LL|       |//      results show where the program did and did not execute.
38   LL|       |//   4. If the program actually aborted, the coverage counters would not be saved (which "works as
39   LL|       |//      intended"). Coverage results would show no executed coverage regions.
40   LL|       |//   6. If `should_abort` is `true` and the program aborts, the program exits with a `132` status
41   LL|       |//      (on Linux at least).
42   LL|       |
43   LL|       |/*
44   LL|       |
45   LL|       |Expect the following coverage results:
46   LL|       |
47   LL|       |```text
48   LL|       |    16|     11|    while countdown > 0 {
49   LL|       |    17|     10|        if countdown < 5 {
50   LL|       |    18|      4|            might_abort(false);
51   LL|       |    19|      6|        }
52   LL|       |```
53   LL|       |
54   LL|       |This is actually correct.
55   LL|       |
56   LL|       |The condition `countdown < 5` executed 10 times (10 loop iterations).
57   LL|       |
58   LL|       |It evaluated to `true` 4 times, and executed the `might_abort()` call.
59   LL|       |
60   LL|       |It skipped the body of the `might_abort()` call 6 times. If an `if` does not include an explicit
61   LL|       |`else`, the coverage implementation injects a counter, at the character immediately after the `if`s
62   LL|       |closing brace, to count the "implicit" `else`. This is the only way to capture the coverage of the
63   LL|       |non-true condition.
64   LL|       |
65   LL|       |As another example of why this is important, say the condition was `countdown < 50`, which is always
66   LL|       |`true`. In that case, we wouldn't have a test for what happens if `might_abort()` is not called.
67   LL|       |The closing brace would have a count of `0`, highlighting the missed coverage.
68   LL|       |*/
69