Skip to content

Commit b4738dd

Browse files
committed
Add Sized? bound to type params; add alternative using for Sized? rather than restricting value to reference types.
1 parent 2c25d5f commit b4738dd

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

0000-objects.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Part of the planned, in progress DST work is to allow trait objects where a
1919
trait is expected. Example:
2020

2121
```
22-
fn foo<T: SomeTrait>(y: &T) { ... }
22+
fn foo<Sized? T: SomeTrait>(y: &T) { ... }
2323
2424
fn bar(x: &SomeTrait) {
2525
foo(x)
@@ -45,7 +45,7 @@ trait SomeTrait {
4545
fn foo(&self, other: &Self) { ... } // assume self and other have the same concrete type
4646
}
4747
48-
fn bar<T: SomeTrait>(x: &T, y: &T) {
48+
fn bar<Sized? T: SomeTrait>(x: &T, y: &T) {
4949
x.foo(y); // x and y may have different concrete types, pre-DST we could
5050
// assume that x and y had the same concrete types.
5151
}
@@ -77,7 +77,8 @@ To be precise about object-safety, an object-safe method:
7777
* must not have any type parameters,
7878
* must not take `self` by value,
7979
* 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).
8182

8283
A trait is object-safe if all of its methods are object-safe.
8384

@@ -136,6 +137,14 @@ as part of the check that the actual type parameter satisfies the formal bounds.
136137
We could probably give a different error message if the bounds are met, but the
137138
trait is not object-safe.
138139

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+
139148
# Unresolved questions
140149

141150
N/A

0 commit comments

Comments
 (0)