Skip to content

Commit dad39e8

Browse files
authored
Rollup merge of #126915 - SparkyPotato:fix-126903, r=compiler-errors
Don't suggest awaiting in closure patterns Fixes #126903. For ```rust async fn do_async() {} fn main() { Some(do_async()).map(|()| {}); } ``` the error is now ```rust error[E0308]: mismatched types --> src/main.rs:4:27 | 4 | Some(do_async()).map(|()| {}); | ^^ | | | expected future, found `()` | expected due to this | = note: expected opaque type `impl Future<Output = ()>` found unit type `()` ``` Ideally, if `main` were to be `async`, it should be ```rs error[E0308]: mismatched types --> src/main.rs:4:27 | 4 | Some(do_async()).map(|()| {}); | ^^ | | | expected future, found `()` | expected due to this | = note: expected opaque type `impl Future<Output = ()>` found unit type `()` help: consider `await`ing on the `Future` | 4 | Some(do_async().await).map(|()| {}); | ++++++ ``` However, this would mean `FnCtx::check_pat_top` would have to be called with an `origin_expr` in `rustc_hir_typeck::check::check_fn`, and that expr would have to be somehow plumbed through `FnCtxt::check_expr_closure` and closure signature deduction. I'm willing to work on the plumbing but unsure how to start.
2 parents 709baae + 26677eb commit dad39e8

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
209209
}
210210
(Some(ty), _) if self.same_type_modulo_infer(ty, exp_found.found) => match cause.code()
211211
{
212-
ObligationCauseCode::Pattern { span: Some(then_span), .. } => {
213-
Some(ConsiderAddingAwait::FutureSugg { span: then_span.shrink_to_hi() })
212+
ObligationCauseCode::Pattern { span: Some(then_span), origin_expr, .. } => {
213+
origin_expr.then_some(ConsiderAddingAwait::FutureSugg {
214+
span: then_span.shrink_to_hi(),
215+
})
214216
}
215217
ObligationCauseCode::IfExpression(box IfExpressionCause { then_id, .. }) => {
216218
let then_span = self.find_block_span_from_hir_id(*then_id);

tests/ui/async-await/suggest-missing-await.rs

+7
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,11 @@ async fn suggest_await_in_generic_pattern() {
7171
}
7272
}
7373

74+
// Issue #126903
75+
async fn do_async() {}
76+
fn dont_suggest_awaiting_closure_patterns() {
77+
Some(do_async()).map(|()| {});
78+
//~^ ERROR mismatched types [E0308]
79+
}
80+
7481
fn main() {}

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ help: consider `await`ing on the `Future`
133133
LL | match dummy_result().await {
134134
| ++++++
135135

136-
error: aborting due to 7 previous errors
136+
error[E0308]: mismatched types
137+
--> $DIR/suggest-missing-await.rs:77:27
138+
|
139+
LL | Some(do_async()).map(|()| {});
140+
| ^^
141+
| |
142+
| expected future, found `()`
143+
| expected due to this
144+
|
145+
= note: expected opaque type `impl Future<Output = ()>`
146+
found unit type `()`
147+
148+
error: aborting due to 8 previous errors
137149

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

0 commit comments

Comments
 (0)