When closure with no arguments was expected, suggest wrapping

This commit is contained in:
Esteban Küber 2018-08-12 12:21:53 -07:00
parent 0f4b4987cd
commit e5e14d307b
8 changed files with 53 additions and 7 deletions

View File

@ -66,6 +66,11 @@
#[lang = "fn"] #[lang = "fn"]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_paren_sugar] #[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` #[fundamental] // so that regex can rely that `&str: !FnMut`
pub trait Fn<Args> : FnMut<Args> { pub trait Fn<Args> : FnMut<Args> {
/// Performs the call operation. /// Performs the call operation.
@ -139,6 +144,11 @@ pub trait Fn<Args> : FnMut<Args> {
#[lang = "fn_mut"] #[lang = "fn_mut"]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_paren_sugar] #[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` #[fundamental] // so that regex can rely that `&str: !FnMut`
pub trait FnMut<Args> : FnOnce<Args> { pub trait FnMut<Args> : FnOnce<Args> {
/// Performs the call operation. /// Performs the call operation.
@ -212,6 +222,11 @@ pub trait FnMut<Args> : FnOnce<Args> {
#[lang = "fn_once"] #[lang = "fn_once"]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_paren_sugar] #[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` #[fundamental] // so that regex can rely that `&str: !FnMut`
pub trait FnOnce<Args> { pub trait FnOnce<Args> {
/// The returned type after the call operator is used. /// The returned type after the call operator is used.

View File

@ -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}`
}

View File

@ -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`.

View File

@ -17,5 +17,5 @@ fn main() {
// extern functions are extern "C" fn // extern functions are extern "C" fn
let _x: extern "C" fn() = f; // OK let _x: extern "C" fn() = f; // OK
is_fn(f); 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}`
} }

View File

@ -27,5 +27,5 @@ fn main() {
//~| found type `std::boxed::Box<dyn std::ops::FnMut() -> isize>` //~| found type `std::boxed::Box<dyn std::ops::FnMut() -> isize>`
needs_fn(1); needs_fn(1);
//~^ ERROR : std::ops::Fn<(isize,)>` //~^ ERROR expected a `std::ops::Fn<(isize,)>` closure, found `{integer}`
} }

View File

@ -16,6 +16,6 @@ fn main() {
let ptr: *mut () = 0 as *mut _; let ptr: *mut () = 0 as *mut _;
let _: &mut Fn() = unsafe { let _: &mut Fn() = unsafe {
&mut *(ptr as *mut Fn()) &mut *(ptr as *mut Fn())
//~^ ERROR `(): std::ops::Fn<()>` is not satisfied //~^ ERROR expected a `std::ops::Fn<()>` closure, found `()`
}; };
} }

View File

@ -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 --> $DIR/issue-22034.rs:18:16
| |
LL | &mut *(ptr as *mut Fn()) 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()` = note: required for the cast to the object type `dyn std::ops::Fn()`
error: aborting due to previous error error: aborting due to previous error

View File

@ -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 --> $DIR/issue-23966.rs:12:16
| |
LL | "".chars().fold(|_, _| (), ()); 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 error: aborting due to previous error