Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
coolreader18 opened this issue Jun 3, 2022 · 6 comments · Fixed by #97721
Closed

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

coolreader18 opened this issue Jun 3, 2022 · 6 comments · Fixed by #97721
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@coolreader18
Copy link
Contributor

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`.
@coolreader18 coolreader18 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 3, 2022
@coolreader18
Copy link
Contributor Author

Huh, looks like this is a case where suggest_await_before_try doesn't kick in for some reason.

@coolreader18
Copy link
Contributor Author

coolreader18 commented Jun 3, 2022

Ah, it's that the impl Future return type of an async function like this has HAS_TY_INFER. I don't know enough about rustc's typechecking to know whether this can be resolved haha.

@coolreader18
Copy link
Contributor Author

coolreader18 commented Jun 3, 2022

It doesn't seem like the problem that caused #72766 is a thing anymore? I can remove the if self_ty.has_infer_types() { return } condition and it doesn't ICE on the example given in that issue.

@coolreader18
Copy link
Contributor Author

@JohnTitor do you know if stuff has changed such that #72775 is no longer necessary?

@compiler-errors
Copy link
Member

@coolreader18 I actually have a PR almost up that does just exactly that, though if you want to put one up instead then feel free.

@compiler-errors
Copy link
Member

I'm pretty sure #86901 fixes this underlying issue. This method could use some additional cleanup as well.

@compiler-errors compiler-errors self-assigned this Jun 4, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jun 6, 2022
…h726

Do `suggest_await_before_try` with infer variables in self, and clean up binders

Fixes rust-lang#97704

Also cleans up binders in this fn, since everything is a `Poly*` and we really shouldn't have stray escaping late-bound regions everywhere. That's why the function changed so much. This isn't necessary, so I can revert if necessary.
@bors bors closed this as completed in df86d04 Jun 6, 2022
JohnTitor added a commit to JohnTitor/rust that referenced this issue Jun 21, 2022
…li-obk

Add proper tracing spans to rustc_trait_selection::traits::error_reporting

While I was trying to figure out rust-lang#97704 I did some of this to make the logs more legible, so I figured I'd do the whole module and open a PR with it. afaict this is an ongoing process in the compiler from the log->tracing transition? but lmk if there was a reason for the more verbose forms of logging as they are.

Also, for some of the functions with only one log in them, I put the function name as a message for that log instead of `#[instrument]`-ing the whole function with a span? but maybe the latter would actually be preferable, I'm not actually sure.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants