rust/tests/ui/async-await/async-fn-nonsend.rs

81 lines
2.2 KiB
Rust
Raw Normal View History

// revisions: no_drop_tracking drop_tracking drop_tracking_mir
// [drop_tracking] compile-flags: -Zdrop-tracking
// [drop_tracking_mir] compile-flags: -Zdrop-tracking-mir
2019-06-24 19:48:21 -05:00
// edition:2018
// compile-flags: --crate-type lib
use std::{cell::RefCell, fmt::Debug, rc::Rc};
2019-06-24 19:48:21 -05:00
fn non_sync() -> impl Debug {
RefCell::new(())
}
2019-06-24 19:48:21 -05:00
fn non_send() -> impl Debug {
Rc::new(())
}
2019-06-24 19:48:21 -05:00
fn take_ref<T>(_: &T) {}
async fn fut() {}
async fn fut_arg<T>(_: T) {}
async fn local_dropped_before_await() {
// this is okay now because of the drop
2019-06-24 19:48:21 -05:00
let x = non_send();
drop(x);
fut().await;
}
async fn non_send_temporary_in_match() {
// We could theoretically make this work as well (produce a `Send` future)
// for scrutinees / temporaries that can or will
// be dropped prior to the match body
// (e.g. `Copy` types).
match Some(non_send()) {
Some(_) => fut().await,
None => {}
}
}
fn get_formatter() -> std::fmt::Formatter<'static> {
panic!()
}
2019-06-24 19:48:21 -05:00
async fn non_sync_with_method_call() {
let f: &mut std::fmt::Formatter = &mut get_formatter();
// It would by nice for this to work.
if non_sync().fmt(f).unwrap() == () {
fut().await;
}
}
async fn non_sync_with_method_call_panic() {
2019-06-24 19:48:21 -05:00
let f: &mut std::fmt::Formatter = panic!();
if non_sync().fmt(f).unwrap() == () {
fut().await;
}
}
async fn non_sync_with_method_call_infinite_loop() {
2021-12-16 15:34:39 -06:00
let f: &mut std::fmt::Formatter = loop {};
if non_sync().fmt(f).unwrap() == () {
fut().await;
}
}
2019-06-24 19:48:21 -05:00
fn assert_send(_: impl Send) {}
pub fn pass_assert() {
assert_send(local_dropped_before_await());
2023-01-25 21:51:26 -06:00
//[no_drop_tracking]~^ ERROR future cannot be sent between threads safely
2019-06-24 19:48:21 -05:00
assert_send(non_send_temporary_in_match());
//~^ ERROR future cannot be sent between threads safely
2019-06-24 19:48:21 -05:00
assert_send(non_sync_with_method_call());
//~^ ERROR future cannot be sent between threads safely
assert_send(non_sync_with_method_call_panic());
2023-01-25 21:51:26 -06:00
//[no_drop_tracking]~^ ERROR future cannot be sent between threads safely
assert_send(non_sync_with_method_call_infinite_loop());
2023-01-25 21:51:26 -06:00
//[no_drop_tracking]~^ ERROR future cannot be sent between threads safely
2019-06-24 19:48:21 -05:00
}