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

Add missing check for async body when suggesting await on futures. #135492

Merged
merged 1 commit into from
Jan 24, 2025

Conversation

metamuffin
Copy link
Contributor

Currently the compiler suggests adding .await to resolve some type conflicts without checking if the conflict happens in an async context. This can lead to the compiler suggesting .await in function signatures where it is invalid. Example:

trait A {
    fn a() -> impl Future<Output = ()>;
}
struct B;
impl A for B {
    fn a() -> impl Future<Output = impl Future<Output = ()>> {
        async { async { () } }
    }
}
error[E0271]: expected `impl Future<Output = impl Future<Output = ()>>` to be a future that resolves to `()`, but it resolves to `impl Future<Output = ()>`
 --> bug.rs:6:15
  |
6 |     fn a() -> impl Future<Output = impl Future<Output = ()>> {
  |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found future
  |
note: calling an async function returns a future
 --> bug.rs:6:15
  |
6 |     fn a() -> impl Future<Output = impl Future<Output = ()>> {
  |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a bound in `A::{synthetic#0}`
 --> bug.rs:2:27
  |
2 |     fn a() -> impl Future<Output = ()>;
  |                           ^^^^^^^^^^^ required by this bound in `A::{synthetic#0}`
help: consider `await`ing on the `Future`
  |
6 |     fn a() -> impl Future<Output = impl Future<Output = ()>>.await {
  |                                                             ++++++

The documentation of suggest_await_on_expect_found (compiler/rustc_trait_selection/src/error_reporting/infer/suggest.rs:156) even mentions such a check but does not actually implement it.

This PR adds that check to ensure .await is only suggested within async blocks.

There were 3 unit tests whose expected output needed to be changed because they had the suggestion outside of async. One of them (tests/ui/async-await/dont-suggest-missing-await.rs) actually tests that exact problem but expects it to be present.

Thanks to @llenck for initially noticing the bug and helping with fixing it

@rustbot
Copy link
Collaborator

rustbot commented Jan 14, 2025

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @BoxyUwU (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 14, 2025
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 14, 2025
@metamuffin
Copy link
Contributor Author

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 14, 2025
Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls fix this and also squash this into one commit at the end.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 16, 2025
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 16, 2025
@compiler-errors
Copy link
Member

Did you see my comment? Please squash this into one commit.

@metamuffin metamuffin force-pushed the bug-invalid-await-suggest branch from 7945d60 to ab2c8ff Compare January 16, 2025 20:35
Copy link
Contributor Author

@metamuffin metamuffin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont understand the github merge workflow. Sorry. Does the squashed commit I pushed yesterday look ok?

@compiler-errors
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jan 23, 2025

📌 Commit ab2c8ff has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 23, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 24, 2025
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#135073 (Implement `ByteStr` and `ByteString` types)
 - rust-lang#135492 (Add missing check for async body when suggesting await on futures.)
 - rust-lang#135766 (handle global trait bounds defining assoc types)
 - rust-lang#135880 (Get rid of RunCompiler)
 - rust-lang#135908 (rustc_codegen_llvm: remove outdated asm-to-obj codegen note)
 - rust-lang#135911 (Allow `arena_cache` queries to return `Option<&'tcx T>`)
 - rust-lang#135920 (simplify parse_format::Parser::ws by using next_if)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 4496f23 into rust-lang:master Jan 24, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants