]> git.lizzy.rs Git - rust.git/blob - tests/ui/async-await/async-fn-nonsend.rs
Rollup merge of #106854 - steffahn:drop_linear_arc_rebased, r=Mark-Simulacrum
[rust.git] / tests / ui / async-await / async-fn-nonsend.rs
1 // edition:2018
2 // compile-flags: --crate-type lib -Zdrop-tracking
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     // this is okay now because of the drop
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 fn get_formatter() -> std::fmt::Formatter<'static> {
39     panic!()
40 }
41
42 async fn non_sync_with_method_call() {
43     let f: &mut std::fmt::Formatter = &mut get_formatter();
44     // It would by nice for this to work.
45     if non_sync().fmt(f).unwrap() == () {
46         fut().await;
47     }
48 }
49
50 async fn non_sync_with_method_call_panic() {
51     let f: &mut std::fmt::Formatter = panic!();
52     if non_sync().fmt(f).unwrap() == () {
53         fut().await;
54     }
55 }
56
57 async fn non_sync_with_method_call_infinite_loop() {
58     let f: &mut std::fmt::Formatter = loop {};
59     if non_sync().fmt(f).unwrap() == () {
60         fut().await;
61     }
62 }
63
64 fn assert_send(_: impl Send) {}
65
66 pub fn pass_assert() {
67     assert_send(local_dropped_before_await());
68     assert_send(non_send_temporary_in_match());
69     //~^ ERROR future cannot be sent between threads safely
70     assert_send(non_sync_with_method_call());
71     //~^ ERROR future cannot be sent between threads safely
72     assert_send(non_sync_with_method_call_panic());
73     assert_send(non_sync_with_method_call_infinite_loop());
74 }