Skip to content

Awaiting a function returning a never type does not warn for unreachable code after the await #128434

Closed

Description

@ungrichtepfl

Code

pub async fn endless_async() -> ! {
    loop {
        tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
    }
}

pub async fn should_warn() {
    endless_async().await;
    println!("This code is unreachable");  // no warning about unreachable code
}

pub fn endless() -> ! {
    loop {
        std::thread::sleep(std::time::Duration::from_secs(1));
    }
}

pub fn warns() {
    endless();
    println!("This code is unreachable");  // warning: unreachable statement
}

Current output

warning: unreachable statement
  --> src/lib.rs:20:5
   |
19 |     endless();
   |     --------- any code following this expression is unreachable
20 |     println!("This code is unreachable");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unreachable statement
   |
   = note: `#[warn(unreachable_code)]` on by default
   = note: this warning originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

Desired output

warning: unreachable statement
  --> src/lib.rs:9:5
   |
 8 |     endless_async().await;
   |     --------------------- any code following this expression is unreachable
 9 |     println!("This code is unreachable");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unreachable statement
   |
   = note: `#[warn(unreachable_code)]` on by default
   = note: this warning originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
warning: unreachable statement
  --> src/lib.rs:20:5
   |
19 |     endless();
   |     --------- any code following this expression is unreachable
20 |     println!("This code is unreachable");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unreachable statement
   |
   = note: `#[warn(unreachable_code)]` on by default
   = note: this warning originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

Rationale and extra context

No response

Other cases

No response

Rust Version

rustc 1.80.0 (051478957 2024-07-21)
binary: rustc
commit-hash: 051478957371ee0084a7c0913941d2a8c4757bb9
commit-date: 2024-07-21
host: x86_64-unknown-linux-gnu
release: 1.80.0
LLVM version: 18.1.7

Anything else?

Same behaviour with the current nightly version:

rustc 1.82.0-nightly (f8060d282 2024-07-30)
binary: rustc
commit-hash: f8060d282d42770fadd73905e3eefb85660d3278
commit-date: 2024-07-30
host: x86_64-unknown-linux-gnu
release: 1.82.0-nightly
LLVM version: 18.1.7

Activity

  1. added
    A-diagnostics
    T-compiler
    on Jul 31, 2024
  2. added
    WG-async
    on Jul 31, 2024
  3. compiler-errors commented on Jul 31, 2024

    @compiler-errors
    Contributor

    I'll take a look at this 👀

    This almost certainly has something to do with the await desugaring into a poll loop

  4. added a commit that references this issue on Jul 31, 2024
    5942456
  5. added a commit that references this issue on Jul 31, 2024
    ac67d10
  6. added a commit that references this issue on Aug 1, 2024
    ffc0e2e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-diagnosticsT-compilerWG-async

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions