Rollup merge of #98879 - compiler-errors:async-closure-wrap-parens, r=oli-obk
Fix "wrap closure in parenthesis" suggestion for `async` closure Fixes #98023
This commit is contained in:
commit
accb41ef01
3 changed files with 46 additions and 5 deletions
|
@ -280,15 +280,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
callee_node: &hir::ExprKind<'_>,
|
callee_node: &hir::ExprKind<'_>,
|
||||||
callee_span: Span,
|
callee_span: Span,
|
||||||
) {
|
) {
|
||||||
let hir_id = self.tcx.hir().get_parent_node(hir_id);
|
let hir = self.tcx.hir();
|
||||||
let parent_node = self.tcx.hir().get(hir_id);
|
let parent_hir_id = hir.get_parent_node(hir_id);
|
||||||
|
let parent_node = hir.get(parent_hir_id);
|
||||||
if let (
|
if let (
|
||||||
hir::Node::Expr(hir::Expr {
|
hir::Node::Expr(hir::Expr {
|
||||||
kind: hir::ExprKind::Closure { fn_decl_span, .. }, ..
|
kind: hir::ExprKind::Closure { fn_decl_span, body, .. },
|
||||||
|
..
|
||||||
}),
|
}),
|
||||||
hir::ExprKind::Block(..),
|
hir::ExprKind::Block(..),
|
||||||
) = (parent_node, callee_node)
|
) = (parent_node, callee_node)
|
||||||
{
|
{
|
||||||
|
let fn_decl_span = if hir.body(*body).generator_kind
|
||||||
|
== Some(hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Closure))
|
||||||
|
{
|
||||||
|
// Actually need to unwrap a few more layers of HIR to get to
|
||||||
|
// the _real_ closure...
|
||||||
|
let async_closure = hir.get_parent_node(hir.get_parent_node(parent_hir_id));
|
||||||
|
if let hir::Node::Expr(hir::Expr {
|
||||||
|
kind: hir::ExprKind::Closure { fn_decl_span, .. },
|
||||||
|
..
|
||||||
|
}) = hir.get(async_closure)
|
||||||
|
{
|
||||||
|
*fn_decl_span
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
*fn_decl_span
|
||||||
|
};
|
||||||
|
|
||||||
let start = fn_decl_span.shrink_to_lo();
|
let start = fn_decl_span.shrink_to_lo();
|
||||||
let end = callee_span.shrink_to_hi();
|
let end = callee_span.shrink_to_hi();
|
||||||
err.multipart_suggestion(
|
err.multipart_suggestion(
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
|
// edition:2021
|
||||||
|
|
||||||
|
#![feature(async_closure)]
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let _ = ||{}();
|
let _ = ||{}();
|
||||||
//~^ ERROR expected function, found `()`
|
//~^ ERROR expected function, found `()`
|
||||||
|
|
||||||
|
let _ = async ||{}();
|
||||||
|
//~^ ERROR expected function, found `()`
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
error[E0618]: expected function, found `()`
|
error[E0618]: expected function, found `()`
|
||||||
--> $DIR/suggest-on-bare-closure-call.rs:2:15
|
--> $DIR/suggest-on-bare-closure-call.rs:6:15
|
||||||
|
|
|
|
||||||
LL | let _ = ||{}();
|
LL | let _ = ||{}();
|
||||||
| ^^--
|
| ^^--
|
||||||
|
@ -11,6 +11,19 @@ help: if you meant to create this closure and immediately call it, surround the
|
||||||
LL | let _ = (||{})();
|
LL | let _ = (||{})();
|
||||||
| + +
|
| + +
|
||||||
|
|
||||||
error: aborting due to previous error
|
error[E0618]: expected function, found `()`
|
||||||
|
--> $DIR/suggest-on-bare-closure-call.rs:9:21
|
||||||
|
|
|
||||||
|
LL | let _ = async ||{}();
|
||||||
|
| ^^--
|
||||||
|
| |
|
||||||
|
| call expression requires function
|
||||||
|
|
|
||||||
|
help: if you meant to create this closure and immediately call it, surround the closure with parentheses
|
||||||
|
|
|
||||||
|
LL | let _ = (async ||{})();
|
||||||
|
| + +
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0618`.
|
For more information about this error, try `rustc --explain E0618`.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue