From 45e647dfe2e961bcab3d588e9ff1e1fafd89a9c9 Mon Sep 17 00:00:00 2001 From: Cristian Oliveira Date: Fri, 27 May 2016 22:38:09 -0300 Subject: [PATCH] Add error description for E0174 --- src/librustc_typeck/diagnostics.rs | 84 +++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 45aec9558feaf..d7d8c60e911d8 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -1972,6 +1972,89 @@ To learn more about traits, take a look at the Book: https://doc.rust-lang.org/book/traits.html "##, +E0174: r##" +This error occurs because of the explicit use of unboxed closure methods +that are an experimental feature in current Rust version. + +Example of erroneous code: + +```compile_fail +fn foo(mut f: F) { + f.call(("call",)); + // error: explicit use of unboxed closure method `call` + f.call_mut(("call_mut",)); + // error: explicit use of unboxed closure method `call_mut` + f.call_once(("call_once",)); + // error: explicit use of unboxed closure method `call_once` +} + +fn bar(text: &str) { + println!("Calling {} it works!", text); +} + +fn main() { + foo(bar); +} +``` + +Rust's implementation of closures is a bit different than other languages. +They are effectively syntax sugar for traits `Fn`, `FnMut` and `FnOnce`. +To understand better how the closures are implemented see here: +https://doc.rust-lang.org/book/closures.html#closure-implementation + +To fix this you can call them using parenthesis, like this: `foo()`. +When you execute the closure with parenthesis, under the hood you are executing +the method `call`, `call_mut` or `call_once`. However, using them explicitly is +currently an experimental feature. + +Example of an implicit call: + +``` +fn foo(f: F) { + f("using ()"); // Calling using () it works! +} + +fn bar(text: &str) { + println!("Calling {} it works!", text); +} + +fn main() { + foo(bar); +} +``` + +To enable the explicit calls you need to add `#![feature(unboxed_closures)]`. + +This feature is still unstable so you will also need to add +`#![feature(fn_traits)]`. +More details about this issue here: +https://github.com/rust-lang/rust/issues/29625 + +Example of use: + +``` +#![feature(fn_traits)] +#![feature(unboxed_closures)] + +fn foo(mut f: F) { + f.call(("call",)); // Calling 'call' it works! + f.call_mut(("call_mut",)); // Calling 'call_mut' it works! + f.call_once(("call_once",)); // Calling 'call_once' it works! +} + +fn bar(text: &str) { + println!("Calling '{}' it works!", text); +} + +fn main() { + foo(bar); +} +``` + +To see more about closures take a look here: +https://doc.rust-lang.org/book/closures.html` +"##, + E0178: r##" In types, the `+` type operator has low precedence, so it is often necessary to use parentheses. @@ -3891,7 +3974,6 @@ register_diagnostics! { E0167, // E0168, // E0173, // manual implementations of unboxed closure traits are experimental - E0174, // explicit use of unboxed closure methods are experimental E0182, E0183, // E0187, // can't infer the kind of the closure