Skip to content

Commit 4137901

Browse files
committed
Auto merge of #61203 - memoryruins:bare_trait_objects, r=Centril
Warn on bare_trait_objects by default The `bare_trait_objects` lint is set to `warn` by default. Most ui tests have been updated to use `dyn` to avoid creating noise in stderr files. r? @Centril cc #54910
2 parents 37d001e + 83660b6 commit 4137901

File tree

752 files changed

+2178
-2132
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

752 files changed

+2178
-2132
lines changed

src/librustc/lint/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ declare_lint! {
255255

256256
declare_lint! {
257257
pub BARE_TRAIT_OBJECTS,
258-
Allow,
258+
Warn,
259259
"suggest using `dyn Trait` for trait objects"
260260
}
261261

src/libstd/error.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub trait Error: Debug + Display {
9494
/// "I'm the superhero of errors"
9595
/// }
9696
///
97-
/// fn cause(&self) -> Option<&Error> {
97+
/// fn cause(&self) -> Option<&dyn Error> {
9898
/// Some(&self.side)
9999
/// }
100100
/// }
@@ -244,7 +244,7 @@ impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
244244
///
245245
/// let an_error = AnError;
246246
/// assert!(0 == mem::size_of_val(&an_error));
247-
/// let a_boxed_error = Box::<Error>::from(an_error);
247+
/// let a_boxed_error = Box::<dyn Error>::from(an_error);
248248
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
249249
/// ```
250250
fn from(err: E) -> Box<dyn Error + 'a> {
@@ -287,7 +287,7 @@ impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync +
287287
///
288288
/// let an_error = AnError;
289289
/// assert!(0 == mem::size_of_val(&an_error));
290-
/// let a_boxed_error = Box::<Error + Send + Sync>::from(an_error);
290+
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(an_error);
291291
/// assert!(
292292
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
293293
/// ```
@@ -309,7 +309,7 @@ impl From<String> for Box<dyn Error + Send + Sync> {
309309
/// use std::mem;
310310
///
311311
/// let a_string_error = "a string error".to_string();
312-
/// let a_boxed_error = Box::<Error + Send + Sync>::from(a_string_error);
312+
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_string_error);
313313
/// assert!(
314314
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
315315
/// ```
@@ -344,7 +344,7 @@ impl From<String> for Box<dyn Error> {
344344
/// use std::mem;
345345
///
346346
/// let a_string_error = "a string error".to_string();
347-
/// let a_boxed_error = Box::<Error>::from(a_string_error);
347+
/// let a_boxed_error = Box::<dyn Error>::from(a_string_error);
348348
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
349349
/// ```
350350
fn from(str_err: String) -> Box<dyn Error> {
@@ -367,7 +367,7 @@ impl<'a> From<&str> for Box<dyn Error + Send + Sync + 'a> {
367367
/// use std::mem;
368368
///
369369
/// let a_str_error = "a str error";
370-
/// let a_boxed_error = Box::<Error + Send + Sync>::from(a_str_error);
370+
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_str_error);
371371
/// assert!(
372372
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
373373
/// ```
@@ -389,7 +389,7 @@ impl From<&str> for Box<dyn Error> {
389389
/// use std::mem;
390390
///
391391
/// let a_str_error = "a str error";
392-
/// let a_boxed_error = Box::<Error>::from(a_str_error);
392+
/// let a_boxed_error = Box::<dyn Error>::from(a_str_error);
393393
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
394394
/// ```
395395
fn from(err: &str) -> Box<dyn Error> {
@@ -412,7 +412,7 @@ impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a> {
412412
/// use std::borrow::Cow;
413413
///
414414
/// let a_cow_str_error = Cow::from("a str error");
415-
/// let a_boxed_error = Box::<Error + Send + Sync>::from(a_cow_str_error);
415+
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_cow_str_error);
416416
/// assert!(
417417
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
418418
/// ```
@@ -436,7 +436,7 @@ impl<'a> From<Cow<'a, str>> for Box<dyn Error> {
436436
/// use std::borrow::Cow;
437437
///
438438
/// let a_cow_str_error = Cow::from("a str error");
439-
/// let a_boxed_error = Box::<Error>::from(a_cow_str_error);
439+
/// let a_boxed_error = Box::<dyn Error>::from(a_cow_str_error);
440440
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
441441
/// ```
442442
fn from(err: Cow<'a, str>) -> Box<dyn Error> {

src/libstd/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1976,7 +1976,7 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
19761976
/// use std::path::Path;
19771977
///
19781978
/// // one possible implementation of walking a directory only visiting files
1979-
/// fn visit_dirs(dir: &Path, cb: &Fn(&DirEntry)) -> io::Result<()> {
1979+
/// fn visit_dirs(dir: &Path, cb: &dyn Fn(&DirEntry)) -> io::Result<()> {
19801980
/// if dir.is_dir() {
19811981
/// for entry in fs::read_dir(dir)? {
19821982
/// let entry = entry?;

src/test/compile-fail/issue-23595-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::ops::{Index};
55
trait Hierarchy {
66
type Value;
77
type ChildKey;
8-
type Children = Index<Self::ChildKey, Output=Hierarchy>;
8+
type Children = dyn Index<Self::ChildKey, Output=dyn Hierarchy>;
99
//~^ ERROR: the value of the associated types `Value` (from the trait `Hierarchy`), `ChildKey`
1010

1111
fn data(&self) -> Option<(Self::Value, Self::Children)>;

src/test/run-pass-fulldeps/pprust-expr-roundtrip.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn make_x() -> P<Expr> {
6262
/// Iterate over exprs of depth up to `depth`. The goal is to explore all "interesting"
6363
/// combinations of expression nesting. For example, we explore combinations using `if`, but not
6464
/// `while` or `match`, since those should print and parse in much the same way as `if`.
65-
fn iter_exprs(depth: usize, f: &mut FnMut(P<Expr>)) {
65+
fn iter_exprs(depth: usize, f: &mut dyn FnMut(P<Expr>)) {
6666
if depth == 0 {
6767
f(make_x());
6868
return;

src/test/run-pass/alignment-gep-tup-like-1.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ impl<A:Clone> Invokable<A> for Invoker<A> {
2222
}
2323
}
2424

25-
fn f<A:Clone + 'static>(a: A, b: u16) -> Box<Invokable<A>+'static> {
25+
fn f<A:Clone + 'static>(a: A, b: u16) -> Box<dyn Invokable<A>+'static> {
2626
box Invoker {
2727
a: a,
2828
b: b,
29-
} as (Box<Invokable<A>+'static>)
29+
} as (Box<dyn Invokable<A>+'static>)
3030
}
3131

3232
pub fn main() {

src/test/run-pass/associated-types/associated-types-doubleendediterator-object.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// run-pass
22
#![feature(box_syntax)]
33

4-
fn pairwise_sub(mut t: Box<DoubleEndedIterator<Item=isize>>) -> isize {
4+
fn pairwise_sub(mut t: Box<dyn DoubleEndedIterator<Item=isize>>) -> isize {
55
let mut result = 0;
66
loop {
77
let front = t.next();

src/test/run-pass/associated-types/associated-types-eq-obj.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl Foo for char {
1515
fn boo(&self) -> Bar { Bar }
1616
}
1717

18-
fn baz(x: &Foo<A=Bar>) -> Bar {
18+
fn baz(x: &dyn Foo<A=Bar>) -> Bar {
1919
x.boo()
2020
}
2121

src/test/run-pass/associated-types/associated-types-projection-in-object-type.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ pub trait Subscriber {
1919

2020
pub trait Publisher<'a> {
2121
type Output;
22-
fn subscribe(&mut self, _: Box<Subscriber<Input=Self::Output> + 'a>);
22+
fn subscribe(&mut self, _: Box<dyn Subscriber<Input=Self::Output> + 'a>);
2323
}
2424

2525
pub trait Processor<'a> : Subscriber + Publisher<'a> { }
2626

2727
impl<'a, P> Processor<'a> for P where P : Subscriber + Publisher<'a> { }
2828

2929
struct MyStruct<'a> {
30-
sub: Box<Subscriber<Input=u64> + 'a>
30+
sub: Box<dyn Subscriber<Input=u64> + 'a>
3131
}
3232

3333
impl<'a> Publisher<'a> for MyStruct<'a> {
3434
type Output = u64;
35-
fn subscribe(&mut self, t : Box<Subscriber<Input=u64> + 'a>) {
35+
fn subscribe(&mut self, t : Box<dyn Subscriber<Input=u64> + 'a>) {
3636
self.sub = t;
3737
}
3838
}

src/test/run-pass/autoref-autoderef/autoderef-method-on-trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ impl double for usize {
1111
}
1212

1313
pub fn main() {
14-
let x: Box<_> = box (box 3usize as Box<double>);
14+
let x: Box<_> = box (box 3usize as Box<dyn double>);
1515
assert_eq!(x.double(), 6);
1616
}

src/test/run-pass/borrowck/borrowck-trait-lifetime.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::marker;
1212
fn main() {
1313
trait T { fn foo(&self) {} }
1414

15-
fn f<'a, V: T>(v: &'a V) -> &'a T {
16-
v as &'a T
15+
fn f<'a, V: T>(v: &'a V) -> &'a dyn T {
16+
v as &'a dyn T
1717
}
1818
}

src/test/run-pass/cast-rfc0401-vtable-kinds.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ impl<T> Foo<T> for () {}
1515
impl Foo<u32> for u32 { fn foo(&self, _: u32) -> u32 { self+43 } }
1616
impl Bar for () {}
1717

18-
unsafe fn round_trip_and_call<'a>(t: *const (Foo<u32>+'a)) -> u32 {
19-
let foo_e : *const Foo<u16> = t as *const _;
20-
let r_1 = foo_e as *mut Foo<u32>;
18+
unsafe fn round_trip_and_call<'a>(t: *const (dyn Foo<u32>+'a)) -> u32 {
19+
let foo_e : *const dyn Foo<u16> = t as *const _;
20+
let r_1 = foo_e as *mut dyn Foo<u32>;
2121

2222
(&*r_1).foo(0)
2323
}
@@ -38,8 +38,8 @@ fn tuple_i32_to_u32<T:?Sized>(u: *const (i32, T)) -> *const (u32, T) {
3838

3939
fn main() {
4040
let x = 4u32;
41-
let y : &Foo<u32> = &x;
42-
let fl = unsafe { round_trip_and_call(y as *const Foo<u32>) };
41+
let y : &dyn Foo<u32> = &x;
42+
let fl = unsafe { round_trip_and_call(y as *const dyn Foo<u32>) };
4343
assert_eq!(fl, (43+4));
4444

4545
let s = FooS([0,1,2]);

src/test/run-pass/cast-rfc0401.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ fn main()
2525
// coercion-cast
2626
let mut it = vec![137].into_iter();
2727
let itr: &mut vec::IntoIter<u32> = &mut it;
28-
assert_eq!((itr as &mut Iterator<Item=u32>).next(), Some(137));
29-
assert_eq!((itr as &mut Iterator<Item=u32>).next(), None);
28+
assert_eq!((itr as &mut dyn Iterator<Item=u32>).next(), Some(137));
29+
assert_eq!((itr as &mut dyn Iterator<Item=u32>).next(), None);
3030

3131
assert_eq!(Some(4u32) as Option<u32>, Some(4u32));
3232
assert_eq!((1u32,2u32) as (u32,u32), (1,2));

src/test/run-pass/close-over-big-then-small-data.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ impl<A:Clone> Invokable<A> for Invoker<A> {
2424
}
2525
}
2626

27-
fn f<A:Clone + 'static>(a: A, b: u16) -> Box<Invokable<A>+'static> {
27+
fn f<A:Clone + 'static>(a: A, b: u16) -> Box<dyn Invokable<A>+'static> {
2828
box Invoker {
2929
a: a,
3030
b: b,
31-
} as (Box<Invokable<A>+'static>)
31+
} as (Box<dyn Invokable<A>+'static>)
3232
}
3333

3434
pub fn main() {
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn main() {
2-
assert_eq!((ToString::to_string as fn(&(ToString+'static)) -> String)(&"foo"),
2+
assert_eq!((ToString::to_string as fn(&(dyn ToString+'static)) -> String)(&"foo"),
33
String::from("foo"));
44
}

src/test/run-pass/coerce/coerce-expect-unsized.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ pub fn main() {
1212
let _: Box<[isize]> = Box::new({ [1, 2, 3] });
1313
let _: Box<[isize]> = Box::new(if true { [1, 2, 3] } else { [1, 3, 4] });
1414
let _: Box<[isize]> = Box::new(match true { true => [1, 2, 3], false => [1, 3, 4] });
15-
let _: Box<Fn(isize) -> _> = Box::new({ |x| (x as u8) });
16-
let _: Box<Debug> = Box::new(if true { false } else { true });
17-
let _: Box<Debug> = Box::new(match true { true => 'a', false => 'b' });
15+
let _: Box<dyn Fn(isize) -> _> = Box::new({ |x| (x as u8) });
16+
let _: Box<dyn Debug> = Box::new(if true { false } else { true });
17+
let _: Box<dyn Debug> = Box::new(match true { true => 'a', false => 'b' });
1818

1919
let _: &[isize] = &{ [1, 2, 3] };
2020
let _: &[isize] = &if true { [1, 2, 3] } else { [1, 3, 4] };
2121
let _: &[isize] = &match true { true => [1, 2, 3], false => [1, 3, 4] };
22-
let _: &Fn(isize) -> _ = &{ |x| (x as u8) };
23-
let _: &Debug = &if true { false } else { true };
24-
let _: &Debug = &match true { true => 'a', false => 'b' };
22+
let _: &dyn Fn(isize) -> _ = &{ |x| (x as u8) };
23+
let _: &dyn Debug = &if true { false } else { true };
24+
let _: &dyn Debug = &match true { true => 'a', false => 'b' };
2525

2626
let _: &str = &{ String::new() };
2727
let _: &str = &if true { String::from("...") } else { 5.to_string() };
@@ -31,12 +31,12 @@ pub fn main() {
3131
};
3232

3333
let _: Box<[isize]> = Box::new([1, 2, 3]);
34-
let _: Box<Fn(isize) -> _> = Box::new(|x| (x as u8));
34+
let _: Box<dyn Fn(isize) -> _> = Box::new(|x| (x as u8));
3535

3636
let _: Rc<RefCell<[isize]>> = Rc::new(RefCell::new([1, 2, 3]));
37-
let _: Rc<RefCell<FnMut(isize) -> _>> = Rc::new(RefCell::new(|x| (x as u8)));
37+
let _: Rc<RefCell<dyn FnMut(isize) -> _>> = Rc::new(RefCell::new(|x| (x as u8)));
3838

39-
let _: Vec<Box<Fn(isize) -> _>> = vec![
39+
let _: Vec<Box<dyn Fn(isize) -> _>> = vec![
4040
Box::new(|x| (x as u8)),
4141
Box::new(|x| (x as i16 as u8)),
4242
];

src/test/run-pass/consts/const-trait-to-trait.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ struct Bar;
88
impl Trait for Bar {}
99

1010
fn main() {
11-
let x: &[&Trait] = &[{ &Bar }];
11+
let x: &[&dyn Trait] = &[{ &Bar }];
1212
}
1313

1414
// Issue #25748 - the cast causes an &Encoding -> &Encoding coercion:
1515
pub struct UTF8Encoding;
1616
pub const UTF_8: &'static UTF8Encoding = &UTF8Encoding;
1717
pub trait Encoding {}
1818
impl Encoding for UTF8Encoding {}
19-
pub fn f() -> &'static Encoding { UTF_8 as &'static Encoding }
19+
pub fn f() -> &'static dyn Encoding { UTF_8 as &'static dyn Encoding }
2020

2121
// Root of the problem: &Trait -> &Trait coercions:
22-
const FOO: &'static Trait = &Bar;
23-
const BAR: &'static Trait = FOO;
22+
const FOO: &'static dyn Trait = &Bar;
23+
const BAR: &'static dyn Trait = FOO;
2424
fn foo() { let _x = BAR; }

src/test/run-pass/deriving/deriving-show.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ enum Enum {
1717
}
1818

1919
#[derive(Debug)]
20-
struct Pointers(*const Send, *mut Sync);
20+
struct Pointers(*const dyn Send, *mut dyn Sync);
2121

2222
macro_rules! t {
2323
($x:expr, $expected:expr) => {

src/test/run-pass/drop/drop-struct-as-object.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Drop for Cat {
3030
pub fn main() {
3131
{
3232
let x = box Cat {name: 22};
33-
let nyan: Box<Dummy> = x as Box<Dummy>;
33+
let nyan: Box<dyn Dummy> = x as Box<dyn Dummy>;
3434
}
3535
unsafe {
3636
assert_eq!(value, 22);

src/test/run-pass/dynamically-sized-types/dst-coerce-custom.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn main() {
3636

3737
// Trait objects.
3838
let a: Bar<i32> = Bar { x: &42 };
39-
let b: Bar<Baz> = a;
39+
let b: Bar<dyn Baz> = a;
4040
unsafe {
4141
assert_eq!((*b.x).get(), 42);
4242
}

src/test/run-pass/dynamically-sized-types/dst-coerce-rc.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ fn main() {
2626
assert_eq!(b[2], 3);
2727

2828
let a: Rc<i32> = Rc::new(42);
29-
let b: Rc<Baz> = a.clone();
29+
let b: Rc<dyn Baz> = a.clone();
3030
assert_eq!(b.get(), 42);
3131

3232
let c: Weak<i32> = Rc::downgrade(&a);
33-
let d: Weak<Baz> = c.clone();
33+
let d: Weak<dyn Baz> = c.clone();
3434

3535
let _c = b.clone();
3636

3737
let a: Rc<RefCell<i32>> = Rc::new(RefCell::new(42));
38-
let b: Rc<RefCell<Baz>> = a.clone();
38+
let b: Rc<RefCell<dyn Baz>> = a.clone();
3939
assert_eq!(b.borrow().get(), 42);
4040
// FIXME
41-
let c: Weak<RefCell<Baz>> = Rc::downgrade(&a) as Weak<_>;
41+
let c: Weak<RefCell<dyn Baz>> = Rc::downgrade(&a) as Weak<_>;
4242
}

src/test/run-pass/dynamically-sized-types/dst-coercions.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ trait T { fn dummy(&self) { } }
99
impl T for S {}
1010

1111
pub fn main() {
12-
let x: &T = &S;
12+
let x: &dyn T = &S;
1313
// Test we can convert from &-ptr to *-ptr of trait objects
14-
let x: *const T = &S;
14+
let x: *const dyn T = &S;
1515

1616
// Test we can convert from &-ptr to *-ptr of struct pointer (not DST)
1717
let x: *const S = &S;
1818

1919
// As above, but mut
20-
let x: &mut T = &mut S;
21-
let x: *mut T = &mut S;
20+
let x: &mut dyn T = &mut S;
21+
let x: *mut dyn T = &mut S;
2222

2323
let x: *mut S = &mut S;
2424

2525
// Test we can change the mutability from mut to const.
26-
let x: &T = &mut S;
27-
let x: *const T = &mut S;
26+
let x: &dyn T = &mut S;
27+
let x: *const dyn T = &mut S;
2828
}

0 commit comments

Comments
 (0)