Skip to content

Commit f4e35c6

Browse files
Fix async closure call suggestion
1 parent f1ee076 commit f4e35c6

File tree

2 files changed

+51
-36
lines changed

2 files changed

+51
-36
lines changed

compiler/rustc_hir_typeck/src/callee.rs

+46-36
Original file line numberDiff line numberDiff line change
@@ -293,49 +293,59 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
293293
callee_node: &hir::ExprKind<'_>,
294294
callee_span: Span,
295295
) {
296+
let hir::ExprKind::Block(..) = callee_node else {
297+
// Only calls on blocks suggested here.
298+
return;
299+
};
300+
296301
let hir = self.tcx.hir();
297-
let parent_hir_id = hir.parent_id(hir_id);
298-
let parent_node = self.tcx.hir_node(parent_hir_id);
299-
if let (
300-
hir::Node::Expr(hir::Expr {
301-
kind: hir::ExprKind::Closure(&hir::Closure { fn_decl_span, kind, .. }),
302+
let fn_decl_span = if let hir::Node::Expr(hir::Expr {
303+
kind: hir::ExprKind::Closure(&hir::Closure { fn_decl_span, .. }),
304+
..
305+
}) = hir.get_parent(hir_id)
306+
{
307+
fn_decl_span
308+
} else if let Some((
309+
_,
310+
hir::Node::Expr(&hir::Expr {
311+
hir_id: parent_hir_id,
312+
kind:
313+
hir::ExprKind::Closure(&hir::Closure {
314+
kind:
315+
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
316+
hir::CoroutineDesugaring::Async,
317+
hir::CoroutineSource::Closure,
318+
)),
319+
..
320+
}),
302321
..
303322
}),
304-
hir::ExprKind::Block(..),
305-
) = (parent_node, callee_node)
323+
)) = hir.parent_iter(hir_id).nth(3)
306324
{
307-
let fn_decl_span = if matches!(
308-
kind,
309-
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
310-
hir::CoroutineDesugaring::Async,
311-
hir::CoroutineSource::Closure
312-
),)
313-
) {
314-
// Actually need to unwrap one more layer of HIR to get to
315-
// the _real_ closure...
316-
let async_closure = hir.parent_id(parent_hir_id);
317-
if let hir::Node::Expr(hir::Expr {
318-
kind: hir::ExprKind::Closure(&hir::Closure { fn_decl_span, .. }),
319-
..
320-
}) = self.tcx.hir_node(async_closure)
321-
{
322-
fn_decl_span
323-
} else {
324-
return;
325-
}
326-
} else {
325+
// Actually need to unwrap one more layer of HIR to get to
326+
// the _real_ closure...
327+
let async_closure = hir.parent_id(parent_hir_id);
328+
if let hir::Node::Expr(hir::Expr {
329+
kind: hir::ExprKind::Closure(&hir::Closure { fn_decl_span, .. }),
330+
..
331+
}) = self.tcx.hir_node(async_closure)
332+
{
327333
fn_decl_span
328-
};
334+
} else {
335+
return;
336+
}
337+
} else {
338+
return;
339+
};
329340

330-
let start = fn_decl_span.shrink_to_lo();
331-
let end = callee_span.shrink_to_hi();
332-
err.multipart_suggestion(
333-
"if you meant to create this closure and immediately call it, surround the \
341+
let start = fn_decl_span.shrink_to_lo();
342+
let end = callee_span.shrink_to_hi();
343+
err.multipart_suggestion(
344+
"if you meant to create this closure and immediately call it, surround the \
334345
closure with parentheses",
335-
vec![(start, "(".to_string()), (end, ")".to_string())],
336-
Applicability::MaybeIncorrect,
337-
);
338-
}
346+
vec![(start, "(".to_string()), (end, ")".to_string())],
347+
Applicability::MaybeIncorrect,
348+
);
339349
}
340350

341351
/// Give appropriate suggestion when encountering `[("a", 0) ("b", 1)]`, where the

tests/ui/suggestions/suggest-on-bare-closure-call.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ LL | let _ = async ||{}();
1818
| ^^--
1919
| |
2020
| 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+
| + +
2126

2227
error: aborting due to 2 previous errors
2328

0 commit comments

Comments
 (0)