]> git.lizzy.rs Git - rust.git/blob - tests/ui/async-await/async-fn-nonsend.rs
Rollup merge of #107100 - compiler-errors:issue-107087, r=lcnr
[rust.git] / tests / ui / async-await / async-fn-nonsend.rs
1 // revisions: no_drop_tracking drop_tracking drop_tracking_mir
2 // [drop_tracking] compile-flags: -Zdrop-tracking
3 // [drop_tracking_mir] compile-flags: -Zdrop-tracking-mir
4 // edition:2018
5 // compile-flags: --crate-type lib
6
7 use std::{cell::RefCell, fmt::Debug, rc::Rc};
8
9 fn non_sync() -> impl Debug {
10     RefCell::new(())
11 }
12
13 fn non_send() -> impl Debug {
14     Rc::new(())
15 }
16
17 fn take_ref<T>(_: &T) {}
18
19 async fn fut() {}
20
21 async fn fut_arg<T>(_: T) {}
22
23 async fn local_dropped_before_await() {
24     // this is okay now because of the drop
25     let x = non_send();
26     drop(x);
27     fut().await;
28 }
29
30 async fn non_send_temporary_in_match() {
31     // We could theoretically make this work as well (produce a `Send` future)
32     // for scrutinees / temporaries that can or will
33     // be dropped prior to the match body
34     // (e.g. `Copy` types).
35     match Some(non_send()) {
36         Some(_) => fut().await,
37         None => {}
38     }
39 }
40
41 fn get_formatter() -> std::fmt::Formatter<'static> {
42     panic!()
43 }
44
45 async fn non_sync_with_method_call() {
46     let f: &mut std::fmt::Formatter = &mut get_formatter();
47     // It would by nice for this to work.
48     if non_sync().fmt(f).unwrap() == () {
49         fut().await;
50     }
51 }
52
53 async fn non_sync_with_method_call_panic() {
54     let f: &mut std::fmt::Formatter = panic!();
55     if non_sync().fmt(f).unwrap() == () {
56         fut().await;
57     }
58 }
59
60 async fn non_sync_with_method_call_infinite_loop() {
61     let f: &mut std::fmt::Formatter = loop {};
62     if non_sync().fmt(f).unwrap() == () {
63         fut().await;
64     }
65 }
66
67 fn assert_send(_: impl Send) {}
68
69 pub fn pass_assert() {
70     assert_send(local_dropped_before_await());
71     //[no_drop_tracking]~^ ERROR future cannot be sent between threads safely
72     assert_send(non_send_temporary_in_match());
73     //~^ ERROR future cannot be sent between threads safely
74     assert_send(non_sync_with_method_call());
75     //~^ ERROR future cannot be sent between threads safely
76     assert_send(non_sync_with_method_call_panic());
77     //[no_drop_tracking]~^ ERROR future cannot be sent between threads safely
78     assert_send(non_sync_with_method_call_infinite_loop());
79     //[no_drop_tracking]~^ ERROR future cannot be sent between threads safely
80 }