Skip to content

Deny bare trait objects in src/librustc_data_structures #52253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/librustc_data_structures/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
//!
//! This API is completely unstable and subject to change.

#![deny(bare_trait_objects)]

#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://doc.rust-lang.org/nightly/")]
Expand Down
52 changes: 26 additions & 26 deletions src/librustc_data_structures/owning_ref/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ unsafe impl<O, T: ?Sized> Send for OwningRefMut<O, T>
unsafe impl<O, T: ?Sized> Sync for OwningRefMut<O, T>
where O: Sync, for<'a> (&'a mut T): Sync {}

impl Debug for Erased {
impl Debug for dyn Erased {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "<Erased>",)
}
Expand Down Expand Up @@ -1166,35 +1166,35 @@ pub type MutexGuardRefMut<'a, T, U = T> = OwningRefMut<MutexGuard<'a, T>, U>;
pub type RwLockWriteGuardRefMut<'a, T, U = T> = OwningRef<RwLockWriteGuard<'a, T>, U>;

unsafe impl<'a, T: 'a> IntoErased<'a> for Box<T> {
type Erased = Box<Erased + 'a>;
type Erased = Box<dyn Erased + 'a>;
fn into_erased(self) -> Self::Erased {
self
}
}
unsafe impl<'a, T: 'a> IntoErased<'a> for Rc<T> {
type Erased = Rc<Erased + 'a>;
type Erased = Rc<dyn Erased + 'a>;
fn into_erased(self) -> Self::Erased {
self
}
}
unsafe impl<'a, T: 'a> IntoErased<'a> for Arc<T> {
type Erased = Arc<Erased + 'a>;
type Erased = Arc<dyn Erased + 'a>;
fn into_erased(self) -> Self::Erased {
self
}
}

unsafe impl<'a, T: Send + 'a> IntoErasedSend<'a> for Box<T> {
type Erased = Box<Erased + Send + 'a>;
type Erased = Box<dyn Erased + Send + 'a>;
fn into_erased_send(self) -> Self::Erased {
self
}
}

unsafe impl<'a, T: Send + 'a> IntoErasedSendSync<'a> for Box<T> {
type Erased = Box<Erased + Sync + Send + 'a>;
type Erased = Box<dyn Erased + Sync + Send + 'a>;
fn into_erased_send_sync(self) -> Self::Erased {
let result: Box<Erased + Send + 'a> = self;
let result: Box<dyn Erased + Send + 'a> = self;
// This is safe since Erased can always implement Sync
// Only the destructor is available and it takes &mut self
unsafe {
Expand All @@ -1204,21 +1204,21 @@ unsafe impl<'a, T: Send + 'a> IntoErasedSendSync<'a> for Box<T> {
}

unsafe impl<'a, T: Send + Sync + 'a> IntoErasedSendSync<'a> for Arc<T> {
type Erased = Arc<Erased + Send + Sync + 'a>;
type Erased = Arc<dyn Erased + Send + Sync + 'a>;
fn into_erased_send_sync(self) -> Self::Erased {
self
}
}

/// Typedef of a owning reference that uses an erased `Box` as the owner.
pub type ErasedBoxRef<U> = OwningRef<Box<Erased>, U>;
pub type ErasedBoxRef<U> = OwningRef<Box<dyn Erased>, U>;
/// Typedef of a owning reference that uses an erased `Rc` as the owner.
pub type ErasedRcRef<U> = OwningRef<Rc<Erased>, U>;
pub type ErasedRcRef<U> = OwningRef<Rc<dyn Erased>, U>;
/// Typedef of a owning reference that uses an erased `Arc` as the owner.
pub type ErasedArcRef<U> = OwningRef<Arc<Erased>, U>;
pub type ErasedArcRef<U> = OwningRef<Arc<dyn Erased>, U>;

/// Typedef of a mutable owning reference that uses an erased `Box` as the owner.
pub type ErasedBoxRefMut<U> = OwningRefMut<Box<Erased>, U>;
pub type ErasedBoxRefMut<U> = OwningRefMut<Box<dyn Erased>, U>;

#[cfg(test)]
mod tests {
Expand Down Expand Up @@ -1443,8 +1443,8 @@ mod tests {
let c: OwningRef<Rc<Vec<u8>>, [u8]> = unsafe {a.map_owner(Rc::new)};
let d: OwningRef<Rc<Box<[u8]>>, [u8]> = unsafe {b.map_owner(Rc::new)};

let e: OwningRef<Rc<Erased>, [u8]> = c.erase_owner();
let f: OwningRef<Rc<Erased>, [u8]> = d.erase_owner();
let e: OwningRef<Rc<dyn Erased>, [u8]> = c.erase_owner();
let f: OwningRef<Rc<dyn Erased>, [u8]> = d.erase_owner();

let _g = e.clone();
let _h = f.clone();
Expand All @@ -1460,16 +1460,16 @@ mod tests {
let c: OwningRef<Box<Vec<u8>>, [u8]> = a.map_owner_box();
let d: OwningRef<Box<Box<[u8]>>, [u8]> = b.map_owner_box();

let _e: OwningRef<Box<Erased>, [u8]> = c.erase_owner();
let _f: OwningRef<Box<Erased>, [u8]> = d.erase_owner();
let _e: OwningRef<Box<dyn Erased>, [u8]> = c.erase_owner();
let _f: OwningRef<Box<dyn Erased>, [u8]> = d.erase_owner();
}

#[test]
fn try_map1() {
use std::any::Any;

let x = Box::new(123_i32);
let y: Box<Any> = x;
let y: Box<dyn Any> = x;

OwningRef::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_ok();
}
Expand All @@ -1479,7 +1479,7 @@ mod tests {
use std::any::Any;

let x = Box::new(123_i32);
let y: Box<Any> = x;
let y: Box<dyn Any> = x;

OwningRef::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_err();
}
Expand Down Expand Up @@ -1843,8 +1843,8 @@ mod tests {
let c: OwningRefMut<Box<Vec<u8>>, [u8]> = unsafe {a.map_owner(Box::new)};
let d: OwningRefMut<Box<Box<[u8]>>, [u8]> = unsafe {b.map_owner(Box::new)};

let _e: OwningRefMut<Box<Erased>, [u8]> = c.erase_owner();
let _f: OwningRefMut<Box<Erased>, [u8]> = d.erase_owner();
let _e: OwningRefMut<Box<dyn Erased>, [u8]> = c.erase_owner();
let _f: OwningRefMut<Box<dyn Erased>, [u8]> = d.erase_owner();
}

#[test]
Expand All @@ -1857,16 +1857,16 @@ mod tests {
let c: OwningRefMut<Box<Vec<u8>>, [u8]> = a.map_owner_box();
let d: OwningRefMut<Box<Box<[u8]>>, [u8]> = b.map_owner_box();

let _e: OwningRefMut<Box<Erased>, [u8]> = c.erase_owner();
let _f: OwningRefMut<Box<Erased>, [u8]> = d.erase_owner();
let _e: OwningRefMut<Box<dyn Erased>, [u8]> = c.erase_owner();
let _f: OwningRefMut<Box<dyn Erased>, [u8]> = d.erase_owner();
}

#[test]
fn try_map1() {
use std::any::Any;

let x = Box::new(123_i32);
let y: Box<Any> = x;
let y: Box<dyn Any> = x;

OwningRefMut::new(y).try_map_mut(|x| x.downcast_mut::<i32>().ok_or(())).is_ok();
}
Expand All @@ -1876,7 +1876,7 @@ mod tests {
use std::any::Any;

let x = Box::new(123_i32);
let y: Box<Any> = x;
let y: Box<dyn Any> = x;

OwningRefMut::new(y).try_map_mut(|x| x.downcast_mut::<i32>().ok_or(())).is_err();
}
Expand All @@ -1886,7 +1886,7 @@ mod tests {
use std::any::Any;

let x = Box::new(123_i32);
let y: Box<Any> = x;
let y: Box<dyn Any> = x;

OwningRefMut::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_ok();
}
Expand All @@ -1896,7 +1896,7 @@ mod tests {
use std::any::Any;

let x = Box::new(123_i32);
let y: Box<Any> = x;
let y: Box<dyn Any> = x;

OwningRefMut::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_err();
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_data_structures/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ cfg_if! {
t.into_iter()
}

pub type MetadataRef = OwningRef<Box<Erased>, [u8]>;
pub type MetadataRef = OwningRef<Box<dyn Erased>, [u8]>;

pub use std::rc::Rc as Lrc;
pub use std::rc::Weak as Weak;
Expand Down Expand Up @@ -268,7 +268,7 @@ cfg_if! {
t.into_par_iter()
}

pub type MetadataRef = OwningRef<Box<Erased + Send + Sync>, [u8]>;
pub type MetadataRef = OwningRef<Box<dyn Erased + Send + Sync>, [u8]>;

/// This makes locks panic if they are already held.
/// It is only useful when you are running in a single thread
Expand Down