Skip to content

Using try operator on a future doesn't suggest to .await it #97704

Closed
@coolreader18

Description

@coolreader18

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b539ebe10f181c1f2b245a021684dff1

use std::future::Future;

async fn foo() -> Result<(), i32> {
    func(async { Ok::<_, i32>(()) })?;
    
    Ok(())
}

async fn func<T>(fut: impl Future<Output = T>) -> T {
    fut.await
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the `?` operator can only be applied to values that implement `Try`
 --> src/lib.rs:4:5
  |
4 |     func(async { Ok::<_, i32>(()) })?;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `impl Future<Output = Result<(), i32>>`
  |
  = help: the trait `Try` is not implemented for `impl Future<Output = Result<(), i32>>`

For more information about this error, try `rustc --explain E0277`.

Ideally the output should look like:

Compiling playground v0.0.1 (/playground)
error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the `?` operator can only be applied to values that implement `Try`
 --> src/lib.rs:4:5
  |
4 |     func(async { Ok::<_, i32>(()) })?;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `impl Future<Output = Result<(), i32>>`
  |
  = help: try adding `.await` or something
 --> src/lib.rs:4:5
  |
4 |     func(async { Ok::<_, i32>(()) }).await?;
  |                                     ++++++
  |

For more information about this error, try `rustc --explain E0277`.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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