Skip to content

Commit cea42a6

Browse files
Rollup merge of rust-lang#151194 - chenyukang:yukang-fix-150701-async-closure, r=wesleywiser
Fix wrong suggestion for returning async closure Fixes rust-lang#150701 r? @estebank
2 parents c9cc85e + dbb9bbb commit cea42a6

6 files changed

Lines changed: 138 additions & 19 deletions

File tree

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -864,23 +864,63 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
864864
.collect::<Vec<_>>()
865865
.join(", ");
866866

867-
if matches!(obligation.cause.code(), ObligationCauseCode::FunctionArg { .. })
867+
if let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code()
868868
&& obligation.cause.span.can_be_used_for_suggestions()
869869
{
870-
let (span, sugg) = if let Some(snippet) =
871-
self.tcx.sess.source_map().span_to_snippet(obligation.cause.span).ok()
872-
&& snippet.starts_with("|")
873-
{
874-
(obligation.cause.span, format!("({snippet})({args})"))
875-
} else {
876-
(obligation.cause.span.shrink_to_hi(), format!("({args})"))
870+
let span = obligation.cause.span;
871+
872+
let arg_expr = match self.tcx.hir_node(*arg_hir_id) {
873+
hir::Node::Expr(expr) => Some(expr),
874+
_ => None,
877875
};
878876

879-
// When the obligation error has been ensured to have been caused by
880-
// an argument, the `obligation.cause.span` points at the expression
881-
// of the argument, so we can provide a suggestion. Otherwise, we give
882-
// a more general note.
883-
err.span_suggestion_verbose(span, msg, sugg, Applicability::HasPlaceholders);
877+
let is_closure_expr =
878+
arg_expr.is_some_and(|expr| matches!(expr.kind, hir::ExprKind::Closure(..)));
879+
880+
// If the user wrote `|| {}()`, suggesting to call the closure would produce `(|| {}())()`,
881+
// which doesn't help and is often outright wrong.
882+
if args.is_empty()
883+
&& let Some(expr) = arg_expr
884+
&& let hir::ExprKind::Closure(closure) = expr.kind
885+
{
886+
let mut body = self.tcx.hir_body(closure.body).value;
887+
888+
// Async closures desugar to a closure returning a coroutine
889+
if let hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async) =
890+
closure.kind
891+
{
892+
let peeled = body.peel_blocks().peel_drop_temps();
893+
if let hir::ExprKind::Closure(inner) = peeled.kind {
894+
body = self.tcx.hir_body(inner.body).value;
895+
}
896+
}
897+
898+
let peeled_body = body.peel_blocks().peel_drop_temps();
899+
if let hir::ExprKind::Call(callee, call_args) = peeled_body.kind
900+
&& call_args.is_empty()
901+
&& let hir::ExprKind::Block(..) = callee.peel_blocks().peel_drop_temps().kind
902+
{
903+
return false;
904+
}
905+
}
906+
907+
if is_closure_expr {
908+
err.multipart_suggestion_verbose(
909+
msg,
910+
vec![
911+
(span.shrink_to_lo(), "(".to_string()),
912+
(span.shrink_to_hi(), format!(")({args})")),
913+
],
914+
Applicability::HasPlaceholders,
915+
);
916+
} else {
917+
err.span_suggestion_verbose(
918+
span.shrink_to_hi(),
919+
msg,
920+
format!("({args})"),
921+
Applicability::HasPlaceholders,
922+
);
923+
}
884924
} else if let DefIdOrName::DefId(def_id) = def_id_or_name {
885925
let name = match self.tcx.hir_get_if_local(def_id) {
886926
Some(hir::Node::Expr(hir::Expr {

tests/ui/const-generics/adt_const_params/const_param_ty_bad.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ LL | fn check(_: impl std::marker::ConstParamTy_) {}
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `check`
3333
help: use parentheses to call this closure
3434
|
35-
LL - check(|| {});
36-
LL + check((|| {})());
37-
|
35+
LL | check((|| {})());
36+
| + +++
3837

3938
error[E0277]: `fn()` can't be used as a const parameter type
4039
--> $DIR/const_param_ty_bad.rs:9:11
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Regression test for #150701
2+
3+
//@ run-rustfix
4+
//@ edition: 2024
5+
6+
use std::future::Future;
7+
8+
fn f(_c: impl Future<Output = ()>) {}
9+
10+
fn main() {
11+
f((async || {})()); //~ ERROR: expected function, found `()`
12+
//~^ ERROR: is not a future
13+
f((async || {})());
14+
//~^ ERROR: is not a future
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Regression test for #150701
2+
3+
//@ run-rustfix
4+
//@ edition: 2024
5+
6+
use std::future::Future;
7+
8+
fn f(_c: impl Future<Output = ()>) {}
9+
10+
fn main() {
11+
f(async || {}()); //~ ERROR: expected function, found `()`
12+
//~^ ERROR: is not a future
13+
f(async || {});
14+
//~^ ERROR: is not a future
15+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
error[E0618]: expected function, found `()`
2+
--> $DIR/suggest-create-closure-issue-150701.rs:11:16
3+
|
4+
LL | f(async || {}());
5+
| ^^--
6+
| |
7+
| call expression requires function
8+
|
9+
help: if you meant to create this closure and immediately call it, surround the closure with parentheses
10+
|
11+
LL | f((async || {})());
12+
| + +
13+
14+
error[E0277]: `{async closure@$DIR/suggest-create-closure-issue-150701.rs:11:7: 11:15}` is not a future
15+
--> $DIR/suggest-create-closure-issue-150701.rs:11:7
16+
|
17+
LL | f(async || {}());
18+
| - ^^^^^^^^^^^^^ `{async closure@$DIR/suggest-create-closure-issue-150701.rs:11:7: 11:15}` is not a future
19+
| |
20+
| required by a bound introduced by this call
21+
|
22+
= help: the trait `Future` is not implemented for `{async closure@$DIR/suggest-create-closure-issue-150701.rs:11:7: 11:15}`
23+
note: required by a bound in `f`
24+
--> $DIR/suggest-create-closure-issue-150701.rs:8:15
25+
|
26+
LL | fn f(_c: impl Future<Output = ()>) {}
27+
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `f`
28+
29+
error[E0277]: `{async closure@$DIR/suggest-create-closure-issue-150701.rs:13:7: 13:15}` is not a future
30+
--> $DIR/suggest-create-closure-issue-150701.rs:13:7
31+
|
32+
LL | f(async || {});
33+
| - ^^^^^^^^^^^ `{async closure@$DIR/suggest-create-closure-issue-150701.rs:13:7: 13:15}` is not a future
34+
| |
35+
| required by a bound introduced by this call
36+
|
37+
= help: the trait `Future` is not implemented for `{async closure@$DIR/suggest-create-closure-issue-150701.rs:13:7: 13:15}`
38+
note: required by a bound in `f`
39+
--> $DIR/suggest-create-closure-issue-150701.rs:8:15
40+
|
41+
LL | fn f(_c: impl Future<Output = ()>) {}
42+
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `f`
43+
help: use parentheses to call this closure
44+
|
45+
LL | f((async || {})());
46+
| + +++
47+
48+
error: aborting due to 3 previous errors
49+
50+
Some errors have detailed explanations: E0277, E0618.
51+
For more information about an error, try `rustc --explain E0277`.

tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ LL | fn call(&self, _: impl Display) {}
1414
| ^^^^^^^ required by this bound in `S::call`
1515
help: use parentheses to call this closure
1616
|
17-
LL - S.call(|| "hello");
18-
LL + S.call((|| "hello")());
19-
|
17+
LL | S.call((|| "hello")());
18+
| + +++
2019

2120
error: aborting due to 1 previous error
2221

0 commit comments

Comments
 (0)