Skip to content

Commit 12ac312

Browse files
authored
Rollup merge of #80284 - ThePuzzlemaker:issue-80179-fix, r=varkor
Suggest fn ptr rather than fn item and suggest to use `Fn` trait bounds rather than the unique closure type in E0121 Previously, using `_` as a return type in a function that returned a function/closure would provide a diagnostic that would cause a papercut. For example: ```rust fn f() -> i32 { 0 } fn fn_ptr() -> _ { f } fn closure() -> _ { || 0 } ``` would result in this diagnostic: ```rust error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> <anon>:2:16 | 2 | fn fn_ptr() -> _ { f } | ^ | | | not allowed in type signatures | help: replace with the correct return type: `fn() -> i32 {f}` error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> <anon>:3:17 | 3 | fn closure() -> _ { || 0 } | ^ | | | not allowed in type signatures | help: replace with the correct return type: `[closure@<anon>:3:21: 3:25]` error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0121`. ``` As can be seen, it was suggested to use the function definition return type `fn() -> i32 { f }` which is not valid syntax as a return type. Additionally, closures cause a papercut as unique closure types (notated in this case as `[closure@<anon>:3:21: 3:25]`) are not valid syntax either. Instead, this PR implements this version of the diagnostic (this example is for the same code featured above): ```rust error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> <anon>:2:16 | 2 | fn fn_ptr() -> _ { f } | ^ | | | not allowed in type signatures | help: replace with the correct return type: `fn() -> i32` error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> <anon>:3:17 | 3 | fn closure() -> _ { || 0 } | ^ not allowed in type signatures | = help: consider using an `Fn`, `FnMut`, or `FnOnce` trait bound = note: for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0121`. ``` As can be seen in this diagnostic, the papercut for returning a function item is fixed by suggesting the usage of a function pointer as the return type. As for closures, it's suggested to use an `Fn`, `FnMut`, or `FnOnce` trait bound (with further reading on closures and `Fn` traits in *The Book* for beginners). I did not implement a suggestion to use `impl Fn() -> i32` syntax as that was out-of-scope for my abilities at the moment, therefore someone in the future may want to implement that. Also, it's possible to use either `impl Trait` syntax, generics, or generics with a `where` clause, and some users may not want to use `impl Trait` syntax for their own reasons. This PR fixes #80179.
2 parents d3c43ac + 5e6dc92 commit 12ac312

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

compiler/rustc_typeck/src/collect.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -1544,12 +1544,27 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
15441544
let mut diag = bad_placeholder_type(tcx, visitor.0);
15451545
let ret_ty = fn_sig.output();
15461546
if ret_ty != tcx.ty_error() {
1547-
diag.span_suggestion(
1548-
ty.span,
1549-
"replace with the correct return type",
1550-
ret_ty.to_string(),
1551-
Applicability::MaybeIncorrect,
1552-
);
1547+
if !ret_ty.is_closure() {
1548+
let ret_ty_str = match ret_ty.kind() {
1549+
// Suggest a function pointer return type instead of a unique function definition
1550+
// (e.g. `fn() -> i32` instead of `fn() -> i32 { f }`, the latter of which is invalid
1551+
// syntax)
1552+
ty::FnDef(..) => ret_ty.fn_sig(tcx).to_string(),
1553+
_ => ret_ty.to_string(),
1554+
};
1555+
diag.span_suggestion(
1556+
ty.span,
1557+
"replace with the correct return type",
1558+
ret_ty_str,
1559+
Applicability::MaybeIncorrect,
1560+
);
1561+
} else {
1562+
// We're dealing with a closure, so we should suggest using `impl Fn` or trait bounds
1563+
// to prevent the user from getting a papercut while trying to use the unique closure
1564+
// syntax (e.g. `[closure@src/lib.rs:2:5: 2:9]`).
1565+
diag.help("consider using an `Fn`, `FnMut`, or `FnOnce` trait bound");
1566+
diag.note("for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html");
1567+
}
15531568
}
15541569
diag.emit();
15551570
ty::Binder::bind(fn_sig)

src/test/ui/fn/issue-80179.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Functions with a type placeholder `_` as the return type should
2+
// show a function pointer suggestion when given a function item
3+
// and suggest how to return closures correctly from a function.
4+
// This is a regression test of #80179
5+
6+
fn returns_i32() -> i32 {
7+
0
8+
}
9+
10+
fn returns_fn_ptr() -> _ {
11+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures [E0121]
12+
//~| NOTE not allowed in type signatures
13+
//~| HELP replace with the correct return type
14+
//~| SUGGESTION fn() -> i32
15+
returns_i32
16+
}
17+
18+
fn returns_closure() -> _ {
19+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures [E0121]
20+
//~| NOTE not allowed in type signatures
21+
//~| HELP consider using an `Fn`, `FnMut`, or `FnOnce` trait bound
22+
//~| NOTE for more information on `Fn` traits and closure types, see
23+
// https://doc.rust-lang.org/book/ch13-01-closures.html
24+
|| 0
25+
}
26+
27+
fn main() {}

src/test/ui/fn/issue-80179.stderr

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
2+
--> $DIR/issue-80179.rs:10:24
3+
|
4+
LL | fn returns_fn_ptr() -> _ {
5+
| ^
6+
| |
7+
| not allowed in type signatures
8+
| help: replace with the correct return type: `fn() -> i32`
9+
10+
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
11+
--> $DIR/issue-80179.rs:18:25
12+
|
13+
LL | fn returns_closure() -> _ {
14+
| ^ not allowed in type signatures
15+
|
16+
= help: consider using an `Fn`, `FnMut`, or `FnOnce` trait bound
17+
= note: for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0121`.

0 commit comments

Comments
 (0)