Skip to content

Commit ffde96c

Browse files
authored
Rollup merge of rust-lang#53296 - estebank:suggest-closure, r=KodrAus
When closure with no arguments was expected, suggest wrapping Fix rust-lang#49694.
2 parents fa3d56a + cea73d6 commit ffde96c

14 files changed

+90
-34
lines changed

src/libcore/ops/function.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
#[lang = "fn"]
6767
#[stable(feature = "rust1", since = "1.0.0")]
6868
#[rustc_paren_sugar]
69+
#[rustc_on_unimplemented(
70+
on(Args="()", note="wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}"),
71+
message="expected a `{Fn}<{Args}>` closure, found `{Self}`",
72+
label="expected an `Fn<{Args}>` closure, found `{Self}`",
73+
)]
6974
#[fundamental] // so that regex can rely that `&str: !FnMut`
7075
pub trait Fn<Args> : FnMut<Args> {
7176
/// Performs the call operation.
@@ -139,6 +144,11 @@ pub trait Fn<Args> : FnMut<Args> {
139144
#[lang = "fn_mut"]
140145
#[stable(feature = "rust1", since = "1.0.0")]
141146
#[rustc_paren_sugar]
147+
#[rustc_on_unimplemented(
148+
on(Args="()", note="wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}"),
149+
message="expected a `{FnMut}<{Args}>` closure, found `{Self}`",
150+
label="expected an `FnMut<{Args}>` closure, found `{Self}`",
151+
)]
142152
#[fundamental] // so that regex can rely that `&str: !FnMut`
143153
pub trait FnMut<Args> : FnOnce<Args> {
144154
/// Performs the call operation.
@@ -212,6 +222,11 @@ pub trait FnMut<Args> : FnOnce<Args> {
212222
#[lang = "fn_once"]
213223
#[stable(feature = "rust1", since = "1.0.0")]
214224
#[rustc_paren_sugar]
225+
#[rustc_on_unimplemented(
226+
on(Args="()", note="wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}"),
227+
message="expected a `{FnOnce}<{Args}>` closure, found `{Self}`",
228+
label="expected an `FnOnce<{Args}>` closure, found `{Self}`",
229+
)]
215230
#[fundamental] // so that regex can rely that `&str: !FnMut`
216231
pub trait FnOnce<Args> {
217232
/// The returned type after the call operator is used.

src/test/ui/closure-expected.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let x = Some(1);
13+
let y = x.or_else(4);
14+
//~^ ERROR expected a `std::ops::FnOnce<()>` closure, found `{integer}`
15+
}

src/test/ui/closure-expected.stderr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0277]: expected a `std::ops::FnOnce<()>` closure, found `{integer}`
2+
--> $DIR/closure-expected.rs:13:15
3+
|
4+
LL | let y = x.or_else(4);
5+
| ^^^^^^^ expected an `FnOnce<()>` closure, found `{integer}`
6+
|
7+
= help: the trait `std::ops::FnOnce<()>` is not implemented for `{integer}`
8+
= note: wrap the `{integer}` in a closure with no arguments: `|| { /* code */ }
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0277`.

src/test/ui/extern/extern-wrong-value-type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ fn main() {
1717
// extern functions are extern "C" fn
1818
let _x: extern "C" fn() = f; // OK
1919
is_fn(f);
20-
//~^ ERROR `extern "C" fn() {f}: std::ops::Fn<()>` is not satisfied
20+
//~^ ERROR expected a `std::ops::Fn<()>` closure, found `extern "C" fn() {f}`
2121
}

src/test/ui/extern/extern-wrong-value-type.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
error[E0277]: the trait bound `extern "C" fn() {f}: std::ops::Fn<()>` is not satisfied
1+
error[E0277]: expected a `std::ops::Fn<()>` closure, found `extern "C" fn() {f}`
22
--> $DIR/extern-wrong-value-type.rs:19:5
33
|
44
LL | is_fn(f);
5-
| ^^^^^ the trait `std::ops::Fn<()>` is not implemented for `extern "C" fn() {f}`
5+
| ^^^^^ expected an `Fn<()>` closure, found `extern "C" fn() {f}`
66
|
7+
= help: the trait `std::ops::Fn<()>` is not implemented for `extern "C" fn() {f}`
8+
= note: wrap the `extern "C" fn() {f}` in a closure with no arguments: `|| { /* code */ }
79
note: required by `is_fn`
810
--> $DIR/extern-wrong-value-type.rs:14:1
911
|

src/test/ui/fn/fn-trait-formatting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ fn main() {
2727
//~| found type `std::boxed::Box<dyn std::ops::FnMut() -> isize>`
2828

2929
needs_fn(1);
30-
//~^ ERROR : std::ops::Fn<(isize,)>`
30+
//~^ ERROR expected a `std::ops::Fn<(isize,)>` closure, found `{integer}`
3131
}

src/test/ui/fn/fn-trait-formatting.stderr

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ LL | let _: () = (box || -> isize { unimplemented!() }) as Box<FnMut() -> is
2525
= note: expected type `()`
2626
found type `std::boxed::Box<dyn std::ops::FnMut() -> isize>`
2727

28-
error[E0277]: the trait bound `{integer}: std::ops::Fn<(isize,)>` is not satisfied
28+
error[E0277]: expected a `std::ops::Fn<(isize,)>` closure, found `{integer}`
2929
--> $DIR/fn-trait-formatting.rs:29:5
3030
|
3131
LL | needs_fn(1);
32-
| ^^^^^^^^ the trait `std::ops::Fn<(isize,)>` is not implemented for `{integer}`
32+
| ^^^^^^^^ expected an `Fn<(isize,)>` closure, found `{integer}`
3333
|
34-
= help: the following implementations were found:
35-
<&'a F as std::ops::Fn<A>>
36-
<core::str::LinesAnyMap as std::ops::Fn<(&'a str,)>>
34+
= help: the trait `std::ops::Fn<(isize,)>` is not implemented for `{integer}`
3735
note: required by `needs_fn`
3836
--> $DIR/fn-trait-formatting.rs:13:1
3937
|

src/test/ui/issues/issue-22034.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ fn main() {
1616
let ptr: *mut () = 0 as *mut _;
1717
let _: &mut Fn() = unsafe {
1818
&mut *(ptr as *mut Fn())
19-
//~^ ERROR `(): std::ops::Fn<()>` is not satisfied
19+
//~^ ERROR expected a `std::ops::Fn<()>` closure, found `()`
2020
};
2121
}

src/test/ui/issues/issue-22034.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
error[E0277]: the trait bound `(): std::ops::Fn<()>` is not satisfied
1+
error[E0277]: expected a `std::ops::Fn<()>` closure, found `()`
22
--> $DIR/issue-22034.rs:18:16
33
|
44
LL | &mut *(ptr as *mut Fn())
5-
| ^^^ the trait `std::ops::Fn<()>` is not implemented for `()`
5+
| ^^^ expected an `Fn<()>` closure, found `()`
66
|
7+
= help: the trait `std::ops::Fn<()>` is not implemented for `()`
8+
= note: wrap the `()` in a closure with no arguments: `|| { /* code */ }
79
= note: required for the cast to the object type `dyn std::ops::Fn()`
810

911
error: aborting due to previous error

src/test/ui/issues/issue-23966.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error[E0277]: the trait bound `(): std::ops::FnMut<(_, char)>` is not satisfied
1+
error[E0277]: expected a `std::ops::FnMut<(_, char)>` closure, found `()`
22
--> $DIR/issue-23966.rs:12:16
33
|
44
LL | "".chars().fold(|_, _| (), ());
5-
| ^^^^ the trait `std::ops::FnMut<(_, char)>` is not implemented for `()`
5+
| ^^^^ expected an `FnMut<(_, char)>` closure, found `()`
6+
|
7+
= help: the trait `std::ops::FnMut<(_, char)>` is not implemented for `()`
68

79
error: aborting due to previous error
810

src/test/ui/unboxed-closures/unboxed-closures-fnmut-as-fn.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
error[E0277]: the trait bound `S: std::ops::Fn<(isize,)>` is not satisfied
1+
error[E0277]: expected a `std::ops::Fn<(isize,)>` closure, found `S`
22
--> $DIR/unboxed-closures-fnmut-as-fn.rs:38:13
33
|
44
LL | let x = call_it(&S, 22);
5-
| ^^^^^^^ the trait `std::ops::Fn<(isize,)>` is not implemented for `S`
5+
| ^^^^^^^ expected an `Fn<(isize,)>` closure, found `S`
66
|
7+
= help: the trait `std::ops::Fn<(isize,)>` is not implemented for `S`
78
note: required by `call_it`
89
--> $DIR/unboxed-closures-fnmut-as-fn.rs:33:1
910
|

src/test/ui/unboxed-closures/unboxed-closures-unsafe-extern-fn.stderr

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1-
error[E0277]: the trait bound `for<'r> for<'s> unsafe fn(&'s isize) -> isize {square}: std::ops::Fn<(&'r isize,)>` is not satisfied
1+
error[E0277]: expected a `std::ops::Fn<(&isize,)>` closure, found `for<'r> unsafe fn(&'r isize) -> isize {square}`
22
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:22:13
33
|
44
LL | let x = call_it(&square, 22);
5-
| ^^^^^^^ the trait `for<'r> std::ops::Fn<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}`
5+
| ^^^^^^^ expected an `Fn<(&isize,)>` closure, found `for<'r> unsafe fn(&'r isize) -> isize {square}`
66
|
7+
= help: the trait `for<'r> std::ops::Fn<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}`
78
note: required by `call_it`
89
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:17:1
910
|
1011
LL | fn call_it<F:Fn(&isize)->isize>(_: &F, _: isize) -> isize { 0 }
1112
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1213

13-
error[E0277]: the trait bound `for<'r> for<'s> unsafe fn(&'s isize) -> isize {square}: std::ops::FnMut<(&'r isize,)>` is not satisfied
14+
error[E0277]: expected a `std::ops::FnMut<(&isize,)>` closure, found `for<'r> unsafe fn(&'r isize) -> isize {square}`
1415
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:27:13
1516
|
1617
LL | let y = call_it_mut(&mut square, 22);
17-
| ^^^^^^^^^^^ the trait `for<'r> std::ops::FnMut<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}`
18+
| ^^^^^^^^^^^ expected an `FnMut<(&isize,)>` closure, found `for<'r> unsafe fn(&'r isize) -> isize {square}`
1819
|
20+
= help: the trait `for<'r> std::ops::FnMut<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}`
1921
note: required by `call_it_mut`
2022
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:18:1
2123
|
2224
LL | fn call_it_mut<F:FnMut(&isize)->isize>(_: &mut F, _: isize) -> isize { 0 }
2325
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2426

25-
error[E0277]: the trait bound `for<'r> for<'s> unsafe fn(&'s isize) -> isize {square}: std::ops::FnOnce<(&'r isize,)>` is not satisfied
27+
error[E0277]: expected a `std::ops::FnOnce<(&isize,)>` closure, found `for<'r> unsafe fn(&'r isize) -> isize {square}`
2628
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:32:13
2729
|
2830
LL | let z = call_it_once(square, 22);
29-
| ^^^^^^^^^^^^ the trait `for<'r> std::ops::FnOnce<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}`
31+
| ^^^^^^^^^^^^ expected an `FnOnce<(&isize,)>` closure, found `for<'r> unsafe fn(&'r isize) -> isize {square}`
3032
|
33+
= help: the trait `for<'r> std::ops::FnOnce<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}`
3134
note: required by `call_it_once`
3235
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:19:1
3336
|

src/test/ui/unboxed-closures/unboxed-closures-wrong-abi.stderr

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1-
error[E0277]: the trait bound `for<'r> for<'s> extern "C" fn(&'s isize) -> isize {square}: std::ops::Fn<(&'r isize,)>` is not satisfied
1+
error[E0277]: expected a `std::ops::Fn<(&isize,)>` closure, found `for<'r> extern "C" fn(&'r isize) -> isize {square}`
22
--> $DIR/unboxed-closures-wrong-abi.rs:22:13
33
|
44
LL | let x = call_it(&square, 22);
5-
| ^^^^^^^ the trait `for<'r> std::ops::Fn<(&'r isize,)>` is not implemented for `for<'r> extern "C" fn(&'r isize) -> isize {square}`
5+
| ^^^^^^^ expected an `Fn<(&isize,)>` closure, found `for<'r> extern "C" fn(&'r isize) -> isize {square}`
66
|
7+
= help: the trait `for<'r> std::ops::Fn<(&'r isize,)>` is not implemented for `for<'r> extern "C" fn(&'r isize) -> isize {square}`
78
note: required by `call_it`
89
--> $DIR/unboxed-closures-wrong-abi.rs:17:1
910
|
1011
LL | fn call_it<F:Fn(&isize)->isize>(_: &F, _: isize) -> isize { 0 }
1112
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1213

13-
error[E0277]: the trait bound `for<'r> for<'s> extern "C" fn(&'s isize) -> isize {square}: std::ops::FnMut<(&'r isize,)>` is not satisfied
14+
error[E0277]: expected a `std::ops::FnMut<(&isize,)>` closure, found `for<'r> extern "C" fn(&'r isize) -> isize {square}`
1415
--> $DIR/unboxed-closures-wrong-abi.rs:27:13
1516
|
1617
LL | let y = call_it_mut(&mut square, 22);
17-
| ^^^^^^^^^^^ the trait `for<'r> std::ops::FnMut<(&'r isize,)>` is not implemented for `for<'r> extern "C" fn(&'r isize) -> isize {square}`
18+
| ^^^^^^^^^^^ expected an `FnMut<(&isize,)>` closure, found `for<'r> extern "C" fn(&'r isize) -> isize {square}`
1819
|
20+
= help: the trait `for<'r> std::ops::FnMut<(&'r isize,)>` is not implemented for `for<'r> extern "C" fn(&'r isize) -> isize {square}`
1921
note: required by `call_it_mut`
2022
--> $DIR/unboxed-closures-wrong-abi.rs:18:1
2123
|
2224
LL | fn call_it_mut<F:FnMut(&isize)->isize>(_: &mut F, _: isize) -> isize { 0 }
2325
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2426

25-
error[E0277]: the trait bound `for<'r> for<'s> extern "C" fn(&'s isize) -> isize {square}: std::ops::FnOnce<(&'r isize,)>` is not satisfied
27+
error[E0277]: expected a `std::ops::FnOnce<(&isize,)>` closure, found `for<'r> extern "C" fn(&'r isize) -> isize {square}`
2628
--> $DIR/unboxed-closures-wrong-abi.rs:32:13
2729
|
2830
LL | let z = call_it_once(square, 22);
29-
| ^^^^^^^^^^^^ the trait `for<'r> std::ops::FnOnce<(&'r isize,)>` is not implemented for `for<'r> extern "C" fn(&'r isize) -> isize {square}`
31+
| ^^^^^^^^^^^^ expected an `FnOnce<(&isize,)>` closure, found `for<'r> extern "C" fn(&'r isize) -> isize {square}`
3032
|
33+
= help: the trait `for<'r> std::ops::FnOnce<(&'r isize,)>` is not implemented for `for<'r> extern "C" fn(&'r isize) -> isize {square}`
3134
note: required by `call_it_once`
3235
--> $DIR/unboxed-closures-wrong-abi.rs:19:1
3336
|

src/test/ui/unboxed-closures/unboxed-closures-wrong-arg-type-extern-fn.stderr

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1-
error[E0277]: the trait bound `for<'r> unsafe fn(isize) -> isize {square}: std::ops::Fn<(&'r isize,)>` is not satisfied
1+
error[E0277]: expected a `std::ops::Fn<(&isize,)>` closure, found `unsafe fn(isize) -> isize {square}`
22
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:23:13
33
|
44
LL | let x = call_it(&square, 22);
5-
| ^^^^^^^ the trait `for<'r> std::ops::Fn<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}`
5+
| ^^^^^^^ expected an `Fn<(&isize,)>` closure, found `unsafe fn(isize) -> isize {square}`
66
|
7+
= help: the trait `for<'r> std::ops::Fn<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}`
78
note: required by `call_it`
89
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:18:1
910
|
1011
LL | fn call_it<F:Fn(&isize)->isize>(_: &F, _: isize) -> isize { 0 }
1112
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1213

13-
error[E0277]: the trait bound `for<'r> unsafe fn(isize) -> isize {square}: std::ops::FnMut<(&'r isize,)>` is not satisfied
14+
error[E0277]: expected a `std::ops::FnMut<(&isize,)>` closure, found `unsafe fn(isize) -> isize {square}`
1415
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:28:13
1516
|
1617
LL | let y = call_it_mut(&mut square, 22);
17-
| ^^^^^^^^^^^ the trait `for<'r> std::ops::FnMut<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}`
18+
| ^^^^^^^^^^^ expected an `FnMut<(&isize,)>` closure, found `unsafe fn(isize) -> isize {square}`
1819
|
20+
= help: the trait `for<'r> std::ops::FnMut<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}`
1921
note: required by `call_it_mut`
2022
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:19:1
2123
|
2224
LL | fn call_it_mut<F:FnMut(&isize)->isize>(_: &mut F, _: isize) -> isize { 0 }
2325
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2426

25-
error[E0277]: the trait bound `for<'r> unsafe fn(isize) -> isize {square}: std::ops::FnOnce<(&'r isize,)>` is not satisfied
27+
error[E0277]: expected a `std::ops::FnOnce<(&isize,)>` closure, found `unsafe fn(isize) -> isize {square}`
2628
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:33:13
2729
|
2830
LL | let z = call_it_once(square, 22);
29-
| ^^^^^^^^^^^^ the trait `for<'r> std::ops::FnOnce<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}`
31+
| ^^^^^^^^^^^^ expected an `FnOnce<(&isize,)>` closure, found `unsafe fn(isize) -> isize {square}`
3032
|
33+
= help: the trait `for<'r> std::ops::FnOnce<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}`
3134
note: required by `call_it_once`
3235
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:20:1
3336
|

0 commit comments

Comments
 (0)