From 73fd3497d4cc65c733cc1848bea363c00cb87878 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Fri, 24 May 2019 08:55:33 +0200 Subject: [PATCH] Deprecate `FnBox`. `Box` can be called directly, since 1.35 FCP completion: https://github.com/rust-lang/rust/issues/28796#issuecomment-439731515 --- src/liballoc/boxed.rs | 42 +++++++++++++++---- .../run-pass/unsized-locals/fnbox-compat.rs | 1 + .../ui/confuse-field-and-method/issue-2392.rs | 12 ++---- .../issue-2392.stderr | 28 ++++++------- 4 files changed, 52 insertions(+), 31 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 97c2d8e7a8e..bf8f5b8b91a 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -759,13 +759,14 @@ impl + ?Sized> Fn for Box { } } +/// `FnBox` is deprecated and will be removed. +/// `Box` can be called directly, since Rust 1.35.0. +/// /// `FnBox` is a version of the `FnOnce` intended for use with boxed -/// closure objects. The idea is that where one would normally store a -/// `Box` in a data structure, you should use +/// closure objects. The idea was that where one would normally store a +/// `Box` in a data structure, you whould use /// `Box`. The two traits behave essentially the same, except -/// that a `FnBox` closure can only be called if it is boxed. (Note -/// that `FnBox` may be deprecated in the future if `Box` -/// closures become directly usable.) +/// that a `FnBox` closure can only be called if it is boxed. /// /// # Examples /// @@ -777,6 +778,7 @@ impl + ?Sized> Fn for Box { /// /// ``` /// #![feature(fnbox)] +/// #![allow(deprecated)] /// /// use std::boxed::FnBox; /// use std::collections::HashMap; @@ -796,16 +798,38 @@ impl + ?Sized> Fn for Box { /// } /// } /// ``` +/// +/// In Rust 1.35.0 or later, use `FnOnce`, `FnMut`, or `Fn` instead: +/// +/// ``` +/// use std::collections::HashMap; +/// +/// fn make_map() -> HashMap i32>> { +/// let mut map: HashMap i32>> = HashMap::new(); +/// map.insert(1, Box::new(|| 22)); +/// map.insert(2, Box::new(|| 44)); +/// map +/// } +/// +/// fn main() { +/// let mut map = make_map(); +/// for i in &[1, 2] { +/// let f = map.remove(&i).unwrap(); +/// assert_eq!(f(), i * 22); +/// } +/// } +/// ``` #[rustc_paren_sugar] -#[unstable(feature = "fnbox", - reason = "will be deprecated if and when `Box` becomes usable", issue = "28796")] +#[unstable(feature = "fnbox", issue = "28796")] +#[rustc_deprecated(reason = "use `FnOnce`, `FnMut`, or `Fn` instead", since = "1.37.0")] pub trait FnBox: FnOnce { /// Performs the call operation. fn call_box(self: Box, args: A) -> Self::Output; } -#[unstable(feature = "fnbox", - reason = "will be deprecated if and when `Box` becomes usable", issue = "28796")] +#[unstable(feature = "fnbox", issue = "28796")] +#[rustc_deprecated(reason = "use `FnOnce`, `FnMut`, or `Fn` instead", since = "1.37.0")] +#[allow(deprecated, deprecated_in_future)] impl FnBox for F where F: FnOnce { diff --git a/src/test/run-pass/unsized-locals/fnbox-compat.rs b/src/test/run-pass/unsized-locals/fnbox-compat.rs index 5ec54ada13b..74a4dd5d851 100644 --- a/src/test/run-pass/unsized-locals/fnbox-compat.rs +++ b/src/test/run-pass/unsized-locals/fnbox-compat.rs @@ -1,4 +1,5 @@ #![feature(fnbox)] +#![allow(deprecated, deprecated_in_future)] use std::boxed::FnBox; diff --git a/src/test/ui/confuse-field-and-method/issue-2392.rs b/src/test/ui/confuse-field-and-method/issue-2392.rs index 41287c25914..c242b6c2c20 100644 --- a/src/test/ui/confuse-field-and-method/issue-2392.rs +++ b/src/test/ui/confuse-field-and-method/issue-2392.rs @@ -1,7 +1,3 @@ -#![feature(core, fnbox)] - -use std::boxed::FnBox; - struct FuncContainer { f1: fn(data: u8), f2: extern "C" fn(data: u8), @@ -18,7 +14,7 @@ struct Obj where F: FnOnce() -> u32 { } struct BoxedObj { - boxed_closure: Box u32>, + boxed_closure: Box u32>, } struct Wrapper where F: FnMut() -> u32 { @@ -29,8 +25,8 @@ fn func() -> u32 { 0 } -fn check_expression() -> Obj u32>> { - Obj { closure: Box::new(|| 42_u32) as Box u32>, not_closure: 42 } +fn check_expression() -> Obj u32>> { + Obj { closure: Box::new(|| 42_u32) as Box u32>, not_closure: 42 } } fn main() { @@ -48,7 +44,7 @@ fn main() { let boxed_fn = BoxedObj { boxed_closure: Box::new(func) }; boxed_fn.boxed_closure();//~ ERROR no method named `boxed_closure` found - let boxed_closure = BoxedObj { boxed_closure: Box::new(|| 42_u32) as Box u32> }; + let boxed_closure = BoxedObj { boxed_closure: Box::new(|| 42_u32) as Box u32> }; boxed_closure.boxed_closure();//~ ERROR no method named `boxed_closure` found // test expression writing in the notes diff --git a/src/test/ui/confuse-field-and-method/issue-2392.stderr b/src/test/ui/confuse-field-and-method/issue-2392.stderr index 2107318d87b..351cfef1b53 100644 --- a/src/test/ui/confuse-field-and-method/issue-2392.stderr +++ b/src/test/ui/confuse-field-and-method/issue-2392.stderr @@ -1,5 +1,5 @@ -error[E0599]: no method named `closure` found for type `Obj<[closure@$DIR/issue-2392.rs:39:36: 39:41]>` in the current scope - --> $DIR/issue-2392.rs:40:15 +error[E0599]: no method named `closure` found for type `Obj<[closure@$DIR/issue-2392.rs:35:36: 35:41]>` in the current scope + --> $DIR/issue-2392.rs:36:15 | LL | struct Obj where F: FnOnce() -> u32 { | -------------------------------------- method `closure` not found for this @@ -11,8 +11,8 @@ help: to call the function stored in `closure`, surround the field access with p LL | (o_closure.closure)(); | ^ ^ -error[E0599]: no method named `not_closure` found for type `Obj<[closure@$DIR/issue-2392.rs:39:36: 39:41]>` in the current scope - --> $DIR/issue-2392.rs:42:15 +error[E0599]: no method named `not_closure` found for type `Obj<[closure@$DIR/issue-2392.rs:35:36: 35:41]>` in the current scope + --> $DIR/issue-2392.rs:38:15 | LL | struct Obj where F: FnOnce() -> u32 { | -------------------------------------- method `not_closure` not found for this @@ -23,7 +23,7 @@ LL | o_closure.not_closure(); | field, not a method error[E0599]: no method named `closure` found for type `Obj u32 {func}>` in the current scope - --> $DIR/issue-2392.rs:46:12 + --> $DIR/issue-2392.rs:42:12 | LL | struct Obj where F: FnOnce() -> u32 { | -------------------------------------- method `closure` not found for this @@ -36,7 +36,7 @@ LL | (o_func.closure)(); | ^ ^ error[E0599]: no method named `boxed_closure` found for type `BoxedObj` in the current scope - --> $DIR/issue-2392.rs:49:14 + --> $DIR/issue-2392.rs:45:14 | LL | struct BoxedObj { | --------------- method `boxed_closure` not found for this @@ -49,7 +49,7 @@ LL | (boxed_fn.boxed_closure)(); | ^ ^ error[E0599]: no method named `boxed_closure` found for type `BoxedObj` in the current scope - --> $DIR/issue-2392.rs:52:19 + --> $DIR/issue-2392.rs:48:19 | LL | struct BoxedObj { | --------------- method `boxed_closure` not found for this @@ -62,7 +62,7 @@ LL | (boxed_closure.boxed_closure)(); | ^ ^ error[E0599]: no method named `closure` found for type `Obj u32 {func}>` in the current scope - --> $DIR/issue-2392.rs:57:12 + --> $DIR/issue-2392.rs:53:12 | LL | struct Obj where F: FnOnce() -> u32 { | -------------------------------------- method `closure` not found for this @@ -75,7 +75,7 @@ LL | (w.wrap.closure)(); | ^ ^ error[E0599]: no method named `not_closure` found for type `Obj u32 {func}>` in the current scope - --> $DIR/issue-2392.rs:59:12 + --> $DIR/issue-2392.rs:55:12 | LL | struct Obj where F: FnOnce() -> u32 { | -------------------------------------- method `not_closure` not found for this @@ -85,8 +85,8 @@ LL | w.wrap.not_closure(); | | | field, not a method -error[E0599]: no method named `closure` found for type `Obj + 'static)>>` in the current scope - --> $DIR/issue-2392.rs:62:24 +error[E0599]: no method named `closure` found for type `Obj u32 + 'static)>>` in the current scope + --> $DIR/issue-2392.rs:58:24 | LL | struct Obj where F: FnOnce() -> u32 { | -------------------------------------- method `closure` not found for this @@ -99,7 +99,7 @@ LL | (check_expression().closure)(); | ^ ^ error[E0599]: no method named `f1` found for type `FuncContainer` in the current scope - --> $DIR/issue-2392.rs:68:31 + --> $DIR/issue-2392.rs:64:31 | LL | struct FuncContainer { | -------------------- method `f1` not found for this @@ -112,7 +112,7 @@ LL | ((*self.container).f1)(1); | ^ ^ error[E0599]: no method named `f2` found for type `FuncContainer` in the current scope - --> $DIR/issue-2392.rs:69:31 + --> $DIR/issue-2392.rs:65:31 | LL | struct FuncContainer { | -------------------- method `f2` not found for this @@ -125,7 +125,7 @@ LL | ((*self.container).f2)(1); | ^ ^ error[E0599]: no method named `f3` found for type `FuncContainer` in the current scope - --> $DIR/issue-2392.rs:70:31 + --> $DIR/issue-2392.rs:66:31 | LL | struct FuncContainer { | -------------------- method `f3` not found for this