Skip to content

Commit 6b97913

Browse files
authored
macros: avoid unreachable_code in join! and try_join! for Infallible (#8518)
Since `Infallible` became an alias for `!` in nightly Rust, the unreachable code lint sees that the macros' generated code can't be reached when an output or error type is `Infallible`, and reports it in the caller's code. Build the error result by mapping with `Err` so no expression of the error type is created, and allow the lint on the output tuple, which is unreachable whenever one of the output types is uninhabited.
1 parent 3536cd6 commit 6b97913

4 files changed

Lines changed: 102 additions & 23 deletions

File tree

‎tokio/src/macros/join.rs‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,20 @@ doc! {macro_rules! join {
189189
if is_pending {
190190
$crate::macros::support::Poll::Pending
191191
} else {
192-
$crate::macros::support::Poll::Ready(($({
193-
// Extract the future for this branch from the tuple.
194-
let ( $($skip,)* fut, .. ) = &mut futures;
195-
196-
// Safety: future is stored on the stack above
197-
// and never moved.
198-
let mut fut = unsafe { $crate::macros::support::Pin::new_unchecked(fut) };
199-
200-
fut.take_output().expect("expected completed future")
201-
},)*))
192+
// Unreachable if an output type is uninhabited (e.g. `Infallible`).
193+
#[allow(unreachable_code)]
194+
{
195+
$crate::macros::support::Poll::Ready(($({
196+
// Extract the future for this branch from the tuple.
197+
let ( $($skip,)* fut, .. ) = &mut futures;
198+
199+
// Safety: future is stored on the stack above
200+
// and never moved.
201+
let mut fut = unsafe { $crate::macros::support::Pin::new_unchecked(fut) };
202+
203+
fut.take_output().expect("expected completed future")
204+
},)*))
205+
}
202206
}
203207
}).await
204208
}};

‎tokio/src/macros/try_join.rs‎

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,16 @@ doc! {macro_rules! try_join {
229229
if $crate::macros::support::Future::poll(fut.as_mut(), cx).is_pending() {
230230
is_pending = true;
231231
} else if fut.as_mut().output_mut().expect("expected completed future").is_err() {
232-
return $crate::macros::support::Poll::Ready($crate::macros::support::Result::Err(fut.take_output().expect("expected completed future").err().unwrap()))
232+
// The error type may be uninhabited (e.g. `Infallible`).
233+
// Mapping with `Err` avoids an expression of that type,
234+
// which would trigger `unreachable_code` in the caller.
235+
return $crate::macros::support::Poll::Ready(
236+
fut.take_output()
237+
.expect("expected completed future")
238+
.err()
239+
.map($crate::macros::support::Result::Err)
240+
.unwrap()
241+
)
233242
}
234243
} else {
235244
// Future skipped, one less future to skip in the next iteration
@@ -241,20 +250,24 @@ doc! {macro_rules! try_join {
241250
if is_pending {
242251
$crate::macros::support::Poll::Pending
243252
} else {
244-
$crate::macros::support::Poll::Ready($crate::macros::support::Result::Ok(($({
245-
// Extract the future for this branch from the tuple.
246-
let ( $($skip,)* fut, .. ) = &mut futures;
253+
// Unreachable if an output type is uninhabited (e.g. `Infallible`).
254+
#[allow(unreachable_code)]
255+
{
256+
$crate::macros::support::Poll::Ready($crate::macros::support::Result::Ok(($({
257+
// Extract the future for this branch from the tuple.
258+
let ( $($skip,)* fut, .. ) = &mut futures;
247259

248-
// Safety: future is stored on the stack above
249-
// and never moved.
250-
let mut fut = unsafe { $crate::macros::support::Pin::new_unchecked(fut) };
260+
// Safety: future is stored on the stack above
261+
// and never moved.
262+
let mut fut = unsafe { $crate::macros::support::Pin::new_unchecked(fut) };
251263

252-
fut
253-
.take_output()
254-
.expect("expected completed future")
255-
.ok()
256-
.expect("expected Ok(_)")
257-
},)*)))
264+
fut
265+
.take_output()
266+
.expect("expected completed future")
267+
.ok()
268+
.expect("expected Ok(_)")
269+
},)*)))
270+
}
258271
}
259272
}).await
260273
}};

‎tokio/tests/macros_join.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,29 @@ async fn caller_names_const_count() {
254254
// not shadowing the caller-specified COUNT value
255255
assert_eq!(2, res);
256256
}
257+
258+
// Regression test for: https://github.com/tokio-rs/tokio/issues/7031
259+
// An uninhabited output type must not trigger `unreachable_code` in the caller.
260+
#[deny(unreachable_code)]
261+
#[maybe_tokio_test]
262+
async fn uninhabited_output_type() {
263+
use std::convert::Infallible;
264+
use std::future::pending;
265+
266+
let mut join = task::spawn(async { tokio::join!(pending::<Infallible>(), async { 1 }) });
267+
assert_pending!(join.poll());
268+
269+
let mut join =
270+
task::spawn(async { tokio::join!(biased; pending::<Infallible>(), async { 1 }) });
271+
assert_pending!(join.poll());
272+
273+
let mut join = task::spawn(async { tokio::join!(async { 1 }, pending::<Infallible>()) });
274+
assert_pending!(join.poll());
275+
276+
let mut join =
277+
task::spawn(async { tokio::join!(biased; async { 1 }, pending::<Infallible>()) });
278+
assert_pending!(join.poll());
279+
280+
let mut join = task::spawn(async { tokio::join!(pending::<Infallible>()) });
281+
assert_pending!(join.poll());
282+
}

‎tokio/tests/macros_try_join.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,39 @@ async fn caller_names_const_count() {
267267
// not shadowing the caller-specified COUNT value
268268
assert_eq!(2, res);
269269
}
270+
271+
// Regression test for: https://github.com/tokio-rs/tokio/issues/7031
272+
// An uninhabited `Ok` or error type must not trigger `unreachable_code`
273+
// in the caller.
274+
#[deny(unreachable_code)]
275+
#[maybe_tokio_test]
276+
async fn uninhabited_ok_type() {
277+
let res = tokio::try_join!(async { Err::<Infallible, _>(1) }, async { Ok(2) });
278+
assert_eq!(res, Err(1));
279+
280+
let res = tokio::try_join!(biased; async { Err::<Infallible, _>(1) }, async { Ok(2) });
281+
assert_eq!(res, Err(1));
282+
283+
let res = tokio::try_join!(async { Ok::<_, i32>(1) }, async { Err::<Infallible, _>(2) });
284+
assert_eq!(res, Err(2));
285+
286+
let res =
287+
tokio::try_join!(biased; async { Ok::<_, i32>(1) }, async { Err::<Infallible, _>(2) });
288+
assert_eq!(res, Err(2));
289+
290+
let res = tokio::try_join!(async { Err::<Infallible, i32>(1) });
291+
assert_eq!(res, Err(1));
292+
}
293+
294+
#[deny(unreachable_code)]
295+
#[maybe_tokio_test]
296+
async fn uninhabited_err_type() {
297+
let res = tokio::try_join!(async { Ok::<_, Infallible>(1) }, async { Ok(2) });
298+
assert_eq!(res, Ok((1, 2)));
299+
300+
let res = tokio::try_join!(biased; async { Ok::<_, Infallible>(1) }, async { Ok(2) });
301+
assert_eq!(res, Ok((1, 2)));
302+
303+
let res = tokio::try_join!(async { Ok::<_, Infallible>(1) });
304+
assert_eq!(res, Ok((1,)));
305+
}

0 commit comments

Comments
 (0)