]> git.lizzy.rs Git - rust.git/blob - src/test/ui/async-await/async-block-control-flow-static-semantics.rs
Auto merge of #91030 - estebank:trait-bounds-are-tricky-2, r=oli-obk
[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 type mismatch
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 type mismatch resolving `<impl Future as Future>::Output == ()`
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> { Err(MyErr) }
46
47 fn rethrow_targets_async_block_not_fn() -> Result<u8, MyErr> {
48     //~^ ERROR mismatched types
49     let block = async {
50         err()?;
51         Ok(())
52     };
53     let _: &dyn Future<Output = Result<(), MyErr>> = &block;
54 }
55
56 fn rethrow_targets_async_block_not_async_fn() -> Result<u8, MyErr> {
57     //~^ ERROR mismatched types
58     let block = async {
59         err()?;
60         Ok(())
61     };
62     let _: &dyn Future<Output = Result<(), MyErr>> = &block;
63 }