@@ -19,7 +19,7 @@ Part of the planned, in progress DST work is to allow trait objects where a
19
19
trait is expected. Example:
20
20
21
21
```
22
- fn foo<T: SomeTrait>(y: &T) { ... }
22
+ fn foo<Sized? T: SomeTrait>(y: &T) { ... }
23
23
24
24
fn bar(x: &SomeTrait) {
25
25
foo(x)
@@ -45,7 +45,7 @@ trait SomeTrait {
45
45
fn foo(&self, other: &Self) { ... } // assume self and other have the same concrete type
46
46
}
47
47
48
- fn bar<T: SomeTrait>(x: &T, y: &T) {
48
+ fn bar<Sized? T: SomeTrait>(x: &T, y: &T) {
49
49
x.foo(y); // x and y may have different concrete types, pre-DST we could
50
50
// assume that x and y had the same concrete types.
51
51
}
@@ -77,7 +77,8 @@ To be precise about object-safety, an object-safe method:
77
77
* must not have any type parameters,
78
78
* must not take ` self ` by value,
79
79
* must not use ` Self ` (in the future, where we allow arbitrary types for the
80
- receiver, ` Self ` may only be used for the type of the receiver).
80
+ receiver, ` Self ` may only be used for the type of the receiver and only where
81
+ we allow ` Sized? ` types).
81
82
82
83
A trait is object-safe if all of its methods are object-safe.
83
84
@@ -136,6 +137,14 @@ as part of the check that the actual type parameter satisfies the formal bounds.
136
137
We could probably give a different error message if the bounds are met, but the
137
138
trait is not object-safe.
138
139
140
+ Rather than the restriction on taking ` self ` by value, we could require a trait
141
+ is ` for Sized? ` in order to be object safe. The purpose of forbidding self by
142
+ value is to enforce that we always have statically known size and that we have a
143
+ vtable for dynamic dispatch. If the programmer were going to manually provide
144
+ ` impl ` s for each trait, we would require the ` Sized? ` bound on the trait to
145
+ ensure that ` self ` was not dereferenced. However, with the compiler-driven
146
+ approach, this is not necessary.
147
+
139
148
# Unresolved questions
140
149
141
150
N/A
0 commit comments