Skip to content

Commit dbb9bbb

Browse files
committed
Fix wrong suggestion for returning async closure
1 parent f57eac1 commit dbb9bbb

File tree

6 files changed

+138
-19
lines changed

6 files changed

+138
-19
lines changed

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

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

838-
if matches!(obligation.cause.code(), ObligationCauseCode::FunctionArg { .. })
838+
if let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code()
839839
&& obligation.cause.span.can_be_used_for_suggestions()
840840
{
841-
let (span, sugg) = if let Some(snippet) =
842-
self.tcx.sess.source_map().span_to_snippet(obligation.cause.span).ok()
843-
&& snippet.starts_with("|")
844-
{
845-
(obligation.cause.span, format!("({snippet})({args})"))
846-
} else {
847-
(obligation.cause.span.shrink_to_hi(), format!("({args})"))
841+
let span = obligation.cause.span;
842+
843+
let arg_expr = match self.tcx.hir_node(*arg_hir_id) {
844+
hir::Node::Expr(expr) => Some(expr),
845+
_ => None,
848846
};
849847

850-
// When the obligation error has been ensured to have been caused by
851-
// an argument, the `obligation.cause.span` points at the expression
852-
// of the argument, so we can provide a suggestion. Otherwise, we give
853-
// a more general note.
854-
err.span_suggestion_verbose(span, msg, sugg, Applicability::HasPlaceholders);
848+
let is_closure_expr =
849+
arg_expr.is_some_and(|expr| matches!(expr.kind, hir::ExprKind::Closure(..)));
850+
851+
// If the user wrote `|| {}()`, suggesting to call the closure would produce `(|| {}())()`,
852+
// which doesn't help and is often outright wrong.
853+
if args.is_empty()
854+
&& let Some(expr) = arg_expr
855+
&& let hir::ExprKind::Closure(closure) = expr.kind
856+
{
857+
let mut body = self.tcx.hir_body(closure.body).value;
858+
859+
// Async closures desugar to a closure returning a coroutine
860+
if let hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async) =
861+
closure.kind
862+
{
863+
let peeled = body.peel_blocks().peel_drop_temps();
864+
if let hir::ExprKind::Closure(inner) = peeled.kind {
865+
body = self.tcx.hir_body(inner.body).value;
866+
}
867+
}
868+
869+
let peeled_body = body.peel_blocks().peel_drop_temps();
870+
if let hir::ExprKind::Call(callee, call_args) = peeled_body.kind
871+
&& call_args.is_empty()
872+
&& let hir::ExprKind::Block(..) = callee.peel_blocks().peel_drop_temps().kind
873+
{
874+
return false;
875+
}
876+
}
877+
878+
if is_closure_expr {
879+
err.multipart_suggestion_verbose(
880+
msg,
881+
vec![
882+
(span.shrink_to_lo(), "(".to_string()),
883+
(span.shrink_to_hi(), format!(")({args})")),
884+
],
885+
Applicability::HasPlaceholders,
886+
);
887+
} else {
888+
err.span_suggestion_verbose(
889+
span.shrink_to_hi(),
890+
msg,
891+
format!("({args})"),
892+
Applicability::HasPlaceholders,
893+
);
894+
}
855895
} else if let DefIdOrName::DefId(def_id) = def_id_or_name {
856896
let name = match self.tcx.hir_get_if_local(def_id) {
857897
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)