From f8dc13db43d61bba73f68cfcccfe4771864f6d78 Mon Sep 17 00:00:00 2001 From: Daniel Henry-Mantilla Date: Thu, 9 Dec 2021 22:19:57 +0100 Subject: [PATCH] Add tests asserting the function-like semantics of `join!()` --- library/core/tests/future.rs | 34 ++++++++++++++++++++++++++++++++++ library/core/tests/lib.rs | 1 + 2 files changed, 35 insertions(+) diff --git a/library/core/tests/future.rs b/library/core/tests/future.rs index 73249b1b8a435..8f4a991ed2d23 100644 --- a/library/core/tests/future.rs +++ b/library/core/tests/future.rs @@ -64,6 +64,40 @@ fn test_join() { }); } +/// Tests that `join!(…)` behaves "like a function": evaluating its arguments +/// before applying any of its own logic. +/// +/// _e.g._, `join!(async_fn(&borrowed), …)` does not consume `borrowed`; +/// and `join!(opt_fut?, …)` does let that `?` refer to the callsite scope. +mod test_join_function_like_value_arg_semantics { + use super::*; + + async fn async_fn(_: impl Sized) {} + + // no need to _run_ this test, just to compile it. + fn _join_does_not_unnecessarily_move_mentioned_bindings() { + let not_copy = vec![()]; + let _ = join!(async_fn(¬_copy)); // should not move `not_copy` + let _ = not_copy; // OK + } + + #[test] + fn join_lets_control_flow_effects_such_as_try_flow_through() { + let maybe_fut = None; + if false { + *&mut { maybe_fut } = Some(async {}); + loop {} + } + assert!(Option::is_none(&try { join!(maybe_fut?, async { unreachable!() }) })); + } + + #[test] + fn join_is_able_to_handle_temporaries() { + let _ = join!(async_fn(&String::from("temporary"))); + let () = block_on(join!(async_fn(&String::from("temporary")))); + } +} + fn block_on(fut: impl Future) { struct Waker; impl Wake for Waker { diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 73a3a1fc3e0af..012e6e5b57ad0 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -49,6 +49,7 @@ #![feature(str_internals)] #![feature(test)] #![feature(trusted_len)] +#![feature(try_blocks)] #![feature(try_trait_v2)] #![feature(slice_internals)] #![feature(slice_partition_dedup)]