Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False unreachable_code warning for try_join #7031

Open
cblp opened this issue Dec 12, 2024 · 1 comment
Open

False unreachable_code warning for try_join #7031

cblp opened this issue Dec 12, 2024 · 1 comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-macros Module: macros in the main Tokio crate

Comments

@cblp
Copy link

cblp commented Dec 12, 2024

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.

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

And I usually run this as

#[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?

#[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.

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?

@cblp cblp added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Dec 12, 2024
@Darksonn Darksonn added the M-macros Module: macros in the main Tokio crate label Dec 12, 2024
@Darksonn
Copy link
Contributor

@taiki-e, do you think this is one of the cases where the lint should be fixed to not trigger in macros, or one of those we should fix on our end?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-macros Module: macros in the main Tokio crate
Projects
None yet
Development

No branches or pull requests

2 participants