]> git.lizzy.rs Git - rust.git/blob - src/test/ui/async-await/async-block-control-flow-static-semantics.rs
Adjust wording
[rust.git] / src / test / ui / async-await / async-block-control-flow-static-semantics.rs
1 // Test that `async { .. }` blocks:
2 // 1. do not allow `break` expressions.
3 // 2. get targeted by `return` and not the parent function.
4 // 3. get targeted by `?` and not the parent function.
5 //
6 // edition:2018
7
8 fn main() {}
9
10 use core::future::Future;
11
12 fn return_targets_async_block_not_fn() -> u8 {
13     //~^ ERROR mismatched types
14     let block = async {
15         return 0u8;
16     };
17     let _: &dyn Future<Output = ()> = &block;
18     //~^ ERROR expected `impl Future<Output = u8>` to be a future that resolves to `()`, but it resolves to `u8`
19 }
20
21 async fn return_targets_async_block_not_async_fn() -> u8 {
22     //~^ ERROR mismatched types [E0308]
23     let block = async {
24         return 0u8;
25     };
26     let _: &dyn Future<Output = ()> = &block;
27     //~^ ERROR expected `impl Future<Output = u8>` to be a future that resolves to `()`, but it resolves to `u8`
28 }
29
30 fn no_break_in_async_block() {
31     async {
32         break 0u8; //~ ERROR `break` inside of an `async` block
33     };
34 }
35
36 fn no_break_in_async_block_even_with_outer_loop() {
37     loop {
38         async {
39             break 0u8; //~ ERROR `break` inside of an `async` block
40         };
41     }
42 }
43
44 struct MyErr;
45 fn err() -> Result<u8, MyErr> {
46     Err(MyErr)
47 }
48
49 fn rethrow_targets_async_block_not_fn() -> Result<u8, MyErr> {
50     //~^ ERROR mismatched types
51     let block = async {
52         err()?;
53         Ok(())
54     };
55     let _: &dyn Future<Output = Result<(), MyErr>> = &block;
56 }
57
58 fn rethrow_targets_async_block_not_async_fn() -> Result<u8, MyErr> {
59     //~^ ERROR mismatched types
60     let block = async {
61         err()?;
62         Ok(())
63     };
64     let _: &dyn Future<Output = Result<(), MyErr>> = &block;
65 }