Skip to content

Error in async function/block produces errors for valid unrelated expressions  #66634

Open

Description

@olegnn

Code (Playground)

use futures::{future::ready, join};

async fn _incorrect_error() {
    join! { ready(0) };
    // Produces cannot infer type for `{integer}` for expression above
    0.await;
}

fn main() {}

produces following errors:

Errors

error[E0277]: the trait bound `{integer}: core::future::future::Future` is not satisfied
  --> src/main.rs:6:5
   |
6  |     0.await;
   |     ^^^^^^^ the trait `core::future::future::Future` is not implemented for `{integer}`
   |
   = help: the following implementations were found:
             <&mut F as core::future::future::Future>
             <futures_channel::oneshot::Receiver<T> as core::future::future::Future>
             <futures_task::future_obj::FutureObj<'_, T> as core::future::future::Future>
             <futures_task::future_obj::LocalFutureObj<'_, T> as core::future::future::Future>
           and 84 others

error[E0698]: type inside `async fn` body must be known in this context
 --> src/main.rs:4:5
  |
4 |     join! { ready(0) };
  |     ^^^^^^^^^^^^^^^^^^^ cannot infer type for `{integer}`
  |
note: the type is part of the `async fn` body because of this `await`
 --> src/main.rs:4:5
  |
4 |     join! { ready(0) };
  |     ^^^^^^^^^^^^^^^^^^^
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0698]: type inside `async fn` body must be known in this context
 --> src/main.rs:4:5
  |
4 |     join! { ready(0) };
  |     ^^^^^^^^^^^^^^^^^^^ cannot infer type for `{integer}`
  |
note: the type is part of the `async fn` body because of this `await`
 --> src/main.rs:4:5
  |
4 |     join! { ready(0) };
  |     ^^^^^^^^^^^^^^^^^^^
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0698]: type inside `async fn` body must be known in this context
 --> src/main.rs:4:5
  |
4 |     join! { ready(0) };
  |     ^^^^^^^^^^^^^^^^^^^ cannot infer type for `{integer}`
  |
note: the type is part of the `async fn` body because of this `await`
 --> src/main.rs:4:5
  |
4 |     join! { ready(0) };
  |     ^^^^^^^^^^^^^^^^^^^
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0698]: type inside `async fn` body must be known in this context
 --> src/main.rs:6:5
  |
6 |     0.await;
  |     ^ cannot infer type for `{integer}`
  |
note: the type is part of the `async fn` body because of this `await`
 --> src/main.rs:6:5
  |
6 |     0.await;
  |     ^^^^^^^

error[E0698]: type inside `async fn` body must be known in this context
 --> src/main.rs:6:5
  |
6 |     0.await;
  |     ^^^^^^^ cannot infer type for `{integer}`
  |
note: the type is part of the `async fn` body because of this `await`
 --> src/main.rs:6:5
  |
6 |     0.await;
  |     ^^^^^^^

However, if we remove line

0.await;

, it will become valid. Error on this line affects unrelated

join! { ready(0) };

and produces cannot infer type for {integer} error for valid expression. This behaviour could seem not so bad, but imagine large async function with many lines (you can simply copy-paste first line several times and look to the output).

Activity

  1. changed the title [-]Type error in `async` function produces errors in valid unrelated expressions [/-] [+]Type error in `async` function produces errors for valid unrelated expressions [/+] on Nov 22, 2019
  2. changed the title [-]Type error in `async` function produces errors for valid unrelated expressions [/-] [+]Error in `async` function produces errors for valid unrelated expressions [/+] on Nov 22, 2019
  3. changed the title [-]Error in `async` function produces errors for valid unrelated expressions [/-] [+]Error in `async` function/block produces errors for valid unrelated expressions [/+] on Nov 22, 2019
  4. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    C-bugCategory: This is a bug.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Nov 22, 2019
  5. added
    AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.
    on Dec 3, 2019
  6. jimblandy commented on Jun 20, 2020

    @jimblandy
    Contributor

    Another example: playground

    What's really weird is that I get five copies of the same error message.

    use std::io::Error;
    
    fn main() {
        let server = async {
            for _connection in 0..2 {
                futures::future::ready(Ok::<(), Error>(())).await?;
            }
    
            Ok::<(), Error>(())
        };
    
        server.await;
    }
    
  7. estebank commented on Sep 28, 2025

    @estebank
    Contributor

    Current output:

    error[E0277]: `{integer}` is not a future
     --> src/main.rs:6:7
      |
    6 |     0.await;
      |       ^^^^^ `{integer}` is not a future
      |
      = help: the trait `futures::Future` is not implemented for `{integer}`
      = note: {integer} must be a future or must implement `IntoFuture` to be awaited
      = note: required for `{integer}` to implement `std::future::IntoFuture`
    help: remove the `.await`
      |
    6 -     0.await;
    6 +     0;
      |
    
    error[E0728]: `await` is only allowed inside `async` functions and blocks
      --> src/main.rs:12:12
       |
     3 | fn main() {
       | --------- this is not `async`
    ...
    12 |     server.await;
       |            ^^^^^ only allowed inside `async` functions and blocks
    

    If we change the above to be within an async fn, the code compiles.

  8. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Sep 28, 2025
  9. Enselic commented on Dec 23, 2025

    @Enselic
    Member

    Triage: My understanding is that we are happy with the current output of the code in the issue description and just need a test to make sure it stays the way it looks? Unfortunately we can't add the original code as a test yet since it depends on futures, so we need someone to come up with a MCVE before we can add this as a ui test. Relabeling accordingly.

  10. added
    E-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example
    and removed
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Dec 23, 2025
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-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.E-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable ExampleT-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