fork/rust

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

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

master
5.9 KiB139 linesraw
1   LL|       |#![allow(unused_assignments, dead_code)]
2   LL|       |
3   LL|       |// compile-flags: --edition=2018 -C opt-level=1
4   LL|       |
5   LL|      1|async fn c(x: u8) -> u8 {
6   LL|      1|    if x == 8 {
7   LL|      1|        1
8   LL|       |    } else {
9   LL|      0|        0
10   LL|       |    }
11   LL|      1|}
12   LL|       |
13   LL|      0|async fn d() -> u8 { 1 }
14   LL|       |
15   LL|      0|async fn e() -> u8 { 1 } // unused function; executor does not block on `g()`
16   LL|       |
17   LL|      1|async fn f() -> u8 { 1 }
18   LL|       |
19   LL|      0|async fn foo() -> [bool; 10] { [false; 10] } // unused function; executor does not block on `h()`
20   LL|       |
21   LL|      1|pub async fn g(x: u8) {
22   LL|      0|    match x {
23   LL|      0|        y if e().await == y => (),
24   LL|      0|        y if f().await == y => (),
25   LL|      0|        _ => (),
26   LL|       |    }
27   LL|      0|}
28   LL|       |
29   LL|      1|async fn h(x: usize) { // The function signature is counted when called, but the body is not
30   LL|      0|                       // executed (not awaited) so the open brace has a `0` count (at least when
31   LL|      0|                       // displayed with `llvm-cov show` in color-mode).
32   LL|      0|    match x {
33   LL|      0|        y if foo().await[y] => (),
34   LL|      0|        _ => (),
35   LL|       |    }
36   LL|      0|}
37   LL|       |
38   LL|      1|async fn i(x: u8) { // line coverage is 1, but there are 2 regions:
39   LL|      1|                    // (a) the function signature, counted when the function is called; and
40   LL|      1|                    // (b) the open brace for the function body, counted once when the body is
41   LL|      1|                    // executed asynchronously.
42   LL|      1|    match x {
43   LL|      1|        y if c(x).await == y + 1 => { d().await; }
44                      ^0        ^0                  ^0  ^0
45   LL|      1|        y if f().await == y + 1 => (),
46                      ^0       ^0                ^0
47   LL|      1|        _ => (),
48   LL|       |    }
49   LL|      1|}
50   LL|       |
51   LL|      1|fn j(x: u8) {
52   LL|      1|    // non-async versions of `c()`, `d()`, and `f()` to make it similar to async `i()`.
53   LL|      1|    fn c(x: u8) -> u8 {
54   LL|      1|        if x == 8 {
55   LL|      1|            1 // This line appears covered, but the 1-character expression span covering the `1`
56                          ^0
57   LL|      1|              // is not executed. (`llvm-cov show` displays a `^0` below the `1` ). This is because
58   LL|      1|              // `fn j()` executes the open brace for the function body, followed by the function's
59   LL|      1|              // first executable statement, `match x`. Inner function declarations are not
60   LL|      1|              // "visible" to the MIR for `j()`, so the code region counts all lines between the
61   LL|      1|              // open brace and the first statement as executed, which is, in a sense, true.
62   LL|      1|              // `llvm-cov show` overcomes this kind of situation by showing the actual counts
63   LL|      1|              // of the enclosed coverages, (that is, the `1` expression was not executed, and
64   LL|      1|              // accurately displays a `0`).
65   LL|      1|        } else {
66   LL|      1|            0
67   LL|      1|        }
68   LL|      1|    }
69   LL|      1|    fn d() -> u8 { 1 } // inner function is defined in-line, but the function is not executed
70                  ^0
71   LL|      1|    fn f() -> u8 { 1 }
72   LL|      1|    match x {
73   LL|      1|        y if c(x) == y + 1 => { d(); }
74                      ^0                    ^0
75   LL|      1|        y if f() == y + 1 => (),
76                      ^0                   ^0
77   LL|      1|        _ => (),
78   LL|       |    }
79   LL|      1|}
80   LL|       |
81   LL|      0|fn k(x: u8) { // unused function
82   LL|      0|    match x {
83   LL|      0|        1 => (),
84   LL|      0|        2 => (),
85   LL|      0|        _ => (),
86   LL|       |    }
87   LL|      0|}
88   LL|       |
89   LL|      1|fn l(x: u8) {
90   LL|      1|    match x {
91   LL|      0|        1 => (),
92   LL|      0|        2 => (),
93   LL|      1|        _ => (),
94   LL|       |    }
95   LL|      1|}
96   LL|       |
97   LL|      1|async fn m(x: u8) -> u8 { x - 1 }
98                                      ^0
99   LL|       |
100   LL|      1|fn main() {
101   LL|      1|    let _ = g(10);
102   LL|      1|    let _ = h(9);
103   LL|      1|    let mut future = Box::pin(i(8));
104   LL|      1|    j(7);
105   LL|      1|    l(6);
106   LL|      1|    let _ = m(5);
107   LL|      1|    executor::block_on(future.as_mut());
108   LL|      1|}
109   LL|       |
110   LL|       |mod executor {
111   LL|       |    use core::{
112   LL|       |        future::Future,
113   LL|       |        pin::Pin,
114   LL|       |        task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
115   LL|       |    };
116   LL|       |
117   LL|      1|    pub fn block_on<F: Future>(mut future: F) -> F::Output {
118   LL|      1|        let mut future = unsafe { Pin::new_unchecked(&mut future) };
119   LL|      1|        use std::hint::unreachable_unchecked;
120   LL|      1|        static VTABLE: RawWakerVTable = RawWakerVTable::new(
121   LL|      1|            |_| unsafe { unreachable_unchecked() }, // clone
122                              ^0
123   LL|      1|            |_| unsafe { unreachable_unchecked() }, // wake
124                              ^0
125   LL|      1|            |_| unsafe { unreachable_unchecked() }, // wake_by_ref
126                              ^0
127   LL|      1|            |_| (),
128   LL|      1|        );
129   LL|      1|        let waker = unsafe { Waker::from_raw(RawWaker::new(core::ptr::null(), &VTABLE)) };
130   LL|      1|        let mut context = Context::from_waker(&waker);
131   LL|       |
132   LL|       |        loop {
133   LL|      1|            if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
134   LL|      1|                break val;
135   LL|      0|            }
136   LL|       |        }
137   LL|      1|    }
138   LL|       |}
139