Skip to content

Commit 484fa44

Browse files
authored
Rollup merge of #98879 - compiler-errors:async-closure-wrap-parens, r=oli-obk
Fix "wrap closure in parenthesis" suggestion for `async` closure Fixes #98023
2 parents bff8a2c + eef5630 commit 484fa44

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

compiler/rustc_typeck/src/check/callee.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
280280
callee_node: &hir::ExprKind<'_>,
281281
callee_span: Span,
282282
) {
283-
let hir_id = self.tcx.hir().get_parent_node(hir_id);
284-
let parent_node = self.tcx.hir().get(hir_id);
283+
let hir = self.tcx.hir();
284+
let parent_hir_id = hir.get_parent_node(hir_id);
285+
let parent_node = hir.get(parent_hir_id);
285286
if let (
286287
hir::Node::Expr(hir::Expr {
287-
kind: hir::ExprKind::Closure { fn_decl_span, .. }, ..
288+
kind: hir::ExprKind::Closure { fn_decl_span, body, .. },
289+
..
288290
}),
289291
hir::ExprKind::Block(..),
290292
) = (parent_node, callee_node)
291293
{
294+
let fn_decl_span = if hir.body(*body).generator_kind
295+
== Some(hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Closure))
296+
{
297+
// Actually need to unwrap a few more layers of HIR to get to
298+
// the _real_ closure...
299+
let async_closure = hir.get_parent_node(hir.get_parent_node(parent_hir_id));
300+
if let hir::Node::Expr(hir::Expr {
301+
kind: hir::ExprKind::Closure { fn_decl_span, .. },
302+
..
303+
}) = hir.get(async_closure)
304+
{
305+
*fn_decl_span
306+
} else {
307+
return;
308+
}
309+
} else {
310+
*fn_decl_span
311+
};
312+
292313
let start = fn_decl_span.shrink_to_lo();
293314
let end = callee_span.shrink_to_hi();
294315
err.multipart_suggestion(
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
// edition:2021
2+
3+
#![feature(async_closure)]
4+
15
fn main() {
26
let _ = ||{}();
37
//~^ ERROR expected function, found `()`
8+
9+
let _ = async ||{}();
10+
//~^ ERROR expected function, found `()`
411
}

src/test/ui/suggestions/suggest-on-bare-closure-call.stderr

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0618]: expected function, found `()`
2-
--> $DIR/suggest-on-bare-closure-call.rs:2:15
2+
--> $DIR/suggest-on-bare-closure-call.rs:6:15
33
|
44
LL | let _ = ||{}();
55
| ^^--
@@ -11,6 +11,19 @@ help: if you meant to create this closure and immediately call it, surround the
1111
LL | let _ = (||{})();
1212
| + +
1313

14-
error: aborting due to previous error
14+
error[E0618]: expected function, found `()`
15+
--> $DIR/suggest-on-bare-closure-call.rs:9:21
16+
|
17+
LL | let _ = async ||{}();
18+
| ^^--
19+
| |
20+
| call expression requires function
21+
|
22+
help: if you meant to create this closure and immediately call it, surround the closure with parentheses
23+
|
24+
LL | let _ = (async ||{})();
25+
| + +
26+
27+
error: aborting due to 2 previous errors
1528

1629
For more information about this error, try `rustc --explain E0618`.

0 commit comments

Comments
 (0)