fork/rust

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

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

master
3.9 KiB64 linesraw
1   LL|       |#![allow(unused_assignments)]
2   LL|       |// failure-status: 101
3   LL|       |
4   LL|      4|fn might_overflow(to_add: u32) -> u32 {
5   LL|      4|    if to_add > 5 {
6   LL|      1|        println!("this will probably overflow");
7   LL|      3|    }
8   LL|      4|    let add_to = u32::MAX - 5;
9   LL|      4|    println!("does {} + {} overflow?", add_to, to_add);
10   LL|      4|    let result = to_add + add_to;
11   LL|      4|    println!("continuing after overflow check");
12   LL|      4|    result
13   LL|      4|}
14   LL|       |
15   LL|      1|fn main() -> Result<(), u8> {
16   LL|      1|    let mut countdown = 10;
17   LL|     11|    while countdown > 0 {
18   LL|     11|        if countdown == 1 {
19   LL|      1|            let result = might_overflow(10);
20   LL|      1|            println!("Result: {}", result);
21   LL|     10|        } else if countdown < 5 {
22   LL|      3|            let result = might_overflow(1);
23   LL|      3|            println!("Result: {}", result);
24   LL|      6|        }
25   LL|     10|        countdown -= 1;
26   LL|       |    }
27   LL|      0|    Ok(())
28   LL|      0|}
29   LL|       |
30   LL|       |// Notes:
31   LL|       |//   1. Compare this program and its coverage results to those of the very similar test `assert.rs`,
32   LL|       |//      and similar tests `panic_unwind.rs`, abort.rs` and `try_error_result.rs`.
33   LL|       |//   2. This test confirms the coverage generated when a program passes or fails a
34   LL|       |//      compiler-generated `TerminatorKind::Assert` (based on an overflow check, in this case).
35   LL|       |//   3. Similar to how the coverage instrumentation handles `TerminatorKind::Call`,
36   LL|       |//      compiler-generated assertion failures are assumed to be a symptom of a program bug, not
37   LL|       |//      expected behavior. To simplify the coverage graphs and keep instrumented programs as
38   LL|       |//      small and fast as possible, `Assert` terminators are assumed to always succeed, and
39   LL|       |//      therefore are considered "non-branching" terminators. So, an `Assert` terminator does not
40   LL|       |//      get its own coverage counter.
41   LL|       |//   4. After an unhandled panic or failed Assert, coverage results may not always be intuitive.
42   LL|       |//      In this test, the final count for the statements after the `if` block in `might_overflow()`
43   LL|       |//      is 4, even though the lines after `to_add + add_to` were executed only 3 times. Depending
44   LL|       |//      on the MIR graph and the structure of the code, this count could have been 3 (which might
45   LL|       |//      have been valid for the overflowed add `+`, but should have been 4 for the lines before
46   LL|       |//      the overflow. The reason for this potential uncertainty is, a `CounterKind` is incremented
47   LL|       |//      via StatementKind::Counter at the end of the block, but (as in the case in this test),
48   LL|       |//      a CounterKind::Expression is always evaluated. In this case, the expression was based on
49   LL|       |//      a `Counter` incremented as part of the evaluation of the `if` expression, which was
50   LL|       |//      executed, and counted, 4 times, before reaching the overflow add.
51   LL|       |
52   LL|       |// If the program did not overflow, the coverage for `might_overflow()` would look like this:
53   LL|       |//
54   LL|       |//     4|       |fn might_overflow(to_add: u32) -> u32 {
55   LL|       |//     5|      4|    if to_add > 5 {
56   LL|       |//     6|      0|        println!("this will probably overflow");
57   LL|       |//     7|      4|    }
58   LL|       |//     8|      4|    let add_to = u32::MAX - 5;
59   LL|       |//     9|      4|    println!("does {} + {} overflow?", add_to, to_add);
60   LL|       |//    10|      4|    let result = to_add + add_to;
61   LL|       |//    11|      4|    println!("continuing after overflow check");
62   LL|       |//    12|      4|    result
63   LL|       |//    13|      4|}
64