macros: allow select with only else branch (#6339)

This commit is contained in:
Gil Shoshan 2024-02-13 10:28:16 +02:00 committed by GitHub
parent 84e41d4aff
commit 2ce1cee0f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 0 deletions

View File

@ -608,6 +608,10 @@ macro_rules! select {
// ===== Entry point =====
($(biased;)? else => $else:expr $(,)? ) => {{
$else
}};
(biased; $p:pat = $($t:tt)* ) => {
$crate::select!(@{ start=0; () } $p = $($t)*)
};
@ -617,6 +621,7 @@ macro_rules! select {
// fair and avoids always polling the first future.
$crate::select!(@{ start={ $crate::macros::support::thread_rng_n(BRANCHES) }; () } $p = $($t)*)
};
() => {
compile_error!("select! requires at least one branch.")
};

View File

@ -22,6 +22,25 @@ async fn sync_one_lit_expr_comma() {
assert_eq!(foo, 1);
}
#[maybe_tokio_test]
async fn no_branch_else_only() {
let foo = tokio::select! {
else => 1,
};
assert_eq!(foo, 1);
}
#[maybe_tokio_test]
async fn no_branch_else_only_biased() {
let foo = tokio::select! {
biased;
else => 1,
};
assert_eq!(foo, 1);
}
#[maybe_tokio_test]
async fn nested_one() {
let foo = tokio::select! {