Skip to content

Commit

Permalink
RFC 2027: Add revisions to some existing object safety tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bovinebuddha committed Jan 12, 2019
1 parent 632e3be commit a78286b
Show file tree
Hide file tree
Showing 25 changed files with 221 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/arbitrary-self-types-not-object-safe.rs:31:32
--> $DIR/arbitrary-self-types-not-object-safe.rs:34:32
|
LL | let x = Rc::new(5usize) as Rc<Foo>;
| ^^^^^^^ the trait `Foo` cannot be made into an object
|
= note: method `foo`'s receiver cannot be dispatched on

error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/arbitrary-self-types-not-object-safe.rs:31:13
--> $DIR/arbitrary-self-types-not-object-safe.rs:34:13
|
LL | let x = Rc::new(5usize) as Rc<Foo>;
| ^^^^^^^^^^^^^^^ the trait `Foo` cannot be made into an object
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/arbitrary-self-types-not-object-safe.rs:34:32
|
LL | let x = Rc::new(5usize) as Rc<Foo>;
| ^^^^^^^ the trait `Foo` cannot be made into an object
|
= note: method `foo`'s receiver cannot be dispatched on
= note: required by cast to type 'std::rc::Rc<dyn Foo>'

error: aborting due to previous error

For more information about this error, try `rustc --explain E0038`.
8 changes: 6 additions & 2 deletions src/test/ui/arbitrary-self-types-not-object-safe.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// revisions: curr object_safe_for_dispatch

#![cfg_attr(object_safe_for_dispatch, feature(object_safe_for_dispatch))]
#![feature(arbitrary_self_types)]

use std::rc::Rc;
Expand Down Expand Up @@ -29,8 +32,9 @@ impl Bar for usize {

fn make_foo() {
let x = Rc::new(5usize) as Rc<Foo>;
//~^ ERROR E0038
//~| ERROR E0038
//[curr]~^ ERROR E0038
//[curr]~| ERROR E0038
//[object_safe_for_dispatch]~^^^ ERROR E0038
}

fn make_bar() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
error[E0277]: the trait bound `std::boxed::Box<{integer}>: std::marker::Copy` is not satisfied
--> $DIR/kindck-inherited-copy-bound.rs:18:5
--> $DIR/kindck-inherited-copy-bound.rs:21:5
|
LL | take_param(&x); //~ ERROR E0277
LL | take_param(&x); //[curr]~ ERROR E0277
| ^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `std::boxed::Box<{integer}>`
|
= note: required because of the requirements on the impl of `Foo` for `std::boxed::Box<{integer}>`
note: required by `take_param`
--> $DIR/kindck-inherited-copy-bound.rs:14:1
--> $DIR/kindck-inherited-copy-bound.rs:17:1
|
LL | fn take_param<T:Foo>(foo: &T) { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/kindck-inherited-copy-bound.rs:24:19
--> $DIR/kindck-inherited-copy-bound.rs:28:19
|
LL | let z = &x as &Foo;
| ^^^^ the trait `Foo` cannot be made into an object
|
= note: the trait cannot require that `Self : Sized`

error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/kindck-inherited-copy-bound.rs:24:13
--> $DIR/kindck-inherited-copy-bound.rs:28:13
|
LL | let z = &x as &Foo;
| ^^ the trait `Foo` cannot be made into an object
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error[E0277]: the trait bound `std::boxed::Box<{integer}>: std::marker::Copy` is not satisfied
--> $DIR/kindck-inherited-copy-bound.rs:21:5
|
LL | take_param(&x); //[curr]~ ERROR E0277
| ^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `std::boxed::Box<{integer}>`
|
= note: required because of the requirements on the impl of `Foo` for `std::boxed::Box<{integer}>`
note: required by `take_param`
--> $DIR/kindck-inherited-copy-bound.rs:17:1
|
LL | fn take_param<T:Foo>(foo: &T) { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/kindck-inherited-copy-bound.rs:28:19
|
LL | let z = &x as &Foo;
| ^^^^ the trait `Foo` cannot be made into an object
|
= note: the trait cannot require that `Self : Sized`
= note: required by cast to type '&dyn Foo'

error: aborting due to 2 previous errors

Some errors occurred: E0038, E0277.
For more information about an error, try `rustc --explain E0038`.
11 changes: 8 additions & 3 deletions src/test/ui/kindck/kindck-inherited-copy-bound.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Test that Copy bounds inherited by trait are checked.
//
// revisions: curr object_safe_for_dispatch

#![cfg_attr(object_safe_for_dispatch, feature(object_safe_for_dispatch))]
#![feature(box_syntax)]

use std::any::Any;
Expand All @@ -15,15 +18,17 @@ fn take_param<T:Foo>(foo: &T) { }

fn a() {
let x: Box<_> = box 3;
take_param(&x); //~ ERROR E0277
take_param(&x); //[curr]~ ERROR E0277
//[object_safe_for_dispatch]~^ ERROR E0277
}

fn b() {
let x: Box<_> = box 3;
let y = &x;
let z = &x as &Foo;
//~^ ERROR E0038
//~| ERROR E0038
//[curr]~^ ERROR E0038
//[curr]~| ERROR E0038
//[object_safe_for_dispatch]~^^^ ERROR E0038
}

fn main() { }
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-associated-consts.rs:9:1
--> $DIR/object-safety-associated-consts.rs:12:1
|
LL | fn make_bar<T:Bar>(t: &T) -> &Bar {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Bar` cannot be made into an object
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-associated-consts.rs:14:5
|
LL | t
| ^ the trait `Bar` cannot be made into an object
|
= note: the trait cannot contain associated consts like `X`
= note: required when trying to coerce from type `&T` to type '&dyn Bar`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0038`.
6 changes: 5 additions & 1 deletion src/test/ui/object-safety/object-safety-associated-consts.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
// Check that we correctly prevent users from making trait objects
// from traits with associated consts.
//
// revisions: curr object_safe_for_dispatch

#![cfg_attr(object_safe_for_dispatch, feature(object_safe_for_dispatch))]

trait Bar {
const X: usize;
}

fn make_bar<T:Bar>(t: &T) -> &Bar {
//~^ ERROR E0038
//[curr]~^ ERROR E0038
t
//[object_safe_for_dispatch]~^ ERROR E0038
}

fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-generics.rs:14:1
--> $DIR/object-safety-generics.rs:18:1
|
LL | fn make_bar<T:Bar>(t: &T) -> &Bar {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Bar` cannot be made into an object
|
= note: method `bar` has generic type parameters

error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-generics.rs:19:1
--> $DIR/object-safety-generics.rs:24:1
|
LL | fn make_bar_explicit<T:Bar>(t: &T) -> &Bar {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Bar` cannot be made into an object
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-generics.rs:20:5
|
LL | t
| ^ the trait `Bar` cannot be made into an object
|
= note: method `bar` has generic type parameters
= note: required when trying to coerce from type `&T` to type '&dyn Bar`

error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-generics.rs:26:10
|
LL | t as &Bar
| ^^^^ the trait `Bar` cannot be made into an object
|
= note: method `bar` has generic type parameters
= note: required by cast to type '&dyn Bar'

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0038`.
10 changes: 8 additions & 2 deletions src/test/ui/object-safety/object-safety-generics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Check that we correctly prevent users from making trait objects
// from traits with generic methods, unless `where Self : Sized` is
// present.
// revisions: curr object_safe_for_dispatch

#![cfg_attr(object_safe_for_dispatch, feature(object_safe_for_dispatch))]


trait Bar {
fn bar<T>(&self, t: T);
Expand All @@ -12,13 +16,15 @@ trait Quux {
}

fn make_bar<T:Bar>(t: &T) -> &Bar {
//~^ ERROR E0038
//[curr]~^ ERROR E0038
t
//[object_safe_for_dispatch]~^ ERROR E0038
}

fn make_bar_explicit<T:Bar>(t: &T) -> &Bar {
//~^ ERROR E0038
//[curr]~^ ERROR E0038
t as &Bar
//[object_safe_for_dispatch]~^ ERROR E0038
}

fn make_quux<T:Quux>(t: &T) -> &Quux {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-mentions-Self.rs:17:1
--> $DIR/object-safety-mentions-Self.rs:22:1
|
LL | fn make_bar<T:Bar>(t: &T) -> &Bar {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Bar` cannot be made into an object
|
= note: method `bar` references the `Self` type in its arguments or return type

error[E0038]: the trait `Baz` cannot be made into an object
--> $DIR/object-safety-mentions-Self.rs:22:1
--> $DIR/object-safety-mentions-Self.rs:28:1
|
LL | fn make_baz<T:Baz>(t: &T) -> &Baz {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Baz` cannot be made into an object
|
= note: method `bar` references the `Self` type in its arguments or return type
= note: method `baz` references the `Self` type in its arguments or return type

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-mentions-Self.rs:24:5
|
LL | t
| ^ the trait `Bar` cannot be made into an object
|
= note: method `bar` references the `Self` type in its arguments or return type
= note: required when trying to coerce from type `&T` to type '&dyn Bar`

error[E0038]: the trait `Baz` cannot be made into an object
--> $DIR/object-safety-mentions-Self.rs:30:5
|
LL | t
| ^ the trait `Baz` cannot be made into an object
|
= note: method `baz` references the `Self` type in its arguments or return type
= note: required when trying to coerce from type `&T` to type '&dyn Baz`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0038`.
22 changes: 14 additions & 8 deletions src/test/ui/object-safety/object-safety-mentions-Self.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
// Check that we correctly prevent users from making trait objects
// form traits that make use of `Self` in an argument or return
// position, unless `where Self : Sized` is present..
//
// revisions: curr object_safe_for_dispatch

#![cfg_attr(object_safe_for_dispatch, feature(object_safe_for_dispatch))]


trait Bar {
fn bar(&self, x: &Self);
}

trait Baz {
fn bar(&self) -> Self;
fn baz(&self) -> Self;
}

trait Quux {
fn get(&self, s: &Self) -> Self where Self : Sized;
fn quux(&self, s: &Self) -> Self where Self : Sized;
}

fn make_bar<T:Bar>(t: &T) -> &Bar {
//~^ ERROR E0038
loop { }
//[curr]~^ ERROR E0038
t
//[object_safe_for_dispatch]~^ ERROR E0038
}

fn make_baz<T:Baz>(t: &T) -> &Baz {
//~^ ERROR E0038
//[curr]~^ ERROR E0038
t
//[object_safe_for_dispatch]~^ ERROR E0038
}

fn make_quux<T:Quux>(t: &T) -> &Quux {
t
}

fn make_quux_explicit<T:Quux>(t: &T) -> &Quux {
t as &Quux
t as &dyn Quux
}

fn main() {
}
fn main() {}
11 changes: 11 additions & 0 deletions src/test/ui/object-safety/object-safety-no-static.curr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/object-safety-no-static.rs:12:1
|
LL | fn diverges() -> Box<dyn Foo> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` cannot be made into an object
|
= note: method `foo` has no receiver

error: aborting due to previous error

For more information about this error, try `rustc --explain E0038`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/object-safety-no-static.rs:22:27
|
LL | let b: Box<dyn Foo> = Box::new(Bar);
| ^^^^^^^^^^^^^ the trait `Foo` cannot be made into an object
|
= note: method `foo` has no receiver
= note: required when trying to coerce from type `std::boxed::Box<Bar>` to type 'std::boxed::Box<dyn Foo>`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0038`.
16 changes: 13 additions & 3 deletions src/test/ui/object-safety/object-safety-no-static.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
// Check that we correctly prevent users from making trait objects
// from traits with static methods.
//
// revisions: curr object_safe_for_dispatch

#![cfg_attr(object_safe_for_dispatch, feature(object_safe_for_dispatch))]

trait Foo {
fn foo();
fn foo() {}
}

fn foo_implicit<T:Foo+'static>(b: Box<T>) -> Box<Foo+'static> {
//~^ ERROR E0038
fn diverges() -> Box<dyn Foo> {
//[curr]~^ ERROR E0038
loop { }
}

struct Bar;

impl Foo for Bar {}

fn main() {
let b: Box<dyn Foo> = Box::new(Bar);
//[object_safe_for_dispatch]~^ ERROR E0038
}
Loading

0 comments on commit a78286b

Please sign in to comment.