Skip to content

Commit af58854

Browse files
committed
add feature gate tests for DispatchFromDyn
1 parent a2d1cb2 commit af58854

4 files changed

+101
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Check that even though Cell: DispatchFromDyn it remains an invalid self parameter type
2+
3+
use std::cell::Cell;
4+
5+
trait Trait{
6+
fn cell(self: Cell<&Self>); //~ ERROR invalid `self` parameter type: Cell<&Self>
7+
}
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0307]: invalid `self` parameter type: Cell<&Self>
2+
--> $DIR/feature-gate-dispatch-from-dyn-cell.rs:6:19
3+
|
4+
LL | fn cell(self: Cell<&Self>);
5+
| ^^^^^^^^^^^
6+
|
7+
= note: type of `self` must be `Self` or a type that dereferences to it
8+
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0307`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Check that a self parameter type requires a DispatchFromDyn impl to be object safe
2+
3+
#![feature(arbitrary_self_types, unsize, coerce_unsized)]
4+
5+
use std::{
6+
marker::Unsize,
7+
ops::{CoerceUnsized, Deref},
8+
};
9+
10+
struct Ptr<T: ?Sized>(Box<T>);
11+
12+
impl<T: ?Sized> Deref for Ptr<T> {
13+
type Target = T;
14+
15+
fn deref(&self) -> &T {
16+
&*self.0
17+
}
18+
}
19+
20+
impl<T: Unsize<U> + ?Sized, U: ?Sized> CoerceUnsized<Ptr<U>> for Ptr<T> {}
21+
// Because this impl is missing the coercion below fails.
22+
// impl<T: Unsize<U> + ?Sized, U: ?Sized> DispatchFromDyn<Ptr<U>> for Ptr<T> {}
23+
24+
trait Trait {
25+
fn ptr(self: Ptr<Self>);
26+
}
27+
impl Trait for i32 {
28+
fn ptr(self: Ptr<Self>) {}
29+
}
30+
31+
fn main() {
32+
Ptr(Box::new(4)) as Ptr<dyn Trait>;
33+
//~^ ERROR the trait `Trait` cannot be made into an object
34+
//~^^ ERROR the trait `Trait` cannot be made into an object
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error[E0038]: the trait `Trait` cannot be made into an object
2+
--> $DIR/feature-gate-dispatch-from-dyn-missing-impl.rs:32:25
3+
|
4+
LL | fn ptr(self: Ptr<Self>);
5+
| --------- help: consider changing method `ptr`'s `self` parameter to be `&self`: `&Self`
6+
...
7+
LL | Ptr(Box::new(4)) as Ptr<dyn Trait>;
8+
| ^^^^^^^^^^^^^^ `Trait` cannot be made into an object
9+
|
10+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
11+
--> $DIR/feature-gate-dispatch-from-dyn-missing-impl.rs:25:18
12+
|
13+
LL | trait Trait {
14+
| ----- this trait cannot be made into an object...
15+
LL | fn ptr(self: Ptr<Self>);
16+
| ^^^^^^^^^ ...because method `ptr`'s `self` parameter cannot be dispatched on
17+
18+
error[E0038]: the trait `Trait` cannot be made into an object
19+
--> $DIR/feature-gate-dispatch-from-dyn-missing-impl.rs:32:5
20+
|
21+
LL | fn ptr(self: Ptr<Self>);
22+
| --------- help: consider changing method `ptr`'s `self` parameter to be `&self`: `&Self`
23+
...
24+
LL | Ptr(Box::new(4)) as Ptr<dyn Trait>;
25+
| ^^^^^^^^^^^^^^^^ `Trait` cannot be made into an object
26+
|
27+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
28+
--> $DIR/feature-gate-dispatch-from-dyn-missing-impl.rs:25:18
29+
|
30+
LL | trait Trait {
31+
| ----- this trait cannot be made into an object...
32+
LL | fn ptr(self: Ptr<Self>);
33+
| ^^^^^^^^^ ...because method `ptr`'s `self` parameter cannot be dispatched on
34+
note: required for `Ptr<{integer}>` to implement `CoerceUnsized<Ptr<dyn Trait>>`
35+
--> $DIR/feature-gate-dispatch-from-dyn-missing-impl.rs:20:40
36+
|
37+
LL | impl<T: Unsize<U> + ?Sized, U: ?Sized> CoerceUnsized<Ptr<U>> for Ptr<T> {}
38+
| --------- ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^
39+
| |
40+
| unsatisfied trait bound introduced here
41+
= note: required by cast to type `Ptr<dyn Trait>`
42+
43+
error: aborting due to 2 previous errors
44+
45+
For more information about this error, try `rustc --explain E0038`.

0 commit comments

Comments
 (0)