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