Skip to content

Commit 5f9ae4c

Browse files
Nemo157cramertj
authored andcommitted
Add allow(unreachable_code) for usage of try_join with the never type
Fixes #1992
1 parent aa12702 commit 5f9ae4c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

futures-macro/src/join.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ pub(crate) fn try_join(input: TokenStream) -> TokenStream {
129129
} else if unsafe { #futures_crate::core_reexport::pin::Pin::new_unchecked(&mut #fut) }.output_mut().unwrap().is_err() {
130130
// `.err().unwrap()` rather than `.unwrap_err()` so that we don't introduce
131131
// a `T: Debug` bound.
132+
// Also, for an error type of ! any code after `err().unwrap()` is unreachable.
133+
#[allow(unreachable_code)]
132134
return #futures_crate::core_reexport::task::Poll::Ready(
133135
#futures_crate::core_reexport::result::Result::Err(
134136
unsafe { #futures_crate::core_reexport::pin::Pin::new_unchecked(&mut #fut) }.take_output().unwrap().err().unwrap()
@@ -141,6 +143,8 @@ pub(crate) fn try_join(input: TokenStream) -> TokenStream {
141143
quote! {
142144
// `.ok().unwrap()` rather than `.unwrap()` so that we don't introduce
143145
// an `E: Debug` bound.
146+
// Also, for an ok type of ! any code after `ok().unwrap()` is unreachable.
147+
#[allow(unreachable_code)]
144148
unsafe { #futures_crate::core_reexport::pin::Pin::new_unchecked(&mut #fut) }.take_output().unwrap().ok().unwrap(),
145149
}
146150
});

futures/tests/try_join.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![deny(unreachable_code)]
2+
3+
use futures::{try_join, executor::block_on};
4+
5+
#[test]
6+
fn try_join_never_error() {
7+
block_on(async {
8+
let future1 = async { Ok::<(), !>(()) };
9+
let future2 = async { Ok::<(), !>(()) };
10+
try_join!(future1, future2)
11+
})
12+
.unwrap();
13+
}
14+
15+
#[test]
16+
fn try_join_never_ok() {
17+
block_on(async {
18+
let future1 = async { Err::<!, ()>(()) };
19+
let future2 = async { Err::<!, ()>(()) };
20+
try_join!(future1, future2)
21+
})
22+
.unwrap_err();
23+
}

0 commit comments

Comments
 (0)