Skip to content

Commit efad6f1

Browse files
authored
Rollup merge of rust-lang#52221 - ljedrz:dyn_libstd, r=Kimundi
Deny bare trait objects in src/libstd Enforce #![deny(bare_trait_objects)] in src/libstd.
2 parents 530ddcd + 1915cd1 commit efad6f1

28 files changed

+115
-114
lines changed

src/libstd/error.rs

+45-45
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub trait Error: Debug + Display {
138138
/// }
139139
/// ```
140140
#[stable(feature = "rust1", since = "1.0.0")]
141-
fn cause(&self) -> Option<&Error> { None }
141+
fn cause(&self) -> Option<&dyn Error> { None }
142142

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

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

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

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

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

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

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

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

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

217217
#[stable(feature = "cow_box_error", since = "1.22.0")]
218-
impl<'a> From<Cow<'a, str>> for Box<Error> {
219-
fn from(err: Cow<'a, str>) -> Box<Error> {
218+
impl<'a> From<Cow<'a, str>> for Box<dyn Error> {
219+
fn from(err: Cow<'a, str>) -> Box<dyn Error> {
220220
From::from(String::from(err))
221221
}
222222
}
@@ -327,7 +327,7 @@ impl<T: Error> Error for Box<T> {
327327
Error::description(&**self)
328328
}
329329

330-
fn cause(&self) -> Option<&Error> {
330+
fn cause(&self) -> Option<&dyn Error> {
331331
Error::cause(&**self)
332332
}
333333
}
@@ -368,7 +368,7 @@ impl Error for char::ParseCharError {
368368
}
369369

370370
// copied from any.rs
371-
impl Error + 'static {
371+
impl dyn Error + 'static {
372372
/// Returns true if the boxed type is the same as `T`
373373
#[stable(feature = "error_downcast", since = "1.3.0")]
374374
#[inline]
@@ -390,7 +390,7 @@ impl Error + 'static {
390390
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
391391
if self.is::<T>() {
392392
unsafe {
393-
Some(&*(self as *const Error as *const T))
393+
Some(&*(self as *const dyn Error as *const T))
394394
}
395395
} else {
396396
None
@@ -404,68 +404,68 @@ impl Error + 'static {
404404
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
405405
if self.is::<T>() {
406406
unsafe {
407-
Some(&mut *(self as *mut Error as *mut T))
407+
Some(&mut *(self as *mut dyn Error as *mut T))
408408
}
409409
} else {
410410
None
411411
}
412412
}
413413
}
414414

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

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

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

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

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

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

461-
impl Error {
461+
impl dyn Error {
462462
#[inline]
463463
#[stable(feature = "error_downcast", since = "1.3.0")]
464464
/// Attempt to downcast the box to a concrete type.
465-
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Error>> {
465+
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error>> {
466466
if self.is::<T>() {
467467
unsafe {
468-
let raw: *mut Error = Box::into_raw(self);
468+
let raw: *mut dyn Error = Box::into_raw(self);
469469
Ok(Box::from_raw(raw as *mut T))
470470
}
471471
} else {
@@ -474,30 +474,30 @@ impl Error {
474474
}
475475
}
476476

477-
impl Error + Send {
477+
impl dyn Error + Send {
478478
#[inline]
479479
#[stable(feature = "error_downcast", since = "1.3.0")]
480480
/// Attempt to downcast the box to a concrete type.
481481
pub fn downcast<T: Error + 'static>(self: Box<Self>)
482-
-> Result<Box<T>, Box<Error + Send>> {
483-
let err: Box<Error> = self;
484-
<Error>::downcast(err).map_err(|s| unsafe {
482+
-> Result<Box<T>, Box<dyn Error + Send>> {
483+
let err: Box<dyn Error> = self;
484+
<dyn Error>::downcast(err).map_err(|s| unsafe {
485485
// reapply the Send marker
486-
transmute::<Box<Error>, Box<Error + Send>>(s)
486+
transmute::<Box<dyn Error>, Box<dyn Error + Send>>(s)
487487
})
488488
}
489489
}
490490

491-
impl Error + Send + Sync {
491+
impl dyn Error + Send + Sync {
492492
#[inline]
493493
#[stable(feature = "error_downcast", since = "1.3.0")]
494494
/// Attempt to downcast the box to a concrete type.
495495
pub fn downcast<T: Error + 'static>(self: Box<Self>)
496496
-> Result<Box<T>, Box<Self>> {
497-
let err: Box<Error> = self;
498-
<Error>::downcast(err).map_err(|s| unsafe {
497+
let err: Box<dyn Error> = self;
498+
<dyn Error>::downcast(err).map_err(|s| unsafe {
499499
// reapply the Send+Sync marker
500-
transmute::<Box<Error>, Box<Error + Send + Sync>>(s)
500+
transmute::<Box<dyn Error>, Box<dyn Error + Send + Sync>>(s)
501501
})
502502
}
503503
}
@@ -533,13 +533,13 @@ mod tests {
533533
#[test]
534534
fn downcasting() {
535535
let mut a = A;
536-
let a = &mut a as &mut (Error + 'static);
536+
let a = &mut a as &mut (dyn Error + 'static);
537537
assert_eq!(a.downcast_ref::<A>(), Some(&A));
538538
assert_eq!(a.downcast_ref::<B>(), None);
539539
assert_eq!(a.downcast_mut::<A>(), Some(&mut A));
540540
assert_eq!(a.downcast_mut::<B>(), None);
541541

542-
let a: Box<Error> = Box::new(A);
542+
let a: Box<dyn Error> = Box::new(A);
543543
match a.downcast::<B>() {
544544
Ok(..) => panic!("expected error"),
545545
Err(e) => assert_eq!(*e.downcast::<A>().unwrap(), A),

src/libstd/ffi/c_str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ impl Error for IntoStringError {
891891
"C string contained non-utf8 bytes"
892892
}
893893

894-
fn cause(&self) -> Option<&Error> {
894+
fn cause(&self) -> Option<&dyn Error> {
895895
Some(&self.error)
896896
}
897897
}

src/libstd/io/error.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ enum Repr {
8383
#[derive(Debug)]
8484
struct Custom {
8585
kind: ErrorKind,
86-
error: Box<error::Error+Send+Sync>,
86+
error: Box<dyn error::Error+Send+Sync>,
8787
}
8888

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

258-
fn _new(kind: ErrorKind, error: Box<error::Error+Send+Sync>) -> Error {
258+
fn _new(kind: ErrorKind, error: Box<dyn error::Error+Send+Sync>) -> Error {
259259
Error {
260260
repr: Repr::Custom(Box::new(Custom {
261261
kind,
@@ -373,7 +373,7 @@ impl Error {
373373
/// }
374374
/// ```
375375
#[stable(feature = "io_error_inner", since = "1.3.0")]
376-
pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> {
376+
pub fn get_ref(&self) -> Option<&(dyn error::Error+Send+Sync+'static)> {
377377
match self.repr {
378378
Repr::Os(..) => None,
379379
Repr::Simple(..) => None,
@@ -444,7 +444,7 @@ impl Error {
444444
/// }
445445
/// ```
446446
#[stable(feature = "io_error_inner", since = "1.3.0")]
447-
pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> {
447+
pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error+Send+Sync+'static)> {
448448
match self.repr {
449449
Repr::Os(..) => None,
450450
Repr::Simple(..) => None,
@@ -478,7 +478,7 @@ impl Error {
478478
/// }
479479
/// ```
480480
#[stable(feature = "io_error_inner", since = "1.3.0")]
481-
pub fn into_inner(self) -> Option<Box<error::Error+Send+Sync>> {
481+
pub fn into_inner(self) -> Option<Box<dyn error::Error+Send+Sync>> {
482482
match self.repr {
483483
Repr::Os(..) => None,
484484
Repr::Simple(..) => None,
@@ -551,7 +551,7 @@ impl error::Error for Error {
551551
}
552552
}
553553

554-
fn cause(&self) -> Option<&error::Error> {
554+
fn cause(&self) -> Option<&dyn error::Error> {
555555
match self.repr {
556556
Repr::Os(..) => None,
557557
Repr::Simple(..) => None,

src/libstd/io/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1972,7 +1972,7 @@ impl<T: BufRead> BufRead for Take<T> {
19721972
}
19731973
}
19741974

1975-
fn read_one_byte(reader: &mut Read) -> Option<Result<u8>> {
1975+
fn read_one_byte(reader: &mut dyn Read) -> Option<Result<u8>> {
19761976
let mut buf = [0];
19771977
loop {
19781978
return match reader.read(&mut buf) {
@@ -2081,7 +2081,7 @@ impl std_error::Error for CharsError {
20812081
CharsError::Other(ref e) => std_error::Error::description(e),
20822082
}
20832083
}
2084-
fn cause(&self) -> Option<&std_error::Error> {
2084+
fn cause(&self) -> Option<&dyn std_error::Error> {
20852085
match *self {
20862086
CharsError::NotUtf8 => None,
20872087
CharsError::Other(ref e) => e.cause(),

src/libstd/io/stdio.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use thread::LocalKey;
2121

2222
/// Stdout used by print! and println! macros
2323
thread_local! {
24-
static LOCAL_STDOUT: RefCell<Option<Box<Write + Send>>> = {
24+
static LOCAL_STDOUT: RefCell<Option<Box<dyn Write + Send>>> = {
2525
RefCell::new(None)
2626
}
2727
}
@@ -624,7 +624,7 @@ impl<'a> fmt::Debug for StderrLock<'a> {
624624
with a more general mechanism",
625625
issue = "0")]
626626
#[doc(hidden)]
627-
pub fn set_panic(sink: Option<Box<Write + Send>>) -> Option<Box<Write + Send>> {
627+
pub fn set_panic(sink: Option<Box<dyn Write + Send>>) -> Option<Box<dyn Write + Send>> {
628628
use panicking::LOCAL_STDERR;
629629
use mem;
630630
LOCAL_STDERR.with(move |slot| {
@@ -648,7 +648,7 @@ pub fn set_panic(sink: Option<Box<Write + Send>>) -> Option<Box<Write + Send>> {
648648
with a more general mechanism",
649649
issue = "0")]
650650
#[doc(hidden)]
651-
pub fn set_print(sink: Option<Box<Write + Send>>) -> Option<Box<Write + Send>> {
651+
pub fn set_print(sink: Option<Box<dyn Write + Send>>) -> Option<Box<dyn Write + Send>> {
652652
use mem;
653653
LOCAL_STDOUT.with(move |slot| {
654654
mem::replace(&mut *slot.borrow_mut(), sink)
@@ -670,7 +670,7 @@ pub fn set_print(sink: Option<Box<Write + Send>>) -> Option<Box<Write + Send>> {
670670
/// However, if the actual I/O causes an error, this function does panic.
671671
fn print_to<T>(
672672
args: fmt::Arguments,
673-
local_s: &'static LocalKey<RefCell<Option<Box<Write+Send>>>>,
673+
local_s: &'static LocalKey<RefCell<Option<Box<dyn Write+Send>>>>,
674674
global_s: fn() -> T,
675675
label: &str,
676676
)

src/libstd/io/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ mod tests {
223223
assert_eq!(copy(&mut r, &mut w).unwrap(), 4);
224224

225225
let mut r = repeat(0).take(1 << 17);
226-
assert_eq!(copy(&mut r as &mut Read, &mut w as &mut Write).unwrap(), 1 << 17);
226+
assert_eq!(copy(&mut r as &mut dyn Read, &mut w as &mut dyn Write).unwrap(), 1 << 17);
227227
}
228228

229229
#[test]

src/libstd/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
// Don't link to std. We are std.
222222
#![no_std]
223223

224+
#![deny(bare_trait_objects)]
224225
#![deny(missing_docs)]
225226
#![deny(missing_debug_implementations)]
226227

0 commit comments

Comments
 (0)