Skip to content

Commit 702bf00

Browse files
committed
Fix suggestion for returning async closures
1 parent 401ae55 commit 702bf00

File tree

5 files changed

+231
-0
lines changed

5 files changed

+231
-0
lines changed

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,21 @@ pub fn suggest_impl_trait<'tcx>(
12631263
infcx.tcx.lang_items().future_output(),
12641264
format_as_assoc,
12651265
),
1266+
(
1267+
infcx.tcx.lang_items().async_fn_trait(),
1268+
infcx.tcx.lang_items().async_fn_once_output(),
1269+
format_as_parenthesized,
1270+
),
1271+
(
1272+
infcx.tcx.lang_items().async_fn_mut_trait(),
1273+
infcx.tcx.lang_items().async_fn_once_output(),
1274+
format_as_parenthesized,
1275+
),
1276+
(
1277+
infcx.tcx.lang_items().async_fn_once_trait(),
1278+
infcx.tcx.lang_items().async_fn_once_output(),
1279+
format_as_parenthesized,
1280+
),
12661281
(
12671282
infcx.tcx.lang_items().fn_trait(),
12681283
infcx.tcx.lang_items().fn_once_output(),

compiler/rustc_middle/src/ty/diagnostics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ impl<'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for MakeSuggestableFolder<'tcx> {
698698
}
699699

700700
Closure(..)
701+
| CoroutineClosure(..)
701702
| FnDef(..)
702703
| Infer(..)
703704
| Coroutine(..)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#![allow(dead_code)]
2+
//@ run-rustfix
3+
//@ edition: 2021
4+
5+
// The suggestion should be `impl AsyncFn()` instead of something like `{async closure@...}`
6+
7+
fn test1() -> impl AsyncFn() {
8+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
9+
//~| HELP replace with an appropriate return type
10+
//~| SUGGESTION impl AsyncFn()
11+
async || {}
12+
}
13+
14+
fn test2() -> impl AsyncFn(i32) -> i32 {
15+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
16+
//~| HELP replace with an appropriate return type
17+
//~| SUGGESTION impl AsyncFn(i32) -> i32
18+
async |x: i32| x + 1
19+
}
20+
21+
fn test3() -> impl AsyncFn(i32, i32) -> i32 {
22+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
23+
//~| HELP replace with an appropriate return type
24+
//~| SUGGESTION impl AsyncFn(i32, i32) -> i32
25+
async |x: i32, y: i32| x + y
26+
}
27+
28+
fn test4() -> impl AsyncFn() {
29+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
30+
//~| HELP replace with an appropriate return type
31+
//~| SUGGESTION impl AsyncFn()
32+
async || -> () { () }
33+
}
34+
35+
fn test5() -> impl AsyncFn() -> i32 {
36+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
37+
//~| HELP replace with an appropriate return type
38+
//~| SUGGESTION impl AsyncFn() -> i32
39+
let z = 42;
40+
async move || z
41+
}
42+
43+
fn test6() -> impl AsyncFnMut() -> i32 {
44+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
45+
//~| HELP replace with an appropriate return type
46+
//~| SUGGESTION impl AsyncFnMut() -> i32
47+
let mut x = 0;
48+
async move || {
49+
x += 1;
50+
x
51+
}
52+
}
53+
54+
fn test7() -> impl AsyncFnOnce() {
55+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
56+
//~| HELP replace with an appropriate return type
57+
//~| SUGGESTION impl AsyncFnOnce()
58+
let s = String::from("hello");
59+
async move || {
60+
drop(s);
61+
}
62+
}
63+
64+
fn main() {}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#![allow(dead_code)]
2+
//@ run-rustfix
3+
//@ edition: 2021
4+
5+
// The suggestion should be `impl AsyncFn()` instead of something like `{async closure@...}`
6+
7+
fn test1() -> _ {
8+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
9+
//~| HELP replace with an appropriate return type
10+
//~| SUGGESTION impl AsyncFn()
11+
async || {}
12+
}
13+
14+
fn test2() -> _ {
15+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
16+
//~| HELP replace with an appropriate return type
17+
//~| SUGGESTION impl AsyncFn(i32) -> i32
18+
async |x: i32| x + 1
19+
}
20+
21+
fn test3() -> _ {
22+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
23+
//~| HELP replace with an appropriate return type
24+
//~| SUGGESTION impl AsyncFn(i32, i32) -> i32
25+
async |x: i32, y: i32| x + y
26+
}
27+
28+
fn test4() -> _ {
29+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
30+
//~| HELP replace with an appropriate return type
31+
//~| SUGGESTION impl AsyncFn()
32+
async || -> () { () }
33+
}
34+
35+
fn test5() -> _ {
36+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
37+
//~| HELP replace with an appropriate return type
38+
//~| SUGGESTION impl AsyncFn() -> i32
39+
let z = 42;
40+
async move || z
41+
}
42+
43+
fn test6() -> _ {
44+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
45+
//~| HELP replace with an appropriate return type
46+
//~| SUGGESTION impl AsyncFnMut() -> i32
47+
let mut x = 0;
48+
async move || {
49+
x += 1;
50+
x
51+
}
52+
}
53+
54+
fn test7() -> _ {
55+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
56+
//~| HELP replace with an appropriate return type
57+
//~| SUGGESTION impl AsyncFnOnce()
58+
let s = String::from("hello");
59+
async move || {
60+
drop(s);
61+
}
62+
}
63+
64+
fn main() {}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
2+
--> $DIR/suggest-impl-async-fn-issue-148493.rs:7:15
3+
|
4+
LL | fn test1() -> _ {
5+
| ^ not allowed in type signatures
6+
|
7+
help: replace with an appropriate return type
8+
|
9+
LL - fn test1() -> _ {
10+
LL + fn test1() -> impl AsyncFn() {
11+
|
12+
13+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
14+
--> $DIR/suggest-impl-async-fn-issue-148493.rs:14:15
15+
|
16+
LL | fn test2() -> _ {
17+
| ^ not allowed in type signatures
18+
|
19+
help: replace with an appropriate return type
20+
|
21+
LL - fn test2() -> _ {
22+
LL + fn test2() -> impl AsyncFn(i32) -> i32 {
23+
|
24+
25+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
26+
--> $DIR/suggest-impl-async-fn-issue-148493.rs:21:15
27+
|
28+
LL | fn test3() -> _ {
29+
| ^ not allowed in type signatures
30+
|
31+
help: replace with an appropriate return type
32+
|
33+
LL - fn test3() -> _ {
34+
LL + fn test3() -> impl AsyncFn(i32, i32) -> i32 {
35+
|
36+
37+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
38+
--> $DIR/suggest-impl-async-fn-issue-148493.rs:28:15
39+
|
40+
LL | fn test4() -> _ {
41+
| ^ not allowed in type signatures
42+
|
43+
help: replace with an appropriate return type
44+
|
45+
LL - fn test4() -> _ {
46+
LL + fn test4() -> impl AsyncFn() {
47+
|
48+
49+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
50+
--> $DIR/suggest-impl-async-fn-issue-148493.rs:35:15
51+
|
52+
LL | fn test5() -> _ {
53+
| ^ not allowed in type signatures
54+
|
55+
help: replace with an appropriate return type
56+
|
57+
LL - fn test5() -> _ {
58+
LL + fn test5() -> impl AsyncFn() -> i32 {
59+
|
60+
61+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
62+
--> $DIR/suggest-impl-async-fn-issue-148493.rs:43:15
63+
|
64+
LL | fn test6() -> _ {
65+
| ^ not allowed in type signatures
66+
|
67+
help: replace with an appropriate return type
68+
|
69+
LL - fn test6() -> _ {
70+
LL + fn test6() -> impl AsyncFnMut() -> i32 {
71+
|
72+
73+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
74+
--> $DIR/suggest-impl-async-fn-issue-148493.rs:54:15
75+
|
76+
LL | fn test7() -> _ {
77+
| ^ not allowed in type signatures
78+
|
79+
help: replace with an appropriate return type
80+
|
81+
LL - fn test7() -> _ {
82+
LL + fn test7() -> impl AsyncFnOnce() {
83+
|
84+
85+
error: aborting due to 7 previous errors
86+
87+
For more information about this error, try `rustc --explain E0121`.

0 commit comments

Comments
 (0)