-
-
Notifications
You must be signed in to change notification settings - Fork 15.8k
feat(rustc_trait_selection): suggest .await on future in E0277
#159626
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
Open
DAstapov
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
DAstapov:fix/159484
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4574,41 +4574,17 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { | |
| ) { | ||
| let future_trait = self.tcx.require_lang_item(LangItem::Future, span); | ||
|
|
||
| let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty()); | ||
| let impls_future = self.type_implements_trait( | ||
| future_trait, | ||
| [self.tcx.instantiate_bound_regions_with_erased(self_ty)], | ||
| obligation.param_env, | ||
| ); | ||
| if !impls_future.must_apply_modulo_regions() { | ||
| // Don't suggest `.await` if the `trait_pred.self_ty()` doesn't implement `Future`. | ||
| if !self.check_self_ty(obligation, trait_pred, future_trait) { | ||
| return; | ||
| } | ||
|
|
||
| let item_def_id = self.tcx.associated_item_def_ids(future_trait)[0]; | ||
| // `<T as Future>::Output` | ||
| let projection_ty = trait_pred.map_bound(|trait_pred| { | ||
| Ty::new_projection( | ||
| self.tcx, | ||
| ty::IsRigid::No, | ||
| item_def_id, | ||
| // Future::Output has no args | ||
| [trait_pred.self_ty()], | ||
| ) | ||
| }); | ||
| let InferOk { value: projection_ty, .. } = self | ||
| .at(&obligation.cause, obligation.param_env) | ||
| .normalize(Unnormalized::new_wip(projection_ty)); | ||
| // Don't suggest `.await` if `<T as Future>::Output` doesn't implement the target trait (`Try`). | ||
| if !self.check_future_output(obligation, trait_pred, future_trait) { | ||
| return; | ||
| } | ||
|
|
||
| debug!( | ||
| normalized_projection_type = ?self.resolve_vars_if_possible(projection_ty) | ||
| ); | ||
| let try_obligation = self.mk_trait_obligation_with_new_self_ty( | ||
| obligation.param_env, | ||
| trait_pred.map_bound(|trait_pred| (trait_pred, projection_ty.skip_binder())), | ||
| ); | ||
| debug!(try_trait_obligation = ?try_obligation); | ||
| if self.predicate_may_hold(&try_obligation) | ||
| && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) | ||
| if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) | ||
| && snippet.ends_with('?') | ||
| { | ||
| match self.tcx.coroutine_kind(obligation.cause.body_def_id) { | ||
|
|
@@ -4636,6 +4612,161 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { | |
| } | ||
| } | ||
|
|
||
| /// When a future is used where a non-`Future` trait is expected (e.g. `Display`, | ||
| /// `Copy`), suggest `.await` if it would satisfy the trait bound. | ||
| pub(super) fn suggest_await_on_future( | ||
| &self, | ||
| obligation: &PredicateObligation<'tcx>, | ||
| err: &mut Diag<'_>, | ||
| trait_pred: ty::PolyTraitPredicate<'tcx>, | ||
| span: Span, | ||
| ) { | ||
| let Some(future_trait) = self.tcx.lang_items().get(LangItem::Future) else { | ||
| return; | ||
| }; | ||
|
|
||
| // Don't suggest `.await` if the target trait is `Future` (that's a different error). | ||
| if trait_pred.def_id() == future_trait { | ||
| return; | ||
| } | ||
|
|
||
| // Don't suggest `.await` if the target trait is `Try` - `suggest_await_before_try` handles this. | ||
| if self.tcx.is_lang_item(trait_pred.def_id(), LangItem::Try) { | ||
| return; | ||
| } | ||
|
|
||
| // Non-lifetime binders (e.g. `for<T>`) produce bound types that | ||
| // `instantiate_bound_regions_with_erased` in `self.check_self_ty` cannot handle. | ||
| if trait_pred.bound_vars().len() > 0 { | ||
| return; | ||
| } | ||
|
|
||
| // Don't suggest `.await` if the `trait_pred.self_ty()` doesn't implement `Future`. | ||
| if !self.check_self_ty(obligation, trait_pred, future_trait) { | ||
| return; | ||
| } | ||
|
|
||
| // Don't suggest `.await` if `<T as Future>::Output` doesn't implement the target trait. | ||
| if !self.check_future_output(obligation, trait_pred, future_trait) { | ||
| return; | ||
| } | ||
|
|
||
| let Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) = | ||
| self.tcx.coroutine_kind(obligation.cause.body_def_id) | ||
| else { | ||
| return; | ||
| }; | ||
|
|
||
| let body = self.tcx.hir_body_owned_by(obligation.cause.body_def_id); | ||
|
|
||
| // Don't suggest `.await` if the span is outside the function body | ||
| // (e.g. in a parameter type like `f: dyn Future`). | ||
| if !body.value.span.contains(span) { | ||
| return; | ||
| } | ||
|
|
||
| let sm = self.tcx.sess.source_map(); | ||
|
|
||
| // Check if span is inside a format macro (e.g. `{n}` in `println!("{n}")`). | ||
| if let Ok(snippet) = sm.span_to_snippet(span) | ||
| && snippet.starts_with('{') | ||
| && snippet.ends_with('}') | ||
| && let Some(var_name) = | ||
| snippet.trim_start_matches('{').trim_end_matches('}').split(':').next() | ||
| && !var_name.is_empty() | ||
| && let Some(let_stmt) = self.find_let_binding_by_name(&body.value, var_name) | ||
| && let hir::PatKind::Binding(_, _, ident, _) = let_stmt.pat.kind | ||
| { | ||
| // Format macro - add explicit let binding with `.await`. | ||
| let ident_name = ident.name; | ||
| let padding = sm.indentation_before(let_stmt.span).unwrap_or_default(); | ||
| err.multipart_suggestion( | ||
| "consider `await`ing on the `Future`", | ||
| vec![( | ||
| let_stmt.span.shrink_to_hi(), | ||
| format!("\n{padding}let {ident_name} = {ident_name}.await;"), | ||
| )], | ||
| Applicability::MaybeIncorrect, | ||
| ); | ||
| } else { | ||
| // Other - add `.await` at the usage site. | ||
| err.span_suggestion_verbose( | ||
| span.shrink_to_hi(), | ||
| "consider `await`ing on the `Future`", | ||
| ".await", | ||
| Applicability::MaybeIncorrect, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /// Check if `trait_pred.self_ty()` implements `Future`. | ||
| fn check_self_ty( | ||
| &self, | ||
| obligation: &PredicateObligation<'tcx>, | ||
| trait_pred: ty::PolyTraitPredicate<'tcx>, | ||
| future_trait: DefId, | ||
| ) -> bool { | ||
| let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty()); | ||
| let impls_future = self.type_implements_trait( | ||
| future_trait, | ||
| [self.tcx.instantiate_bound_regions_with_erased(self_ty)], | ||
| obligation.param_env, | ||
| ); | ||
| impls_future.must_apply_modulo_regions() | ||
| } | ||
|
|
||
| /// Check if `<T as Future>::Output` implements a given trait. | ||
| fn check_future_output( | ||
| &self, | ||
| obligation: &PredicateObligation<'tcx>, | ||
| trait_pred: ty::PolyTraitPredicate<'tcx>, | ||
| future_trait: DefId, | ||
| ) -> bool { | ||
| let item_def_id = self.tcx.associated_item_def_ids(future_trait)[0]; | ||
| let projection_ty = trait_pred.map_bound(|trait_pred| { | ||
| Ty::new_projection(self.tcx, ty::IsRigid::No, item_def_id, [trait_pred.self_ty()]) | ||
| }); | ||
| let InferOk { value: projection_ty, .. } = self | ||
| .at(&obligation.cause, obligation.param_env) | ||
| .normalize(Unnormalized::new_wip(projection_ty)); | ||
| let output_obligation = self.mk_trait_obligation_with_new_self_ty( | ||
| obligation.param_env, | ||
| trait_pred.map_bound(|trait_pred| (trait_pred, projection_ty.skip_binder())), | ||
| ); | ||
| self.predicate_may_hold(&output_obligation) | ||
| } | ||
|
|
||
| /// Find a `let` binding in the body by variable name. | ||
| fn find_let_binding_by_name<'hir>( | ||
| &self, | ||
| body_expr: &'hir hir::Expr<'hir>, | ||
| name: &str, | ||
| ) -> Option<&'hir hir::LetStmt<'hir>> { | ||
| use hir::intravisit::Visitor; | ||
|
|
||
| struct FindLetVisitor<'a, 'hir> { | ||
| name: &'a str, | ||
| found: Option<&'hir hir::LetStmt<'hir>>, | ||
| } | ||
|
|
||
| impl<'a, 'hir> Visitor<'hir> for FindLetVisitor<'a, 'hir> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scanning the entire body by textual name can select an unrelated shadowed binding and insert for example: async fn make_number() -> i32 {
42
}
async fn f() {
let number = make_number();
println!("{number}");
{
let number = 0;
}
} |
||
| fn visit_stmt(&mut self, stmt: &'hir hir::Stmt<'hir>) { | ||
| if let hir::StmtKind::Let(let_stmt) = &stmt.kind | ||
| && let hir::PatKind::Binding(_, _, ident, _) = let_stmt.pat.kind | ||
| && ident.name.as_str() == self.name | ||
| { | ||
| self.found = Some(let_stmt); | ||
| return; | ||
| } | ||
| hir::intravisit::walk_stmt(self, stmt); | ||
| } | ||
| } | ||
|
|
||
| let mut visitor = FindLetVisitor { name, found: None }; | ||
| visitor.visit_expr(body_expr); | ||
| visitor.found | ||
| } | ||
|
|
||
| pub(super) fn suggest_floating_point_literal( | ||
| &self, | ||
| obligation: &PredicateObligation<'tcx>, | ||
|
|
||
30 changes: 30 additions & 0 deletions
30
tests/ui/async-await/suggest-await-on-future-for-trait-bound.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| //! Regression test for <https://github.com/rust-lang/rust/issues/159484>. | ||
| //! When a future is used directly where a trait is expected (e.g. `take_copy(n)`), | ||
| //! suggest adding `.await` at the usage site: `take_copy(n.await)`. | ||
| //! Also checks that no suggestion is given when `Future::Output` doesn't implement the trait. | ||
| //@ edition:2021 | ||
|
|
||
| #![crate_type = "lib"] | ||
|
|
||
| fn take_copy(_: impl Copy) {} | ||
|
|
||
| async fn make_number() -> i32 { | ||
| 42 | ||
| } | ||
|
|
||
| async fn use_number() { | ||
| let number = make_number(); | ||
| take_copy(number); | ||
| //~^ ERROR the trait bound `impl Future<Output = i32>: Copy` is not satisfied | ||
| } | ||
|
|
||
| async fn make_string() -> String { | ||
| String::new() | ||
| } | ||
|
|
||
| // String doesn't implement Copy, so no suggestion. | ||
| async fn use_string() { | ||
| let string = make_string(); | ||
| take_copy(string); | ||
| //~^ ERROR the trait bound `impl Future<Output = String>: Copy` is not satisfied | ||
| } |
35 changes: 35 additions & 0 deletions
35
tests/ui/async-await/suggest-await-on-future-for-trait-bound.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| error[E0277]: the trait bound `impl Future<Output = i32>: Copy` is not satisfied | ||
| --> $DIR/suggest-await-on-future-for-trait-bound.rs:17:15 | ||
| | | ||
| LL | take_copy(number); | ||
| | --------- ^^^^^^ the trait `Copy` is not implemented for `impl Future<Output = i32>` | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| note: required by a bound in `take_copy` | ||
| --> $DIR/suggest-await-on-future-for-trait-bound.rs:9:22 | ||
| | | ||
| LL | fn take_copy(_: impl Copy) {} | ||
| | ^^^^ required by this bound in `take_copy` | ||
| help: consider `await`ing on the `Future` | ||
| | | ||
| LL | take_copy(number.await); | ||
| | ++++++ | ||
|
|
||
| error[E0277]: the trait bound `impl Future<Output = String>: Copy` is not satisfied | ||
| --> $DIR/suggest-await-on-future-for-trait-bound.rs:28:15 | ||
| | | ||
| LL | take_copy(string); | ||
| | --------- ^^^^^^ the trait `Copy` is not implemented for `impl Future<Output = String>` | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| note: required by a bound in `take_copy` | ||
| --> $DIR/suggest-await-on-future-for-trait-bound.rs:9:22 | ||
| | | ||
| LL | fn take_copy(_: impl Copy) {} | ||
| | ^^^^ required by this bound in `take_copy` | ||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0277`. |
33 changes: 33 additions & 0 deletions
33
tests/ui/async-await/suggest-await-on-future-in-format-macro.fixed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| //! Regression test for <https://github.com/rust-lang/rust/issues/159484>. | ||
| //! When a future is used inside a format macro like `println!("{number}")`, the | ||
| //! span is `{number}` (inside the string literal), so `.await` cannot be added | ||
| //! directly. Instead, suggest adding an intermediate `let number = number.await;`. | ||
| //@ edition:2021 | ||
| //@ run-rustfix | ||
|
|
||
| #![crate_type = "lib"] | ||
|
|
||
| async fn make_number() -> i32 { | ||
| 42 | ||
| } | ||
|
|
||
| pub async fn println_number_display() { | ||
| let number = make_number(); | ||
| let number = number.await; | ||
| println!("{number}"); | ||
| //~^ ERROR `impl Future<Output = i32>` doesn't implement `std::fmt::Display` | ||
| } | ||
|
|
||
| pub async fn println_number_debug() { | ||
| let number = make_number(); | ||
| let number = number.await; | ||
| println!("{number:?}"); | ||
| //~^ ERROR `impl Future<Output = i32>` doesn't implement `Debug` | ||
| } | ||
|
|
||
| pub async fn println_number_display_format_spec() { | ||
| let number = make_number(); | ||
| let number = number.await; | ||
| println!("{number:.2}"); | ||
| //~^ ERROR `impl Future<Output = i32>` doesn't implement `std::fmt::Display` | ||
| } |
30 changes: 30 additions & 0 deletions
30
tests/ui/async-await/suggest-await-on-future-in-format-macro.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| //! Regression test for <https://github.com/rust-lang/rust/issues/159484>. | ||
| //! When a future is used inside a format macro like `println!("{number}")`, the | ||
| //! span is `{number}` (inside the string literal), so `.await` cannot be added | ||
| //! directly. Instead, suggest adding an intermediate `let number = number.await;`. | ||
| //@ edition:2021 | ||
| //@ run-rustfix | ||
|
|
||
| #![crate_type = "lib"] | ||
|
|
||
| async fn make_number() -> i32 { | ||
| 42 | ||
| } | ||
|
|
||
| pub async fn println_number_display() { | ||
| let number = make_number(); | ||
| println!("{number}"); | ||
| //~^ ERROR `impl Future<Output = i32>` doesn't implement `std::fmt::Display` | ||
| } | ||
|
|
||
| pub async fn println_number_debug() { | ||
| let number = make_number(); | ||
| println!("{number:?}"); | ||
| //~^ ERROR `impl Future<Output = i32>` doesn't implement `Debug` | ||
| } | ||
|
|
||
| pub async fn println_number_display_format_spec() { | ||
| let number = make_number(); | ||
| println!("{number:.2}"); | ||
| //~^ ERROR `impl Future<Output = i32>` doesn't implement `std::fmt::Display` | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fallback can still be reached for a captured format argument when the binding is not a top-level PatKind::Binding, for example:
Here span is still inside the format string, so the suggestion produces println!("{number}.await"), which does not actually await the future.
View changes since the review