]> git.proxmox.com Git - rustc.git/blame - src/test/ui/async-await/async-fn-nonsend.rs
New upstream version 1.66.0+dfsg1
[rustc.git] / src / test / ui / async-await / async-fn-nonsend.rs
CommitLineData
dc9dc135 1// edition:2018
5e7ed085 2// compile-flags: --crate-type lib -Zdrop-tracking
5099ac24 3
dfeec247 4use std::{cell::RefCell, fmt::Debug, rc::Rc};
dc9dc135 5
dfeec247
XL
6fn non_sync() -> impl Debug {
7 RefCell::new(())
8}
dc9dc135 9
dfeec247
XL
10fn non_send() -> impl Debug {
11 Rc::new(())
12}
dc9dc135
XL
13
14fn take_ref<T>(_: &T) {}
15
16async fn fut() {}
17
18async fn fut_arg<T>(_: T) {}
19
20async fn local_dropped_before_await() {
5099ac24 21 // this is okay now because of the drop
dc9dc135
XL
22 let x = non_send();
23 drop(x);
24 fut().await;
25}
26
27async 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
5099ac24
FG
38fn get_formatter() -> std::fmt::Formatter<'static> {
39 panic!()
40}
41
dc9dc135 42async fn non_sync_with_method_call() {
5099ac24
FG
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
50async fn non_sync_with_method_call_panic() {
dc9dc135
XL
51 let f: &mut std::fmt::Formatter = panic!();
52 if non_sync().fmt(f).unwrap() == () {
53 fut().await;
54 }
55}
56
5099ac24
FG
57async 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
dc9dc135
XL
64fn assert_send(_: impl Send) {}
65
66pub fn pass_assert() {
67 assert_send(local_dropped_before_await());
dc9dc135 68 assert_send(non_send_temporary_in_match());
60c5eb7d 69 //~^ ERROR future cannot be sent between threads safely
dc9dc135 70 assert_send(non_sync_with_method_call());
60c5eb7d 71 //~^ ERROR future cannot be sent between threads safely
5099ac24
FG
72 assert_send(non_sync_with_method_call_panic());
73 assert_send(non_sync_with_method_call_infinite_loop());
dc9dc135 74}