]> git.lizzy.rs Git - rust.git/blob - src/test/ui/async-await/async-fn-nonsend.rs
Rollup merge of #66648 - crgl:btree-clone-from, r=Amanieu
[rust.git] / src / test / ui / async-await / async-fn-nonsend.rs
1 // compile-fail
2 // edition:2018
3 // compile-flags: --crate-type lib
4
5 use std::{cell::RefCell, fmt::Debug, rc::Rc};
6
7 fn non_sync() -> impl Debug {
8     RefCell::new(())
9 }
10
11 fn non_send() -> impl Debug {
12     Rc::new(())
13 }
14
15 fn take_ref<T>(_: &T) {}
16
17 async fn fut() {}
18
19 async fn fut_arg<T>(_: T) {}
20
21 async fn local_dropped_before_await() {
22     // FIXME: it'd be nice for this to be allowed in a `Send` `async fn`
23     let x = non_send();
24     drop(x);
25     fut().await;
26 }
27
28 async fn non_send_temporary_in_match() {
29     // We could theoretically make this work as well (produce a `Send` future)
30     // for scrutinees / temporaries that can or will
31     // be dropped prior to the match body
32     // (e.g. `Copy` types).
33     match Some(non_send()) {
34         Some(_) => fut().await,
35         None => {}
36     }
37 }
38
39 async fn non_sync_with_method_call() {
40     // FIXME: it'd be nice for this to work.
41     let f: &mut std::fmt::Formatter = panic!();
42     if non_sync().fmt(f).unwrap() == () {
43         fut().await;
44     }
45 }
46
47 fn assert_send(_: impl Send) {}
48
49 pub fn pass_assert() {
50     assert_send(local_dropped_before_await());
51     //~^ ERROR future cannot be sent between threads safely
52     assert_send(non_send_temporary_in_match());
53     //~^ ERROR future cannot be sent between threads safely
54     assert_send(non_sync_with_method_call());
55     //~^ ERROR future cannot be sent between threads safely
56 }