From e5e14d307b1c6cb551574369f513b8d87fbe52b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 12 Aug 2018 12:21:53 -0700 Subject: [PATCH] When closure with no arguments was expected, suggest wrapping --- src/libcore/ops/function.rs | 15 +++++++++++++++ src/test/ui/closure-expected.rs | 15 +++++++++++++++ src/test/ui/closure-expected.stderr | 12 ++++++++++++ src/test/ui/extern/extern-wrong-value-type.rs | 2 +- src/test/ui/fn/fn-trait-formatting.rs | 2 +- src/test/ui/issues/issue-22034.rs | 2 +- src/test/ui/issues/issue-22034.stderr | 6 ++++-- src/test/ui/issues/issue-23966.stderr | 6 ++++-- 8 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 src/test/ui/closure-expected.rs create mode 100644 src/test/ui/closure-expected.stderr diff --git a/src/libcore/ops/function.rs b/src/libcore/ops/function.rs index d10fcb86b24..3ebd10a9209 100644 --- a/src/libcore/ops/function.rs +++ b/src/libcore/ops/function.rs @@ -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 : FnMut { /// Performs the call operation. @@ -139,6 +144,11 @@ pub trait Fn : FnMut { #[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 : FnOnce { /// Performs the call operation. @@ -212,6 +222,11 @@ pub trait FnMut : FnOnce { #[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 { /// The returned type after the call operator is used. diff --git a/src/test/ui/closure-expected.rs b/src/test/ui/closure-expected.rs new file mode 100644 index 00000000000..ff52948ca85 --- /dev/null +++ b/src/test/ui/closure-expected.rs @@ -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 or the MIT license +// , 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}` +} diff --git a/src/test/ui/closure-expected.stderr b/src/test/ui/closure-expected.stderr new file mode 100644 index 00000000000..0da506b5b81 --- /dev/null +++ b/src/test/ui/closure-expected.stderr @@ -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`. diff --git a/src/test/ui/extern/extern-wrong-value-type.rs b/src/test/ui/extern/extern-wrong-value-type.rs index 66b06c505e4..ea313385c10 100644 --- a/src/test/ui/extern/extern-wrong-value-type.rs +++ b/src/test/ui/extern/extern-wrong-value-type.rs @@ -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}` } diff --git a/src/test/ui/fn/fn-trait-formatting.rs b/src/test/ui/fn/fn-trait-formatting.rs index 56d64d77ee2..ac7b0a60984 100644 --- a/src/test/ui/fn/fn-trait-formatting.rs +++ b/src/test/ui/fn/fn-trait-formatting.rs @@ -27,5 +27,5 @@ fn main() { //~| found type `std::boxed::Box isize>` needs_fn(1); - //~^ ERROR : std::ops::Fn<(isize,)>` + //~^ ERROR expected a `std::ops::Fn<(isize,)>` closure, found `{integer}` } diff --git a/src/test/ui/issues/issue-22034.rs b/src/test/ui/issues/issue-22034.rs index 5271ea79917..2708de2c13a 100644 --- a/src/test/ui/issues/issue-22034.rs +++ b/src/test/ui/issues/issue-22034.rs @@ -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 `()` }; } diff --git a/src/test/ui/issues/issue-22034.stderr b/src/test/ui/issues/issue-22034.stderr index bac62a1301a..da376fedbb9 100644 --- a/src/test/ui/issues/issue-22034.stderr +++ b/src/test/ui/issues/issue-22034.stderr @@ -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 diff --git a/src/test/ui/issues/issue-23966.stderr b/src/test/ui/issues/issue-23966.stderr index 07d2cfbf55f..0418933180c 100644 --- a/src/test/ui/issues/issue-23966.stderr +++ b/src/test/ui/issues/issue-23966.stderr @@ -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