]> git.lizzy.rs Git - rust.git/blob - src/test/ui/async-await/async-fn-nonsend.rs
Rollup merge of #94006 - pierwill:upvar-field, r=nikomatsakis
[rust.git] / src / test / ui / async-await / async-fn-nonsend.rs
1 // edition:2018
2 // compile-flags: --crate-type lib
3
4 // FIXME(eholk): temporarily disabled while drop range tracking is disabled
5 // (see generator_interior.rs:27)
6 // ignore-test
7
8 use std::{cell::RefCell, fmt::Debug, rc::Rc};
9
10 fn non_sync() -> impl Debug {
11     RefCell::new(())
12 }
13
14 fn non_send() -> impl Debug {
15     Rc::new(())
16 }
17
18 fn take_ref<T>(_: &T) {}
19
20 async fn fut() {}
21
22 async fn fut_arg<T>(_: T) {}
23
24 async fn local_dropped_before_await() {
25     // this is okay now because of the drop
26     let x = non_send();
27     drop(x);
28     fut().await;
29 }
30
31 async fn non_send_temporary_in_match() {
32     // We could theoretically make this work as well (produce a `Send` future)
33     // for scrutinees / temporaries that can or will
34     // be dropped prior to the match body
35     // (e.g. `Copy` types).
36     match Some(non_send()) {
37         Some(_) => fut().await,
38         None => {}
39     }
40 }
41
42 fn get_formatter() -> std::fmt::Formatter<'static> {
43     panic!()
44 }
45
46 async fn non_sync_with_method_call() {
47     let f: &mut std::fmt::Formatter = &mut get_formatter();
48     // It would by nice for this to work.
49     if non_sync().fmt(f).unwrap() == () {
50         fut().await;
51     }
52 }
53
54 async fn non_sync_with_method_call_panic() {
55     let f: &mut std::fmt::Formatter = panic!();
56     if non_sync().fmt(f).unwrap() == () {
57         fut().await;
58     }
59 }
60
61 async fn non_sync_with_method_call_infinite_loop() {
62     let f: &mut std::fmt::Formatter = loop {};
63     if non_sync().fmt(f).unwrap() == () {
64         fut().await;
65     }
66 }
67
68 fn assert_send(_: impl Send) {}
69
70 pub fn pass_assert() {
71     assert_send(local_dropped_before_await());
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     assert_send(non_sync_with_method_call_infinite_loop());
78 }