2021-11-03 23:28:07 +00:00
|
|
|
//@ build-pass
|
|
|
|
|
2023-10-19 21:46:28 +00:00
|
|
|
// A test to ensure coroutines capture values that were conditionally dropped,
|
2021-11-03 23:28:07 +00:00
|
|
|
// and also that values that are dropped along all paths to a yield do not get
|
2023-10-19 21:46:28 +00:00
|
|
|
// included in the coroutine type.
|
2021-11-03 23:28:07 +00:00
|
|
|
|
2024-04-11 13:15:34 +00:00
|
|
|
#![feature(coroutines, negative_impls, stmt_expr_attributes)]
|
2021-11-03 23:28:07 +00:00
|
|
|
#![allow(unused_assignments, dead_code)]
|
|
|
|
|
|
|
|
struct Ptr;
|
|
|
|
impl<'a> Drop for Ptr {
|
|
|
|
fn drop(&mut self) {}
|
|
|
|
}
|
|
|
|
|
2021-11-04 23:38:47 +00:00
|
|
|
struct NonSend;
|
2021-11-03 23:28:07 +00:00
|
|
|
impl !Send for NonSend {}
|
|
|
|
|
|
|
|
fn assert_send<T: Send>(_: T) {}
|
|
|
|
|
2023-01-05 08:45:44 +00:00
|
|
|
// This test case is reduced from tests/ui/drop/dynamic-drop-async.rs
|
2021-11-03 23:28:07 +00:00
|
|
|
fn one_armed_if(arg: bool) {
|
2024-04-11 13:15:34 +00:00
|
|
|
let _ = #[coroutine] || {
|
2021-11-03 23:28:07 +00:00
|
|
|
let arr = [Ptr];
|
|
|
|
if arg {
|
|
|
|
drop(arr);
|
|
|
|
}
|
|
|
|
yield;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn two_armed_if(arg: bool) {
|
2024-04-11 13:15:34 +00:00
|
|
|
assert_send(#[coroutine] || {
|
2021-11-03 23:28:07 +00:00
|
|
|
let arr = [Ptr];
|
|
|
|
if arg {
|
|
|
|
drop(arr);
|
|
|
|
} else {
|
|
|
|
drop(arr);
|
|
|
|
}
|
|
|
|
yield;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn if_let(arg: Option<i32>) {
|
2024-04-11 13:15:34 +00:00
|
|
|
let _ = #[coroutine] || {
|
2021-11-03 23:28:07 +00:00
|
|
|
let arr = [Ptr];
|
|
|
|
if let Some(_) = arg {
|
|
|
|
drop(arr);
|
|
|
|
}
|
|
|
|
yield;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-04 23:38:47 +00:00
|
|
|
fn init_in_if(arg: bool) {
|
2024-04-11 13:15:34 +00:00
|
|
|
assert_send(#[coroutine] || {
|
2021-11-04 23:38:47 +00:00
|
|
|
let mut x = NonSend;
|
|
|
|
drop(x);
|
|
|
|
if arg {
|
|
|
|
x = NonSend;
|
|
|
|
} else {
|
|
|
|
yield;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init_in_match_arm(arg: Option<i32>) {
|
2024-04-11 13:15:34 +00:00
|
|
|
assert_send(#[coroutine] || {
|
2021-11-04 23:38:47 +00:00
|
|
|
let mut x = NonSend;
|
|
|
|
drop(x);
|
|
|
|
match arg {
|
|
|
|
Some(_) => x = NonSend,
|
|
|
|
None => yield,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-03 23:28:07 +00:00
|
|
|
fn reinit() {
|
2024-04-11 13:15:34 +00:00
|
|
|
let _ = #[coroutine] || {
|
2021-11-03 23:28:07 +00:00
|
|
|
let mut arr = [Ptr];
|
|
|
|
drop(arr);
|
|
|
|
arr = [Ptr];
|
|
|
|
yield;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn loop_uninit() {
|
2024-04-11 13:15:34 +00:00
|
|
|
let _ = #[coroutine] || {
|
2021-11-03 23:28:07 +00:00
|
|
|
let mut arr = [Ptr];
|
|
|
|
let mut count = 0;
|
|
|
|
drop(arr);
|
|
|
|
while count < 3 {
|
|
|
|
yield;
|
|
|
|
arr = [Ptr];
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-04 23:38:47 +00:00
|
|
|
fn nested_loop() {
|
2024-04-11 13:15:34 +00:00
|
|
|
let _ = #[coroutine] || {
|
2021-11-04 23:38:47 +00:00
|
|
|
let mut arr = [Ptr];
|
|
|
|
let mut count = 0;
|
|
|
|
drop(arr);
|
|
|
|
while count < 3 {
|
|
|
|
for _ in 0..3 {
|
|
|
|
yield;
|
|
|
|
}
|
|
|
|
arr = [Ptr];
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-26 00:13:07 +00:00
|
|
|
fn loop_continue(b: bool) {
|
2024-04-11 13:15:34 +00:00
|
|
|
let _ = #[coroutine] || {
|
2022-01-26 00:13:07 +00:00
|
|
|
let mut arr = [Ptr];
|
|
|
|
let mut count = 0;
|
|
|
|
drop(arr);
|
|
|
|
while count < 3 {
|
|
|
|
count += 1;
|
|
|
|
yield;
|
|
|
|
if b {
|
|
|
|
arr = [Ptr];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-03 23:28:07 +00:00
|
|
|
fn main() {
|
|
|
|
one_armed_if(true);
|
|
|
|
if_let(Some(41));
|
2021-11-04 23:38:47 +00:00
|
|
|
init_in_if(true);
|
|
|
|
init_in_match_arm(Some(41));
|
2021-11-03 23:28:07 +00:00
|
|
|
reinit();
|
2021-11-04 17:56:07 +00:00
|
|
|
loop_uninit();
|
2021-11-04 23:38:47 +00:00
|
|
|
nested_loop();
|
2022-01-26 00:13:07 +00:00
|
|
|
loop_continue(true);
|
2021-11-03 23:28:07 +00:00
|
|
|
}
|