Skip to content

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

Closed

Description

@nextzhou

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

Activity

  1. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    C-bugCategory: This is a bug.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jun 13, 2019
  2. nikomatsakis commented on Jun 25, 2019

    @nikomatsakis
    Contributor

    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.

  3. nikomatsakis commented on Jun 25, 2019

    @nikomatsakis
    Contributor

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

  4. Centril commented on Jun 25, 2019

    @Centril
    Contributor

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

  5. ChrisGreenaway commented on Sep 18, 2019

    @ChrisGreenaway

    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 | |     }
       | |_____^
    
    
  6. PvdBerg1998 commented on Sep 19, 2019

    @PvdBerg1998

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

  7. MOZGIII commented on Sep 25, 2019

    @MOZGIII
    Contributor

    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.

  8. PvdBerg1998 commented on Sep 25, 2019

    @PvdBerg1998

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

  9. added 2 commits that reference this issue on Oct 1, 2019
    cac592f
    f4aa29f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions