Skip to content

Commit

Permalink
Add allow(unreachable_code) for usage of try_join with the never type
Browse files Browse the repository at this point in the history
Fixes #1992
  • Loading branch information
Nemo157 authored and cramertj committed Dec 10, 2019
1 parent aa12702 commit 5f9ae4c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions futures-macro/src/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ pub(crate) fn try_join(input: TokenStream) -> TokenStream {
} else if unsafe { #futures_crate::core_reexport::pin::Pin::new_unchecked(&mut #fut) }.output_mut().unwrap().is_err() {
// `.err().unwrap()` rather than `.unwrap_err()` so that we don't introduce
// a `T: Debug` bound.
// Also, for an error type of ! any code after `err().unwrap()` is unreachable.
#[allow(unreachable_code)]
return #futures_crate::core_reexport::task::Poll::Ready(
#futures_crate::core_reexport::result::Result::Err(
unsafe { #futures_crate::core_reexport::pin::Pin::new_unchecked(&mut #fut) }.take_output().unwrap().err().unwrap()
Expand All @@ -141,6 +143,8 @@ pub(crate) fn try_join(input: TokenStream) -> TokenStream {
quote! {
// `.ok().unwrap()` rather than `.unwrap()` so that we don't introduce
// an `E: Debug` bound.
// Also, for an ok type of ! any code after `ok().unwrap()` is unreachable.
#[allow(unreachable_code)]
unsafe { #futures_crate::core_reexport::pin::Pin::new_unchecked(&mut #fut) }.take_output().unwrap().ok().unwrap(),
}
});
Expand Down
23 changes: 23 additions & 0 deletions futures/tests/try_join.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![deny(unreachable_code)]

use futures::{try_join, executor::block_on};

#[test]
fn try_join_never_error() {
block_on(async {
let future1 = async { Ok::<(), !>(()) };
let future2 = async { Ok::<(), !>(()) };
try_join!(future1, future2)
})
.unwrap();
}

#[test]
fn try_join_never_ok() {
block_on(async {
let future1 = async { Err::<!, ()>(()) };
let future2 = async { Err::<!, ()>(()) };
try_join!(future1, future2)
})
.unwrap_err();
}

0 comments on commit 5f9ae4c

Please sign in to comment.