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

unreachable_code warning when invoke .await on async diverging function #61798

Closed
nextzhou opened this issue Jun 13, 2019 · 7 comments
Closed

unreachable_code warning when invoke .await on async diverging function #61798

nextzhou opened this issue Jun 13, 2019 · 7 comments

Comments

@nextzhou
Copy link

@nextzhou nextzhou commented Jun 13, 2019

I tried this code:

async fn foo() {
    endless().await;
}

async fn endless() -> ! {
    loop {}
}

I expected to see this happen: no build warn

Instead, this happened:

warning: unreachable expression
 --> src/main.rs:8:5
  |
8 |     endless().await;
  |     ^^^^^^^^^^^^^^^
  |
  = note: #[warn(unreachable_code)] on by default

Meta

rustc --version --verbose:

rustc 1.37.0-nightly (5f3656ce9 2019-06-11)
binary: rustc
commit-hash: 5f3656ce9a2212fad872605b7a4ee103a155e9f3
commit-date: 2019-06-11
host: x86_64-unknown-linux-gnu
release: 1.37.0-nightly
LLVM version: 8.0
@nikomatsakis
Copy link
Contributor

@nikomatsakis nikomatsakis commented Jun 25, 2019

We discussed this a bit on Zulip. The likely cause is that parts of the await desugaring are being linted against. We can likely check the span to see if the code that is being linted against is part of the .await desugaring, but we do want to be careful to ensure that

async fn foo() {
    return; bar().await;
}

async fn bar() {
}

still gets a lint warning.

@nikomatsakis
Copy link
Contributor

@nikomatsakis nikomatsakis commented Jun 25, 2019

Marking as deferred as this doesn't seem like a problem that needs to block stabilization.

@Centril
Copy link
Contributor

@Centril Centril commented Jun 25, 2019

cc @varkor who has done some work on the inhabited logic stuff.

@ChrisGreenaway
Copy link

@ChrisGreenaway ChrisGreenaway commented Sep 18, 2019

I'm now getting this warning even for functions that loop but eventually return. It worked fine with nightly-2019-08-21, but the latest nightly (2019-09-17) is broken.

For example, this code generates a warning:

async fn run(&mut self) {
    loop {
        match self.target_status {
            Some(_) => self.update_status().await,
            None => match &self.current_status {
                OperationStatus::PENDING => self.set_target_status(OperationStatus::EXECUTING),
                OperationStatus::EXECUTING => match self.operation.run().await {
                    Ok(()) => self.set_target_status(OperationStatus::SUCCESSFUL),
                    Err(e) => self.set_target_status(OperationStatus::FAILED(format!("{:?}", e))),
                },
                _ => return,
            },
        }
    }
}

with the warning repeated twice:

  --> src\operations.rs:82:29
   |
82 |       async fn run(&mut self) {
   |  _____________________________^
83 | |         loop {
84 | |             match self.target_status {
85 | |                 Some(_) => self.update_status().await,
...  |
95 | |         }
96 | |     }
   | |_____^
error: unreachable expression
  --> src\operations.rs:82:29
   |
82 |       async fn run(&mut self) {
   |  _____________________________^
83 | |         loop {
84 | |             match self.target_status {
85 | |                 Some(_) => self.update_status().await,
...  |
95 | |         }
96 | |     }
   | |_____^

@PvdBerg1998
Copy link

@PvdBerg1998 PvdBerg1998 commented Sep 19, 2019

Can confirm. I believe this regressed somewhere after nightly 09-09-2019.

@MOZGIII
Copy link
Contributor

@MOZGIII MOZGIII commented Sep 25, 2019

I think I have this in my code here: https://github.com/MOZGIII/netsound/blob/cc16cd94c3b5d0e75e18719f223e205f4aaaadb0/src/net/recv.rs#L46

Can somebody confirm this? If not I'll file another bug report.

@PvdBerg1998
Copy link

@PvdBerg1998 PvdBerg1998 commented Sep 25, 2019

@MOZGIII yeah that's the same, the function can "never" return from the unconditional loop.

Centril added a commit to Centril/rust that referenced this issue Oct 1, 2019
…it, r=petrochenkov

Silence unreachable code lint from await desugaring

Fixes rust-lang#61798.

This PR silences the unreachable code lint when it originates from within an await desugaring.
@bors bors closed this in f4aa29f Oct 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

7 participants