Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When closure with no arguments was expected, suggest wrapping #53296

Merged
merged 2 commits into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/libcore/ops/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
#[lang = "fn"]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_paren_sugar]
#[rustc_on_unimplemented(
on(Args="()", note="wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}"),
message="expected a `{Fn}<{Args}>` closure, found `{Self}`",
label="expected an `Fn<{Args}>` closure, found `{Self}`",
)]
#[fundamental] // so that regex can rely that `&str: !FnMut`
pub trait Fn<Args> : FnMut<Args> {
/// Performs the call operation.
Expand Down Expand Up @@ -139,6 +144,11 @@ pub trait Fn<Args> : FnMut<Args> {
#[lang = "fn_mut"]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_paren_sugar]
#[rustc_on_unimplemented(
on(Args="()", note="wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}"),
message="expected a `{FnMut}<{Args}>` closure, found `{Self}`",
label="expected an `FnMut<{Args}>` closure, found `{Self}`",
)]
#[fundamental] // so that regex can rely that `&str: !FnMut`
pub trait FnMut<Args> : FnOnce<Args> {
/// Performs the call operation.
Expand Down Expand Up @@ -212,6 +222,11 @@ pub trait FnMut<Args> : FnOnce<Args> {
#[lang = "fn_once"]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_paren_sugar]
#[rustc_on_unimplemented(
on(Args="()", note="wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}"),
message="expected a `{FnOnce}<{Args}>` closure, found `{Self}`",
label="expected an `FnOnce<{Args}>` closure, found `{Self}`",
)]
#[fundamental] // so that regex can rely that `&str: !FnMut`
pub trait FnOnce<Args> {
/// The returned type after the call operator is used.
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/closure-expected.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
let x = Some(1);
let y = x.or_else(4);
//~^ ERROR expected a `std::ops::FnOnce<()>` closure, found `{integer}`
}
12 changes: 12 additions & 0 deletions src/test/ui/closure-expected.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0277]: expected a `std::ops::FnOnce<()>` closure, found `{integer}`
--> $DIR/closure-expected.rs:13:15
|
LL | let y = x.or_else(4);
| ^^^^^^^ expected an `FnOnce<()>` closure, found `{integer}`
|
= help: the trait `std::ops::FnOnce<()>` is not implemented for `{integer}`
= note: wrap the `{integer}` in a closure with no arguments: `|| { /* code */ }

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
2 changes: 1 addition & 1 deletion src/test/ui/extern/extern-wrong-value-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ fn main() {
// extern functions are extern "C" fn
let _x: extern "C" fn() = f; // OK
is_fn(f);
//~^ ERROR `extern "C" fn() {f}: std::ops::Fn<()>` is not satisfied
//~^ ERROR expected a `std::ops::Fn<()>` closure, found `extern "C" fn() {f}`
}
6 changes: 4 additions & 2 deletions src/test/ui/extern/extern-wrong-value-type.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
error[E0277]: the trait bound `extern "C" fn() {f}: std::ops::Fn<()>` is not satisfied
error[E0277]: expected a `std::ops::Fn<()>` closure, found `extern "C" fn() {f}`
--> $DIR/extern-wrong-value-type.rs:19:5
|
LL | is_fn(f);
| ^^^^^ the trait `std::ops::Fn<()>` is not implemented for `extern "C" fn() {f}`
| ^^^^^ expected an `Fn<()>` closure, found `extern "C" fn() {f}`
|
= help: the trait `std::ops::Fn<()>` is not implemented for `extern "C" fn() {f}`
= note: wrap the `extern "C" fn() {f}` in a closure with no arguments: `|| { /* code */ }
note: required by `is_fn`
--> $DIR/extern-wrong-value-type.rs:14:1
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/fn/fn-trait-formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ fn main() {
//~| found type `std::boxed::Box<dyn std::ops::FnMut() -> isize>`

needs_fn(1);
//~^ ERROR : std::ops::Fn<(isize,)>`
//~^ ERROR expected a `std::ops::Fn<(isize,)>` closure, found `{integer}`
}
8 changes: 3 additions & 5 deletions src/test/ui/fn/fn-trait-formatting.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ LL | let _: () = (box || -> isize { unimplemented!() }) as Box<FnMut() -> is
= note: expected type `()`
found type `std::boxed::Box<dyn std::ops::FnMut() -> isize>`

error[E0277]: the trait bound `{integer}: std::ops::Fn<(isize,)>` is not satisfied
error[E0277]: expected a `std::ops::Fn<(isize,)>` closure, found `{integer}`
--> $DIR/fn-trait-formatting.rs:29:5
|
LL | needs_fn(1);
| ^^^^^^^^ the trait `std::ops::Fn<(isize,)>` is not implemented for `{integer}`
| ^^^^^^^^ expected an `Fn<(isize,)>` closure, found `{integer}`
|
= help: the following implementations were found:
<&'a F as std::ops::Fn<A>>
<core::str::LinesAnyMap as std::ops::Fn<(&'a str,)>>
= help: the trait `std::ops::Fn<(isize,)>` is not implemented for `{integer}`
note: required by `needs_fn`
--> $DIR/fn-trait-formatting.rs:13:1
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-22034.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ fn main() {
let ptr: *mut () = 0 as *mut _;
let _: &mut Fn() = unsafe {
&mut *(ptr as *mut Fn())
//~^ ERROR `(): std::ops::Fn<()>` is not satisfied
//~^ ERROR expected a `std::ops::Fn<()>` closure, found `()`
};
}
6 changes: 4 additions & 2 deletions src/test/ui/issues/issue-22034.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
error[E0277]: the trait bound `(): std::ops::Fn<()>` is not satisfied
error[E0277]: expected a `std::ops::Fn<()>` closure, found `()`
--> $DIR/issue-22034.rs:18:16
|
LL | &mut *(ptr as *mut Fn())
| ^^^ the trait `std::ops::Fn<()>` is not implemented for `()`
| ^^^ expected an `Fn<()>` closure, found `()`
|
= help: the trait `std::ops::Fn<()>` is not implemented for `()`
= note: wrap the `()` in a closure with no arguments: `|| { /* code */ }
= note: required for the cast to the object type `dyn std::ops::Fn()`

error: aborting due to previous error
Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/issues/issue-23966.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0277]: the trait bound `(): std::ops::FnMut<(_, char)>` is not satisfied
error[E0277]: expected a `std::ops::FnMut<(_, char)>` closure, found `()`
--> $DIR/issue-23966.rs:12:16
|
LL | "".chars().fold(|_, _| (), ());
| ^^^^ the trait `std::ops::FnMut<(_, char)>` is not implemented for `()`
| ^^^^ expected an `FnMut<(_, char)>` closure, found `()`
|
= help: the trait `std::ops::FnMut<(_, char)>` is not implemented for `()`

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
error[E0277]: the trait bound `S: std::ops::Fn<(isize,)>` is not satisfied
error[E0277]: expected a `std::ops::Fn<(isize,)>` closure, found `S`
--> $DIR/unboxed-closures-fnmut-as-fn.rs:38:13
|
LL | let x = call_it(&S, 22);
| ^^^^^^^ the trait `std::ops::Fn<(isize,)>` is not implemented for `S`
| ^^^^^^^ expected an `Fn<(isize,)>` closure, found `S`
|
= help: the trait `std::ops::Fn<(isize,)>` is not implemented for `S`
note: required by `call_it`
--> $DIR/unboxed-closures-fnmut-as-fn.rs:33:1
|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
error[E0277]: the trait bound `for<'r> for<'s> unsafe fn(&'s isize) -> isize {square}: std::ops::Fn<(&'r isize,)>` is not satisfied
error[E0277]: expected a `std::ops::Fn<(&isize,)>` closure, found `for<'r> unsafe fn(&'r isize) -> isize {square}`
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:22:13
|
LL | let x = call_it(&square, 22);
| ^^^^^^^ the trait `for<'r> std::ops::Fn<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}`
| ^^^^^^^ expected an `Fn<(&isize,)>` closure, found `for<'r> unsafe fn(&'r isize) -> isize {square}`
|
= help: the trait `for<'r> std::ops::Fn<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}`
note: required by `call_it`
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:17:1
|
LL | fn call_it<F:Fn(&isize)->isize>(_: &F, _: isize) -> isize { 0 }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

error[E0277]: the trait bound `for<'r> for<'s> unsafe fn(&'s isize) -> isize {square}: std::ops::FnOnce<(&'r isize,)>` is not satisfied
error[E0277]: expected a `std::ops::FnOnce<(&isize,)>` closure, found `for<'r> unsafe fn(&'r isize) -> isize {square}`
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:32:13
|
LL | let z = call_it_once(square, 22);
| ^^^^^^^^^^^^ the trait `for<'r> std::ops::FnOnce<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}`
| ^^^^^^^^^^^^ expected an `FnOnce<(&isize,)>` closure, found `for<'r> unsafe fn(&'r isize) -> isize {square}`
|
= help: the trait `for<'r> std::ops::FnOnce<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}`
note: required by `call_it_once`
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:19:1
|
Expand Down
15 changes: 9 additions & 6 deletions src/test/ui/unboxed-closures/unboxed-closures-wrong-abi.stderr
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
error[E0277]: the trait bound `for<'r> for<'s> extern "C" fn(&'s isize) -> isize {square}: std::ops::Fn<(&'r isize,)>` is not satisfied
error[E0277]: expected a `std::ops::Fn<(&isize,)>` closure, found `for<'r> extern "C" fn(&'r isize) -> isize {square}`
--> $DIR/unboxed-closures-wrong-abi.rs:22:13
|
LL | let x = call_it(&square, 22);
| ^^^^^^^ the trait `for<'r> std::ops::Fn<(&'r isize,)>` is not implemented for `for<'r> extern "C" fn(&'r isize) -> isize {square}`
| ^^^^^^^ expected an `Fn<(&isize,)>` closure, found `for<'r> extern "C" fn(&'r isize) -> isize {square}`
|
= help: the trait `for<'r> std::ops::Fn<(&'r isize,)>` is not implemented for `for<'r> extern "C" fn(&'r isize) -> isize {square}`
note: required by `call_it`
--> $DIR/unboxed-closures-wrong-abi.rs:17:1
|
LL | fn call_it<F:Fn(&isize)->isize>(_: &F, _: isize) -> isize { 0 }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

error[E0277]: the trait bound `for<'r> for<'s> extern "C" fn(&'s isize) -> isize {square}: std::ops::FnOnce<(&'r isize,)>` is not satisfied
error[E0277]: expected a `std::ops::FnOnce<(&isize,)>` closure, found `for<'r> extern "C" fn(&'r isize) -> isize {square}`
--> $DIR/unboxed-closures-wrong-abi.rs:32:13
|
LL | let z = call_it_once(square, 22);
| ^^^^^^^^^^^^ the trait `for<'r> std::ops::FnOnce<(&'r isize,)>` is not implemented for `for<'r> extern "C" fn(&'r isize) -> isize {square}`
| ^^^^^^^^^^^^ expected an `FnOnce<(&isize,)>` closure, found `for<'r> extern "C" fn(&'r isize) -> isize {square}`
|
= help: the trait `for<'r> std::ops::FnOnce<(&'r isize,)>` is not implemented for `for<'r> extern "C" fn(&'r isize) -> isize {square}`
note: required by `call_it_once`
--> $DIR/unboxed-closures-wrong-abi.rs:19:1
|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
error[E0277]: the trait bound `for<'r> unsafe fn(isize) -> isize {square}: std::ops::Fn<(&'r isize,)>` is not satisfied
error[E0277]: expected a `std::ops::Fn<(&isize,)>` closure, found `unsafe fn(isize) -> isize {square}`
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:23:13
|
LL | let x = call_it(&square, 22);
| ^^^^^^^ the trait `for<'r> std::ops::Fn<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}`
| ^^^^^^^ expected an `Fn<(&isize,)>` closure, found `unsafe fn(isize) -> isize {square}`
|
= help: the trait `for<'r> std::ops::Fn<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}`
note: required by `call_it`
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:18:1
|
LL | fn call_it<F:Fn(&isize)->isize>(_: &F, _: isize) -> isize { 0 }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

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