From 296e72f11c4120c7b38a0cc580ef8990e7a1c36d Mon Sep 17 00:00:00 2001 From: ljedrz Date: Tue, 10 Jul 2018 20:45:16 +0200 Subject: [PATCH 1/3] Deny bare trait objects in in src/liballoc --- src/liballoc/boxed.rs | 18 +++++++++--------- src/liballoc/lib.rs | 1 + src/liballoc/rc.rs | 4 ++-- src/liballoc/sync.rs | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index fb16bdf0ab4..44f15981137 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -446,7 +446,7 @@ impl From> for Box<[u8]> { } } -impl Box { +impl Box { #[inline] #[stable(feature = "rust1", since = "1.0.0")] /// Attempt to downcast the box to a concrete type. @@ -468,10 +468,10 @@ impl Box { /// print_if_string(Box::new(0i8)); /// } /// ``` - pub fn downcast(self) -> Result, Box> { + pub fn downcast(self) -> Result, Box> { if self.is::() { unsafe { - let raw: *mut Any = Box::into_raw(self); + let raw: *mut dyn Any = Box::into_raw(self); Ok(Box::from_raw(raw as *mut T)) } } else { @@ -480,7 +480,7 @@ impl Box { } } -impl Box { +impl Box { #[inline] #[stable(feature = "rust1", since = "1.0.0")] /// Attempt to downcast the box to a concrete type. @@ -502,10 +502,10 @@ impl Box { /// print_if_string(Box::new(0i8)); /// } /// ``` - pub fn downcast(self) -> Result, Box> { - >::downcast(self).map_err(|s| unsafe { + pub fn downcast(self) -> Result, Box> { + >::downcast(self).map_err(|s| unsafe { // reapply the Send marker - Box::from_raw(Box::into_raw(s) as *mut (Any + Send)) + Box::from_raw(Box::into_raw(s) as *mut (dyn Any + Send)) }) } } @@ -643,7 +643,7 @@ impl FnBox for F #[unstable(feature = "fnbox", reason = "will be deprecated if and when `Box` becomes usable", issue = "28796")] -impl<'a, A, R> FnOnce for Box + 'a> { +impl<'a, A, R> FnOnce for Box + 'a> { type Output = R; extern "rust-call" fn call_once(self, args: A) -> R { @@ -653,7 +653,7 @@ impl<'a, A, R> FnOnce for Box + 'a> { #[unstable(feature = "fnbox", reason = "will be deprecated if and when `Box` becomes usable", issue = "28796")] -impl<'a, A, R> FnOnce for Box + Send + 'a> { +impl<'a, A, R> FnOnce for Box + Send + 'a> { type Output = R; extern "rust-call" fn call_once(self, args: A) -> R { diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index ef619527e06..63cf01a0fac 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -72,6 +72,7 @@ test(no_crate_inject, attr(allow(unused_variables), deny(warnings))))] #![no_std] #![needs_allocator] +#![deny(bare_trait_objects)] #![deny(missing_debug_implementations)] #![cfg_attr(test, allow(deprecated))] // rand diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index f7c12b98f48..d55f6575a37 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -618,7 +618,7 @@ impl Rc { } } -impl Rc { +impl Rc { #[inline] #[stable(feature = "rc_downcast", since = "1.29.0")] /// Attempt to downcast the `Rc` to a concrete type. @@ -641,7 +641,7 @@ impl Rc { /// print_if_string(Rc::new(0i8)); /// } /// ``` - pub fn downcast(self) -> Result, Rc> { + pub fn downcast(self) -> Result, Rc> { if (*self).is::() { let ptr = self.ptr.cast::>(); forget(self); diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 5a738fc5444..920678bbd70 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -978,7 +978,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Arc { } } -impl Arc { +impl Arc { #[inline] #[stable(feature = "rc_downcast", since = "1.29.0")] /// Attempt to downcast the `Arc` to a concrete type. From cd44b3ddaddf22ef3c3b00f31491c660b851f8ee Mon Sep 17 00:00:00 2001 From: ljedrz Date: Tue, 10 Jul 2018 22:32:19 +0200 Subject: [PATCH 2/3] Add missing dyn in liballoc --- src/liballoc/boxed.rs | 18 +++++++++--------- src/liballoc/boxed_test.rs | 20 ++++++++++---------- src/liballoc/lib.rs | 1 - src/liballoc/rc.rs | 18 +++++++++--------- src/liballoc/sync.rs | 16 ++++++++-------- 5 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 44f15981137..fb16bdf0ab4 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -446,7 +446,7 @@ impl From> for Box<[u8]> { } } -impl Box { +impl Box { #[inline] #[stable(feature = "rust1", since = "1.0.0")] /// Attempt to downcast the box to a concrete type. @@ -468,10 +468,10 @@ impl Box { /// print_if_string(Box::new(0i8)); /// } /// ``` - pub fn downcast(self) -> Result, Box> { + pub fn downcast(self) -> Result, Box> { if self.is::() { unsafe { - let raw: *mut dyn Any = Box::into_raw(self); + let raw: *mut Any = Box::into_raw(self); Ok(Box::from_raw(raw as *mut T)) } } else { @@ -480,7 +480,7 @@ impl Box { } } -impl Box { +impl Box { #[inline] #[stable(feature = "rust1", since = "1.0.0")] /// Attempt to downcast the box to a concrete type. @@ -502,10 +502,10 @@ impl Box { /// print_if_string(Box::new(0i8)); /// } /// ``` - pub fn downcast(self) -> Result, Box> { - >::downcast(self).map_err(|s| unsafe { + pub fn downcast(self) -> Result, Box> { + >::downcast(self).map_err(|s| unsafe { // reapply the Send marker - Box::from_raw(Box::into_raw(s) as *mut (dyn Any + Send)) + Box::from_raw(Box::into_raw(s) as *mut (Any + Send)) }) } } @@ -643,7 +643,7 @@ impl FnBox for F #[unstable(feature = "fnbox", reason = "will be deprecated if and when `Box` becomes usable", issue = "28796")] -impl<'a, A, R> FnOnce for Box + 'a> { +impl<'a, A, R> FnOnce for Box + 'a> { type Output = R; extern "rust-call" fn call_once(self, args: A) -> R { @@ -653,7 +653,7 @@ impl<'a, A, R> FnOnce for Box + 'a> { #[unstable(feature = "fnbox", reason = "will be deprecated if and when `Box` becomes usable", issue = "28796")] -impl<'a, A, R> FnOnce for Box + Send + 'a> { +impl<'a, A, R> FnOnce for Box + Send + 'a> { type Output = R; extern "rust-call" fn call_once(self, args: A) -> R { diff --git a/src/liballoc/boxed_test.rs b/src/liballoc/boxed_test.rs index 837f8dfaca1..55995742a4a 100644 --- a/src/liballoc/boxed_test.rs +++ b/src/liballoc/boxed_test.rs @@ -31,8 +31,8 @@ struct Test; #[test] fn any_move() { - let a = Box::new(8) as Box; - let b = Box::new(Test) as Box; + let a = Box::new(8) as Box; + let b = Box::new(Test) as Box; match a.downcast::() { Ok(a) => { @@ -47,8 +47,8 @@ fn any_move() { Err(..) => panic!(), } - let a = Box::new(8) as Box; - let b = Box::new(Test) as Box; + let a = Box::new(8) as Box; + let b = Box::new(Test) as Box; assert!(a.downcast::>().is_err()); assert!(b.downcast::>().is_err()); @@ -56,8 +56,8 @@ fn any_move() { #[test] fn test_show() { - let a = Box::new(8) as Box; - let b = Box::new(Test) as Box; + let a = Box::new(8) as Box; + let b = Box::new(Test) as Box; let a_str = format!("{:?}", a); let b_str = format!("{:?}", b); assert_eq!(a_str, "Any"); @@ -65,8 +65,8 @@ fn test_show() { static EIGHT: usize = 8; static TEST: Test = Test; - let a = &EIGHT as &Any; - let b = &TEST as &Any; + let a = &EIGHT as &dyn Any; + let b = &TEST as &dyn Any; let s = format!("{:?}", a); assert_eq!(s, "Any"); let s = format!("{:?}", b); @@ -110,12 +110,12 @@ fn raw_trait() { } } - let x: Box = Box::new(Bar(17)); + let x: Box = Box::new(Bar(17)); let p = Box::into_raw(x); unsafe { assert_eq!(17, (*p).get()); (*p).set(19); - let y: Box = Box::from_raw(p); + let y: Box = Box::from_raw(p); assert_eq!(19, y.get()); } } diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index 63cf01a0fac..ef619527e06 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -72,7 +72,6 @@ test(no_crate_inject, attr(allow(unused_variables), deny(warnings))))] #![no_std] #![needs_allocator] -#![deny(bare_trait_objects)] #![deny(missing_debug_implementations)] #![cfg_attr(test, allow(deprecated))] // rand diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index d55f6575a37..3643f78d323 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -618,7 +618,7 @@ impl Rc { } } -impl Rc { +impl Rc { #[inline] #[stable(feature = "rc_downcast", since = "1.29.0")] /// Attempt to downcast the `Rc` to a concrete type. @@ -641,7 +641,7 @@ impl Rc { /// print_if_string(Rc::new(0i8)); /// } /// ``` - pub fn downcast(self) -> Result, Rc> { + pub fn downcast(self) -> Result, Rc> { if (*self).is::() { let ptr = self.ptr.cast::>(); forget(self); @@ -1554,7 +1554,7 @@ mod tests { assert_eq!(unsafe { &*ptr }, "foo"); assert_eq!(rc, rc2); - let rc: Rc = Rc::new(123); + let rc: Rc = Rc::new(123); let ptr = Rc::into_raw(rc.clone()); let rc2 = unsafe { Rc::from_raw(ptr) }; @@ -1755,8 +1755,8 @@ mod tests { use std::fmt::Display; use std::string::ToString; - let b: Box = box 123; - let r: Rc = Rc::from(b); + let b: Box = box 123; + let r: Rc = Rc::from(b); assert_eq!(r.to_string(), "123"); } @@ -1765,8 +1765,8 @@ mod tests { fn test_from_box_trait_zero_sized() { use std::fmt::Debug; - let b: Box = box (); - let r: Rc = Rc::from(b); + let b: Box = box (); + let r: Rc = Rc::from(b); assert_eq!(format!("{:?}", r), "()"); } @@ -1783,8 +1783,8 @@ mod tests { fn test_downcast() { use std::any::Any; - let r1: Rc = Rc::new(i32::max_value()); - let r2: Rc = Rc::new("abc"); + let r1: Rc = Rc::new(i32::max_value()); + let r2: Rc = Rc::new("abc"); assert!(r1.clone().downcast::().is_err()); diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 920678bbd70..35aae191683 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -978,7 +978,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Arc { } } -impl Arc { +impl Arc { #[inline] #[stable(feature = "rc_downcast", since = "1.29.0")] /// Attempt to downcast the `Arc` to a concrete type. @@ -1574,7 +1574,7 @@ mod tests { assert_eq!(unsafe { &*ptr }, "foo"); assert_eq!(arc, arc2); - let arc: Arc = Arc::new(123); + let arc: Arc = Arc::new(123); let ptr = Arc::into_raw(arc.clone()); let arc2 = unsafe { Arc::from_raw(ptr) }; @@ -1879,8 +1879,8 @@ mod tests { use std::fmt::Display; use std::string::ToString; - let b: Box = box 123; - let r: Arc = Arc::from(b); + let b: Box = box 123; + let r: Arc = Arc::from(b); assert_eq!(r.to_string(), "123"); } @@ -1889,8 +1889,8 @@ mod tests { fn test_from_box_trait_zero_sized() { use std::fmt::Debug; - let b: Box = box (); - let r: Arc = Arc::from(b); + let b: Box = box (); + let r: Arc = Arc::from(b); assert_eq!(format!("{:?}", r), "()"); } @@ -1907,8 +1907,8 @@ mod tests { fn test_downcast() { use std::any::Any; - let r1: Arc = Arc::new(i32::max_value()); - let r2: Arc = Arc::new("abc"); + let r1: Arc = Arc::new(i32::max_value()); + let r2: Arc = Arc::new("abc"); assert!(r1.clone().downcast::().is_err()); From 217f8fbd4512ebe94fb021ee564ea6c6dae6a919 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Wed, 11 Jul 2018 10:19:54 +0200 Subject: [PATCH 3/3] Revert borked changes in last commit. --- src/liballoc/boxed.rs | 18 +++++++++--------- src/liballoc/lib.rs | 1 + src/liballoc/rc.rs | 4 ++-- src/liballoc/sync.rs | 6 +++--- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index fb16bdf0ab4..44f15981137 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -446,7 +446,7 @@ impl From> for Box<[u8]> { } } -impl Box { +impl Box { #[inline] #[stable(feature = "rust1", since = "1.0.0")] /// Attempt to downcast the box to a concrete type. @@ -468,10 +468,10 @@ impl Box { /// print_if_string(Box::new(0i8)); /// } /// ``` - pub fn downcast(self) -> Result, Box> { + pub fn downcast(self) -> Result, Box> { if self.is::() { unsafe { - let raw: *mut Any = Box::into_raw(self); + let raw: *mut dyn Any = Box::into_raw(self); Ok(Box::from_raw(raw as *mut T)) } } else { @@ -480,7 +480,7 @@ impl Box { } } -impl Box { +impl Box { #[inline] #[stable(feature = "rust1", since = "1.0.0")] /// Attempt to downcast the box to a concrete type. @@ -502,10 +502,10 @@ impl Box { /// print_if_string(Box::new(0i8)); /// } /// ``` - pub fn downcast(self) -> Result, Box> { - >::downcast(self).map_err(|s| unsafe { + pub fn downcast(self) -> Result, Box> { + >::downcast(self).map_err(|s| unsafe { // reapply the Send marker - Box::from_raw(Box::into_raw(s) as *mut (Any + Send)) + Box::from_raw(Box::into_raw(s) as *mut (dyn Any + Send)) }) } } @@ -643,7 +643,7 @@ impl FnBox for F #[unstable(feature = "fnbox", reason = "will be deprecated if and when `Box` becomes usable", issue = "28796")] -impl<'a, A, R> FnOnce for Box + 'a> { +impl<'a, A, R> FnOnce for Box + 'a> { type Output = R; extern "rust-call" fn call_once(self, args: A) -> R { @@ -653,7 +653,7 @@ impl<'a, A, R> FnOnce for Box + 'a> { #[unstable(feature = "fnbox", reason = "will be deprecated if and when `Box` becomes usable", issue = "28796")] -impl<'a, A, R> FnOnce for Box + Send + 'a> { +impl<'a, A, R> FnOnce for Box + Send + 'a> { type Output = R; extern "rust-call" fn call_once(self, args: A) -> R { diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index ef619527e06..63cf01a0fac 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -72,6 +72,7 @@ test(no_crate_inject, attr(allow(unused_variables), deny(warnings))))] #![no_std] #![needs_allocator] +#![deny(bare_trait_objects)] #![deny(missing_debug_implementations)] #![cfg_attr(test, allow(deprecated))] // rand diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 3643f78d323..d76acb28df9 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -618,7 +618,7 @@ impl Rc { } } -impl Rc { +impl Rc { #[inline] #[stable(feature = "rc_downcast", since = "1.29.0")] /// Attempt to downcast the `Rc` to a concrete type. @@ -641,7 +641,7 @@ impl Rc { /// print_if_string(Rc::new(0i8)); /// } /// ``` - pub fn downcast(self) -> Result, Rc> { + pub fn downcast(self) -> Result, Rc> { if (*self).is::() { let ptr = self.ptr.cast::>(); forget(self); diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 35aae191683..5def0237e7e 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -978,10 +978,10 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Arc { } } -impl Arc { +impl Arc { #[inline] #[stable(feature = "rc_downcast", since = "1.29.0")] - /// Attempt to downcast the `Arc` to a concrete type. + /// Attempt to downcast the `Arc` to a concrete type. /// /// # Examples /// @@ -989,7 +989,7 @@ impl Arc { /// use std::any::Any; /// use std::sync::Arc; /// - /// fn print_if_string(value: Arc) { + /// fn print_if_string(value: Arc) { /// if let Ok(string) = value.downcast::() { /// println!("String ({}): {}", string.len(), string); /// }