Skip to content

Commit

Permalink
Add tests asserting the function-like semantics of join!()
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhenrymantilla committed Dec 9, 2021
1 parent e277a98 commit f8dc13d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
34 changes: 34 additions & 0 deletions library/core/tests/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&not_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 {
Expand Down
1 change: 1 addition & 0 deletions library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit f8dc13d

Please sign in to comment.