Skip to content

Commit

Permalink
Auto merge of #61203 - memoryruins:bare_trait_objects, r=Centril
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed May 29, 2019
2 parents 37d001e + 83660b6 commit 4137901
Show file tree
Hide file tree
Showing 752 changed files with 2,178 additions and 2,132 deletions.
2 changes: 1 addition & 1 deletion src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ declare_lint! {

declare_lint! {
pub BARE_TRAIT_OBJECTS,
Allow,
Warn,
"suggest using `dyn Trait` for trait objects"
}

Expand Down
18 changes: 9 additions & 9 deletions src/libstd/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub trait Error: Debug + Display {
/// "I'm the superhero of errors"
/// }
///
/// fn cause(&self) -> Option<&Error> {
/// fn cause(&self) -> Option<&dyn Error> {
/// Some(&self.side)
/// }
/// }
Expand Down Expand Up @@ -244,7 +244,7 @@ impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
///
/// let an_error = AnError;
/// assert!(0 == mem::size_of_val(&an_error));
/// let a_boxed_error = Box::<Error>::from(an_error);
/// let a_boxed_error = Box::<dyn Error>::from(an_error);
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
/// ```
fn from(err: E) -> Box<dyn Error + 'a> {
Expand Down Expand Up @@ -287,7 +287,7 @@ impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync +
///
/// let an_error = AnError;
/// assert!(0 == mem::size_of_val(&an_error));
/// let a_boxed_error = Box::<Error + Send + Sync>::from(an_error);
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(an_error);
/// assert!(
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
/// ```
Expand All @@ -309,7 +309,7 @@ impl From<String> for Box<dyn Error + Send + Sync> {
/// use std::mem;
///
/// let a_string_error = "a string error".to_string();
/// let a_boxed_error = Box::<Error + Send + Sync>::from(a_string_error);
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_string_error);
/// assert!(
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
/// ```
Expand Down Expand Up @@ -344,7 +344,7 @@ impl From<String> for Box<dyn Error> {
/// use std::mem;
///
/// let a_string_error = "a string error".to_string();
/// let a_boxed_error = Box::<Error>::from(a_string_error);
/// let a_boxed_error = Box::<dyn Error>::from(a_string_error);
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
/// ```
fn from(str_err: String) -> Box<dyn Error> {
Expand All @@ -367,7 +367,7 @@ impl<'a> From<&str> for Box<dyn Error + Send + Sync + 'a> {
/// use std::mem;
///
/// let a_str_error = "a str error";
/// let a_boxed_error = Box::<Error + Send + Sync>::from(a_str_error);
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_str_error);
/// assert!(
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
/// ```
Expand All @@ -389,7 +389,7 @@ impl From<&str> for Box<dyn Error> {
/// use std::mem;
///
/// let a_str_error = "a str error";
/// let a_boxed_error = Box::<Error>::from(a_str_error);
/// let a_boxed_error = Box::<dyn Error>::from(a_str_error);
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
/// ```
fn from(err: &str) -> Box<dyn Error> {
Expand All @@ -412,7 +412,7 @@ impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a> {
/// use std::borrow::Cow;
///
/// let a_cow_str_error = Cow::from("a str error");
/// let a_boxed_error = Box::<Error + Send + Sync>::from(a_cow_str_error);
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_cow_str_error);
/// assert!(
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
/// ```
Expand All @@ -436,7 +436,7 @@ impl<'a> From<Cow<'a, str>> for Box<dyn Error> {
/// use std::borrow::Cow;
///
/// let a_cow_str_error = Cow::from("a str error");
/// let a_boxed_error = Box::<Error>::from(a_cow_str_error);
/// let a_boxed_error = Box::<dyn Error>::from(a_cow_str_error);
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
/// ```
fn from(err: Cow<'a, str>) -> Box<dyn Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1976,7 +1976,7 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// use std::path::Path;
///
/// // one possible implementation of walking a directory only visiting files
/// fn visit_dirs(dir: &Path, cb: &Fn(&DirEntry)) -> io::Result<()> {
/// fn visit_dirs(dir: &Path, cb: &dyn Fn(&DirEntry)) -> io::Result<()> {
/// if dir.is_dir() {
/// for entry in fs::read_dir(dir)? {
/// let entry = entry?;
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-23595-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::ops::{Index};
trait Hierarchy {
type Value;
type ChildKey;
type Children = Index<Self::ChildKey, Output=Hierarchy>;
type Children = dyn Index<Self::ChildKey, Output=dyn Hierarchy>;
//~^ ERROR: the value of the associated types `Value` (from the trait `Hierarchy`), `ChildKey`

fn data(&self) -> Option<(Self::Value, Self::Children)>;
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass-fulldeps/pprust-expr-roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn make_x() -> P<Expr> {
/// Iterate over exprs of depth up to `depth`. The goal is to explore all "interesting"
/// combinations of expression nesting. For example, we explore combinations using `if`, but not
/// `while` or `match`, since those should print and parse in much the same way as `if`.
fn iter_exprs(depth: usize, f: &mut FnMut(P<Expr>)) {
fn iter_exprs(depth: usize, f: &mut dyn FnMut(P<Expr>)) {
if depth == 0 {
f(make_x());
return;
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-pass/alignment-gep-tup-like-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ impl<A:Clone> Invokable<A> for Invoker<A> {
}
}

fn f<A:Clone + 'static>(a: A, b: u16) -> Box<Invokable<A>+'static> {
fn f<A:Clone + 'static>(a: A, b: u16) -> Box<dyn Invokable<A>+'static> {
box Invoker {
a: a,
b: b,
} as (Box<Invokable<A>+'static>)
} as (Box<dyn Invokable<A>+'static>)
}

pub fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// run-pass
#![feature(box_syntax)]

fn pairwise_sub(mut t: Box<DoubleEndedIterator<Item=isize>>) -> isize {
fn pairwise_sub(mut t: Box<dyn DoubleEndedIterator<Item=isize>>) -> isize {
let mut result = 0;
loop {
let front = t.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Foo for char {
fn boo(&self) -> Bar { Bar }
}

fn baz(x: &Foo<A=Bar>) -> Bar {
fn baz(x: &dyn Foo<A=Bar>) -> Bar {
x.boo()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ pub trait Subscriber {

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

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

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

struct MyStruct<'a> {
sub: Box<Subscriber<Input=u64> + 'a>
sub: Box<dyn Subscriber<Input=u64> + 'a>
}

impl<'a> Publisher<'a> for MyStruct<'a> {
type Output = u64;
fn subscribe(&mut self, t : Box<Subscriber<Input=u64> + 'a>) {
fn subscribe(&mut self, t : Box<dyn Subscriber<Input=u64> + 'a>) {
self.sub = t;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ impl double for usize {
}

pub fn main() {
let x: Box<_> = box (box 3usize as Box<double>);
let x: Box<_> = box (box 3usize as Box<dyn double>);
assert_eq!(x.double(), 6);
}
4 changes: 2 additions & 2 deletions src/test/run-pass/borrowck/borrowck-trait-lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::marker;
fn main() {
trait T { fn foo(&self) {} }

fn f<'a, V: T>(v: &'a V) -> &'a T {
v as &'a T
fn f<'a, V: T>(v: &'a V) -> &'a dyn T {
v as &'a dyn T
}
}
10 changes: 5 additions & 5 deletions src/test/run-pass/cast-rfc0401-vtable-kinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ impl<T> Foo<T> for () {}
impl Foo<u32> for u32 { fn foo(&self, _: u32) -> u32 { self+43 } }
impl Bar for () {}

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

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

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

let s = FooS([0,1,2]);
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-pass/cast-rfc0401.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ fn main()
// coercion-cast
let mut it = vec![137].into_iter();
let itr: &mut vec::IntoIter<u32> = &mut it;
assert_eq!((itr as &mut Iterator<Item=u32>).next(), Some(137));
assert_eq!((itr as &mut Iterator<Item=u32>).next(), None);
assert_eq!((itr as &mut dyn Iterator<Item=u32>).next(), Some(137));
assert_eq!((itr as &mut dyn Iterator<Item=u32>).next(), None);

assert_eq!(Some(4u32) as Option<u32>, Some(4u32));
assert_eq!((1u32,2u32) as (u32,u32), (1,2));
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-pass/close-over-big-then-small-data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ impl<A:Clone> Invokable<A> for Invoker<A> {
}
}

fn f<A:Clone + 'static>(a: A, b: u16) -> Box<Invokable<A>+'static> {
fn f<A:Clone + 'static>(a: A, b: u16) -> Box<dyn Invokable<A>+'static> {
box Invoker {
a: a,
b: b,
} as (Box<Invokable<A>+'static>)
} as (Box<dyn Invokable<A>+'static>)
}

pub fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/codegen-object-shim.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() {
assert_eq!((ToString::to_string as fn(&(ToString+'static)) -> String)(&"foo"),
assert_eq!((ToString::to_string as fn(&(dyn ToString+'static)) -> String)(&"foo"),
String::from("foo"));
}
18 changes: 9 additions & 9 deletions src/test/run-pass/coerce/coerce-expect-unsized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ pub fn main() {
let _: Box<[isize]> = Box::new({ [1, 2, 3] });
let _: Box<[isize]> = Box::new(if true { [1, 2, 3] } else { [1, 3, 4] });
let _: Box<[isize]> = Box::new(match true { true => [1, 2, 3], false => [1, 3, 4] });
let _: Box<Fn(isize) -> _> = Box::new({ |x| (x as u8) });
let _: Box<Debug> = Box::new(if true { false } else { true });
let _: Box<Debug> = Box::new(match true { true => 'a', false => 'b' });
let _: Box<dyn Fn(isize) -> _> = Box::new({ |x| (x as u8) });
let _: Box<dyn Debug> = Box::new(if true { false } else { true });
let _: Box<dyn Debug> = Box::new(match true { true => 'a', false => 'b' });

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

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

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

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

let _: Vec<Box<Fn(isize) -> _>> = vec![
let _: Vec<Box<dyn Fn(isize) -> _>> = vec![
Box::new(|x| (x as u8)),
Box::new(|x| (x as i16 as u8)),
];
Expand Down
8 changes: 4 additions & 4 deletions src/test/run-pass/consts/const-trait-to-trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ struct Bar;
impl Trait for Bar {}

fn main() {
let x: &[&Trait] = &[{ &Bar }];
let x: &[&dyn Trait] = &[{ &Bar }];
}

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

// Root of the problem: &Trait -> &Trait coercions:
const FOO: &'static Trait = &Bar;
const BAR: &'static Trait = FOO;
const FOO: &'static dyn Trait = &Bar;
const BAR: &'static dyn Trait = FOO;
fn foo() { let _x = BAR; }
2 changes: 1 addition & 1 deletion src/test/run-pass/deriving/deriving-show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enum Enum {
}

#[derive(Debug)]
struct Pointers(*const Send, *mut Sync);
struct Pointers(*const dyn Send, *mut dyn Sync);

macro_rules! t {
($x:expr, $expected:expr) => {
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/drop/drop-struct-as-object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Drop for Cat {
pub fn main() {
{
let x = box Cat {name: 22};
let nyan: Box<Dummy> = x as Box<Dummy>;
let nyan: Box<dyn Dummy> = x as Box<dyn Dummy>;
}
unsafe {
assert_eq!(value, 22);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() {

// Trait objects.
let a: Bar<i32> = Bar { x: &42 };
let b: Bar<Baz> = a;
let b: Bar<dyn Baz> = a;
unsafe {
assert_eq!((*b.x).get(), 42);
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/run-pass/dynamically-sized-types/dst-coerce-rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ fn main() {
assert_eq!(b[2], 3);

let a: Rc<i32> = Rc::new(42);
let b: Rc<Baz> = a.clone();
let b: Rc<dyn Baz> = a.clone();
assert_eq!(b.get(), 42);

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

let _c = b.clone();

let a: Rc<RefCell<i32>> = Rc::new(RefCell::new(42));
let b: Rc<RefCell<Baz>> = a.clone();
let b: Rc<RefCell<dyn Baz>> = a.clone();
assert_eq!(b.borrow().get(), 42);
// FIXME
let c: Weak<RefCell<Baz>> = Rc::downgrade(&a) as Weak<_>;
let c: Weak<RefCell<dyn Baz>> = Rc::downgrade(&a) as Weak<_>;
}
12 changes: 6 additions & 6 deletions src/test/run-pass/dynamically-sized-types/dst-coercions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ trait T { fn dummy(&self) { } }
impl T for S {}

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

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

// As above, but mut
let x: &mut T = &mut S;
let x: *mut T = &mut S;
let x: &mut dyn T = &mut S;
let x: *mut dyn T = &mut S;

let x: *mut S = &mut S;

// Test we can change the mutability from mut to const.
let x: &T = &mut S;
let x: *const T = &mut S;
let x: &dyn T = &mut S;
let x: *const dyn T = &mut S;
}
Loading

0 comments on commit 4137901

Please sign in to comment.