#7031·tokio

False `unreachable_code` warning for `try_join`

Author: cblpCreated Dec 12, 2024Updated Sep 24, 2026
LabelsC-bugA-tokioM-macros

Version 1.42.0

Platform Darwin 24.1.0 Darwin Kernel Version 24.1.0: Thu Oct 10 21:02:27 PDT 2024; root:xnu-11215.41.3~2/RELEASE_X86_64 x86_64

Description Extra code for Infallible type gives false warning.

What's wrong in writing a service as an infinite loop? And an infinite loop clearly has type Infallible.

rust
async fn server() -> Infallible {
    loop {
        ...
        occasionally it may panic!()
    }
}

And I usually run this as

rust
#[tokio::main]
async fn main() {
    let s = tokio::spawn(server());
    let r = tokio::try_join!(s1);
    ...
}

Because I want to know about errors inside that server().

And that compiles and works perfectly.

But what if I want to put several workers/servers in the same process?

rust
#[tokio::main]
async fn main() {
    let s1 = tokio::spawn(server());
    let s2 = tokio::spawn(server());
    let r = tokio::try_join!(s1, s2); // (A)
    println!("reachable unreachable, r = {:?}", r); // (B)
}

This still compiles and works perfectly, but now the compiler complains about unreachable code.

rust
warning: unreachable definition
  --> src/main.rs:11:13
   |
11 |     let r = tokio::try_join!(s1, s2); // (A)
   |             ^^^^^^^^^^^^^^^^^^^^^^^^
   |             |
   |             unreachable definition
   |             any code following this expression is unreachable
   |
note: this expression has type `Infallible`, which is uninhabited
  --> src/main.rs:11:13
   |
11 |     let r = tokio::try_join!(s1, s2); // (A)
   |             ^^^^^^^^^^^^^^^^^^^^^^^^
   = note: `#[warn(unreachable_code)]` on by default
   = note: this warning originates in the macro `$crate::try_join` which comes from the expansion of the macro `tokio::try_join` (in Nightly builds, run with -Z macro-backtrace for more info)

Actually, my code is reachable, because Result<Infallible, JoinError> is inhabited, and line (B) prints something like reachable unreachable, r = Err(JoinError::Panic(Id(10), "explicit panic", ...)).

The unreacheble part is the block inside try_join that creates successful result.

Can we skip generating impossible code or at least hush this warning inside the macro?