Skip to content

Commit 4762f92

Browse files
committed
Add missing check for async body when suggesting await on futures.
1 parent a48e7b0 commit 4762f92

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1836,9 +1836,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
18361836
if should_suggest_fixes
18371837
&& !matches!(terr, TypeError::RegionsInsufficientlyPolymorphic(..))
18381838
{
1839+
let hir = self.tcx.hir_node_by_def_id(cause.body_id);
18391840
self.suggest_tuple_pattern(cause, &exp_found, diag);
18401841
self.suggest_accessing_field_where_appropriate(cause, &exp_found, diag);
1841-
self.suggest_await_on_expect_found(cause, span, &exp_found, diag);
1842+
self.suggest_await_on_expect_found(cause, span, hir, &exp_found, diag);
18421843
self.suggest_function_pointers(cause, span, &exp_found, diag);
18431844
self.suggest_turning_stmt_into_expr(cause, &exp_found, diag);
18441845
}

compiler/rustc_trait_selection/src/error_reporting/infer/suggest.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
159159
&self,
160160
cause: &ObligationCause<'tcx>,
161161
exp_span: Span,
162+
hir: hir::Node<'_>,
162163
exp_found: &ty::error::ExpectedFound<Ty<'tcx>>,
163164
diag: &mut Diag<'_>,
164165
) {
@@ -167,6 +168,23 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
167168
exp_span, exp_found.expected, exp_found.found,
168169
);
169170

171+
let is_async_block = matches!(
172+
hir,
173+
rustc_hir::Node::Expr(rustc_hir::Expr {
174+
kind: rustc_hir::ExprKind::Closure(rustc_hir::Closure {
175+
kind: rustc_hir::ClosureKind::Coroutine(rustc_hir::CoroutineKind::Desugared(
176+
rustc_hir::CoroutineDesugaring::Async,
177+
_
178+
)),
179+
..
180+
}),
181+
..
182+
})
183+
);
184+
if !is_async_block {
185+
return;
186+
}
187+
170188
if let ObligationCauseCode::CompareImplItem { .. } = cause.code() {
171189
return;
172190
}

tests/ui/async-await/coroutine-desc.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ LL | fun(one(), two());
3030
| | expected all arguments to be this future type because they need to match the type of this parameter
3131
| arguments to this function are incorrect
3232
|
33-
= help: consider `await`ing on both `Future`s
3433
= note: distinct uses of `impl Trait` result in different opaque types
3534
note: function defined here
3635
--> $DIR/coroutine-desc.rs:7:4

tests/ui/async-await/dont-suggest-missing-await.stderr

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,11 @@ LL | take_u32(x)
66
| |
77
| arguments to this function are incorrect
88
|
9-
note: calling an async function returns a future
10-
--> $DIR/dont-suggest-missing-await.rs:14:18
11-
|
12-
LL | take_u32(x)
13-
| ^
149
note: function defined here
1510
--> $DIR/dont-suggest-missing-await.rs:5:4
1611
|
1712
LL | fn take_u32(x: u32) {}
1813
| ^^^^^^^^ ------
19-
help: consider `await`ing on the `Future`
20-
|
21-
LL | take_u32(x.await)
22-
| ++++++
2314

2415
error: aborting due to 1 previous error
2516

tests/ui/impl-trait/issue-102605.stderr

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,11 @@ LL | convert_result(foo())
1414
| |
1515
| arguments to this function are incorrect
1616
|
17-
note: calling an async function returns a future
18-
--> $DIR/issue-102605.rs:13:20
19-
|
20-
LL | convert_result(foo())
21-
| ^^^^^
2217
note: function defined here
2318
--> $DIR/issue-102605.rs:7:4
2419
|
2520
LL | fn convert_result<T, E>(r: Result<T, E>) -> Option<T> {
2621
| ^^^^^^^^^^^^^^ ---------------
27-
help: consider `await`ing on the `Future`
28-
|
29-
LL | convert_result(foo().await)
30-
| ++++++
3122
help: try wrapping the expression in `Err`
3223
|
3324
LL | convert_result(Err(foo()))

0 commit comments

Comments
 (0)