Skip to content

Commit

Permalink
Deny bare trait objects in src/libstd.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljedrz committed Jul 10, 2018
1 parent 77117e3 commit 560d807
Show file tree
Hide file tree
Showing 20 changed files with 99 additions and 98 deletions.
86 changes: 43 additions & 43 deletions src/libstd/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub trait Error: Debug + Display {
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn cause(&self) -> Option<&Error> { None }
fn cause(&self) -> Option<&dyn Error> { None }

/// Get the `TypeId` of `self`
#[doc(hidden)]
Expand All @@ -151,22 +151,22 @@ pub trait Error: Debug + Display {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, E: Error + 'a> From<E> for Box<Error + 'a> {
fn from(err: E) -> Box<Error + 'a> {
impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
fn from(err: E) -> Box<dyn Error + 'a> {
Box::new(err)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<Error + Send + Sync + 'a> {
fn from(err: E) -> Box<Error + Send + Sync + 'a> {
impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync + 'a> {
fn from(err: E) -> Box<dyn Error + Send + Sync + 'a> {
Box::new(err)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl From<String> for Box<Error + Send + Sync> {
fn from(err: String) -> Box<Error + Send + Sync> {
impl From<String> for Box<dyn Error + Send + Sync> {
fn from(err: String) -> Box<dyn Error + Send + Sync> {
#[derive(Debug)]
struct StringError(String);

Expand All @@ -185,38 +185,38 @@ impl From<String> for Box<Error + Send + Sync> {
}

#[stable(feature = "string_box_error", since = "1.6.0")]
impl From<String> for Box<Error> {
fn from(str_err: String) -> Box<Error> {
let err1: Box<Error + Send + Sync> = From::from(str_err);
let err2: Box<Error> = err1;
impl From<String> for Box<dyn Error> {
fn from(str_err: String) -> Box<dyn Error> {
let err1: Box<dyn Error + Send + Sync> = From::from(str_err);
let err2: Box<dyn Error> = err1;
err2
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, 'b> From<&'b str> for Box<Error + Send + Sync + 'a> {
fn from(err: &'b str) -> Box<Error + Send + Sync + 'a> {
impl<'a, 'b> From<&'b str> for Box<dyn Error + Send + Sync + 'a> {
fn from(err: &'b str) -> Box<dyn Error + Send + Sync + 'a> {
From::from(String::from(err))
}
}

#[stable(feature = "string_box_error", since = "1.6.0")]
impl<'a> From<&'a str> for Box<Error> {
fn from(err: &'a str) -> Box<Error> {
impl<'a> From<&'a str> for Box<dyn Error> {
fn from(err: &'a str) -> Box<dyn Error> {
From::from(String::from(err))
}
}

#[stable(feature = "cow_box_error", since = "1.22.0")]
impl<'a, 'b> From<Cow<'b, str>> for Box<Error + Send + Sync + 'a> {
fn from(err: Cow<'b, str>) -> Box<Error + Send + Sync + 'a> {
impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a> {
fn from(err: Cow<'b, str>) -> Box<dyn Error + Send + Sync + 'a> {
From::from(String::from(err))
}
}

#[stable(feature = "cow_box_error", since = "1.22.0")]
impl<'a> From<Cow<'a, str>> for Box<Error> {
fn from(err: Cow<'a, str>) -> Box<Error> {
impl<'a> From<Cow<'a, str>> for Box<dyn Error> {
fn from(err: Cow<'a, str>) -> Box<dyn Error> {
From::from(String::from(err))
}
}
Expand Down Expand Up @@ -327,7 +327,7 @@ impl<T: Error> Error for Box<T> {
Error::description(&**self)
}

fn cause(&self) -> Option<&Error> {
fn cause(&self) -> Option<&dyn Error> {
Error::cause(&**self)
}
}
Expand Down Expand Up @@ -368,7 +368,7 @@ impl Error for char::ParseCharError {
}

// copied from any.rs
impl Error + 'static {
impl dyn Error + 'static {
/// Returns true if the boxed type is the same as `T`
#[stable(feature = "error_downcast", since = "1.3.0")]
#[inline]
Expand All @@ -390,7 +390,7 @@ impl Error + 'static {
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
if self.is::<T>() {
unsafe {
Some(&*(self as *const Error as *const T))
Some(&*(self as *const dyn Error as *const T))
}
} else {
None
Expand All @@ -404,68 +404,68 @@ impl Error + 'static {
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
if self.is::<T>() {
unsafe {
Some(&mut *(self as *mut Error as *mut T))
Some(&mut *(self as *mut dyn Error as *mut T))
}
} else {
None
}
}
}

impl Error + 'static + Send {
impl dyn Error + 'static + Send {
/// Forwards to the method defined on the type `Any`.
#[stable(feature = "error_downcast", since = "1.3.0")]
#[inline]
pub fn is<T: Error + 'static>(&self) -> bool {
<Error + 'static>::is::<T>(self)
<dyn Error + 'static>::is::<T>(self)
}

/// Forwards to the method defined on the type `Any`.
#[stable(feature = "error_downcast", since = "1.3.0")]
#[inline]
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
<Error + 'static>::downcast_ref::<T>(self)
<dyn Error + 'static>::downcast_ref::<T>(self)
}

/// Forwards to the method defined on the type `Any`.
#[stable(feature = "error_downcast", since = "1.3.0")]
#[inline]
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
<Error + 'static>::downcast_mut::<T>(self)
<dyn Error + 'static>::downcast_mut::<T>(self)
}
}

impl Error + 'static + Send + Sync {
impl dyn Error + 'static + Send + Sync {
/// Forwards to the method defined on the type `Any`.
#[stable(feature = "error_downcast", since = "1.3.0")]
#[inline]
pub fn is<T: Error + 'static>(&self) -> bool {
<Error + 'static>::is::<T>(self)
<dyn Error + 'static>::is::<T>(self)
}

/// Forwards to the method defined on the type `Any`.
#[stable(feature = "error_downcast", since = "1.3.0")]
#[inline]
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
<Error + 'static>::downcast_ref::<T>(self)
<dyn Error + 'static>::downcast_ref::<T>(self)
}

/// Forwards to the method defined on the type `Any`.
#[stable(feature = "error_downcast", since = "1.3.0")]
#[inline]
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
<Error + 'static>::downcast_mut::<T>(self)
<dyn Error + 'static>::downcast_mut::<T>(self)
}
}

impl Error {
impl dyn Error {
#[inline]
#[stable(feature = "error_downcast", since = "1.3.0")]
/// Attempt to downcast the box to a concrete type.
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Error>> {
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error>> {
if self.is::<T>() {
unsafe {
let raw: *mut Error = Box::into_raw(self);
let raw: *mut dyn Error = Box::into_raw(self);
Ok(Box::from_raw(raw as *mut T))
}
} else {
Expand All @@ -474,30 +474,30 @@ impl Error {
}
}

impl Error + Send {
impl dyn Error + Send {
#[inline]
#[stable(feature = "error_downcast", since = "1.3.0")]
/// Attempt to downcast the box to a concrete type.
pub fn downcast<T: Error + 'static>(self: Box<Self>)
-> Result<Box<T>, Box<Error + Send>> {
let err: Box<Error> = self;
<Error>::downcast(err).map_err(|s| unsafe {
-> Result<Box<T>, Box<dyn Error + Send>> {
let err: Box<dyn Error> = self;
<dyn Error>::downcast(err).map_err(|s| unsafe {
// reapply the Send marker
transmute::<Box<Error>, Box<Error + Send>>(s)
transmute::<Box<dyn Error>, Box<dyn Error + Send>>(s)
})
}
}

impl Error + Send + Sync {
impl dyn Error + Send + Sync {
#[inline]
#[stable(feature = "error_downcast", since = "1.3.0")]
/// Attempt to downcast the box to a concrete type.
pub fn downcast<T: Error + 'static>(self: Box<Self>)
-> Result<Box<T>, Box<Self>> {
let err: Box<Error> = self;
<Error>::downcast(err).map_err(|s| unsafe {
let err: Box<dyn Error> = self;
<dyn Error>::downcast(err).map_err(|s| unsafe {
// reapply the Send+Sync marker
transmute::<Box<Error>, Box<Error + Send + Sync>>(s)
transmute::<Box<dyn Error>, Box<dyn Error + Send + Sync>>(s)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ impl Error for IntoStringError {
"C string contained non-utf8 bytes"
}

fn cause(&self) -> Option<&Error> {
fn cause(&self) -> Option<&dyn Error> {
Some(&self.error)
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/libstd/io/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ enum Repr {
#[derive(Debug)]
struct Custom {
kind: ErrorKind,
error: Box<error::Error+Send+Sync>,
error: Box<dyn error::Error+Send+Sync>,
}

/// A list specifying general categories of I/O error.
Expand Down Expand Up @@ -250,12 +250,12 @@ impl Error {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new<E>(kind: ErrorKind, error: E) -> Error
where E: Into<Box<error::Error+Send+Sync>>
where E: Into<Box<dyn error::Error+Send+Sync>>
{
Self::_new(kind, error.into())
}

fn _new(kind: ErrorKind, error: Box<error::Error+Send+Sync>) -> Error {
fn _new(kind: ErrorKind, error: Box<dyn error::Error+Send+Sync>) -> Error {
Error {
repr: Repr::Custom(Box::new(Custom {
kind,
Expand Down Expand Up @@ -373,7 +373,7 @@ impl Error {
/// }
/// ```
#[stable(feature = "io_error_inner", since = "1.3.0")]
pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> {
pub fn get_ref(&self) -> Option<&(dyn error::Error+Send+Sync+'static)> {
match self.repr {
Repr::Os(..) => None,
Repr::Simple(..) => None,
Expand Down Expand Up @@ -444,7 +444,7 @@ impl Error {
/// }
/// ```
#[stable(feature = "io_error_inner", since = "1.3.0")]
pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> {
pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error+Send+Sync+'static)> {
match self.repr {
Repr::Os(..) => None,
Repr::Simple(..) => None,
Expand Down Expand Up @@ -478,7 +478,7 @@ impl Error {
/// }
/// ```
#[stable(feature = "io_error_inner", since = "1.3.0")]
pub fn into_inner(self) -> Option<Box<error::Error+Send+Sync>> {
pub fn into_inner(self) -> Option<Box<dyn error::Error+Send+Sync>> {
match self.repr {
Repr::Os(..) => None,
Repr::Simple(..) => None,
Expand Down Expand Up @@ -551,7 +551,7 @@ impl error::Error for Error {
}
}

fn cause(&self) -> Option<&error::Error> {
fn cause(&self) -> Option<&dyn error::Error> {
match self.repr {
Repr::Os(..) => None,
Repr::Simple(..) => None,
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1972,7 +1972,7 @@ impl<T: BufRead> BufRead for Take<T> {
}
}

fn read_one_byte(reader: &mut Read) -> Option<Result<u8>> {
fn read_one_byte(reader: &mut dyn Read) -> Option<Result<u8>> {
let mut buf = [0];
loop {
return match reader.read(&mut buf) {
Expand Down Expand Up @@ -2081,7 +2081,7 @@ impl std_error::Error for CharsError {
CharsError::Other(ref e) => std_error::Error::description(e),
}
}
fn cause(&self) -> Option<&std_error::Error> {
fn cause(&self) -> Option<&dyn std_error::Error> {
match *self {
CharsError::NotUtf8 => None,
CharsError::Other(ref e) => e.cause(),
Expand Down
8 changes: 4 additions & 4 deletions src/libstd/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use thread::LocalKey;

/// Stdout used by print! and println! macros
thread_local! {
static LOCAL_STDOUT: RefCell<Option<Box<Write + Send>>> = {
static LOCAL_STDOUT: RefCell<Option<Box<dyn Write + Send>>> = {
RefCell::new(None)
}
}
Expand Down Expand Up @@ -624,7 +624,7 @@ impl<'a> fmt::Debug for StderrLock<'a> {
with a more general mechanism",
issue = "0")]
#[doc(hidden)]
pub fn set_panic(sink: Option<Box<Write + Send>>) -> Option<Box<Write + Send>> {
pub fn set_panic(sink: Option<Box<dyn Write + Send>>) -> Option<Box<dyn Write + Send>> {
use panicking::LOCAL_STDERR;
use mem;
LOCAL_STDERR.with(move |slot| {
Expand All @@ -648,7 +648,7 @@ pub fn set_panic(sink: Option<Box<Write + Send>>) -> Option<Box<Write + Send>> {
with a more general mechanism",
issue = "0")]
#[doc(hidden)]
pub fn set_print(sink: Option<Box<Write + Send>>) -> Option<Box<Write + Send>> {
pub fn set_print(sink: Option<Box<dyn Write + Send>>) -> Option<Box<dyn Write + Send>> {
use mem;
LOCAL_STDOUT.with(move |slot| {
mem::replace(&mut *slot.borrow_mut(), sink)
Expand All @@ -670,7 +670,7 @@ pub fn set_print(sink: Option<Box<Write + Send>>) -> Option<Box<Write + Send>> {
/// However, if the actual I/O causes an error, this function does panic.
fn print_to<T>(
args: fmt::Arguments,
local_s: &'static LocalKey<RefCell<Option<Box<Write+Send>>>>,
local_s: &'static LocalKey<RefCell<Option<Box<dyn Write+Send>>>>,
global_s: fn() -> T,
label: &str,
)
Expand Down
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
// Don't link to std. We are std.
#![no_std]

#![deny(bare_trait_objects)]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]

Expand Down
2 changes: 1 addition & 1 deletion src/libstd/net/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'a> Parser<'a> {
}

// Return result of first successful parser
fn read_or<T>(&mut self, parsers: &mut [Box<FnMut(&mut Parser) -> Option<T> + 'static>])
fn read_or<T>(&mut self, parsers: &mut [Box<dyn FnMut(&mut Parser) -> Option<T> + 'static>])
-> Option<T> {
for pf in parsers {
if let Some(r) = self.read_atomically(|p: &mut Parser| pf(p)) {
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,6 @@ pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> {
/// }
/// ```
#[stable(feature = "resume_unwind", since = "1.9.0")]
pub fn resume_unwind(payload: Box<Any + Send>) -> ! {
pub fn resume_unwind(payload: Box<dyn Any + Send>) -> ! {
panicking::update_count_then_panic(payload)
}
Loading

0 comments on commit 560d807

Please sign in to comment.