Skip to content

Commit acbf7b6

Browse files
author
QuineDot
committed
Correct reflexive notes and add an example for deeply nested supertype coercion
1 parent d00662b commit acbf7b6

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/dyn-covariance.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ your typical covariant lifetime.
2121

2222
## Unsizing coercions in invariant context
2323

24-
[Earlier we noted that](./dyn-trait-coercions.md#the-reflective-case)
24+
[Earlier we noted that](./dyn-trait-coercions.md#the-reflexive-case)
2525
you can cast a `dyn Trait + 'a` to a `dyn Trait + 'b`, where `'a: 'b`.
2626
Well, isn't that just covariance? Not quite -- when we noted this before,
2727
we were talking about an *unsizing coercion* between two `dyn Trait + '_`.
@@ -103,4 +103,15 @@ fn baz(bx: &mut Box<dyn Trait /* + 'static */>) {
103103
Here we reborrow `**bx` as `&'a mut (dyn Trait + 'static)` for some
104104
short-lived `'a`, and then coerce that to a `&'a mut (dyn Trait + 'a)`.
105105

106+
## Variance in nested context
106107

108+
The supertype coercion of going from `dyn Trait + 'a` to `dyn Trait + 'b`
109+
when `'a: 'b` *can* happen in deeply nested contexts, provided the trait
110+
object is still in a covariant context. So unlike the `*mut` version
111+
above, this version compiles:
112+
```rust
113+
# trait Trait {}
114+
fn foo<'l: 's, 's>(v: *const Box<dyn Trait + 'l>) -> *const Box<dyn Trait + 's> {
115+
v
116+
}
117+
```

src/dyn-trait-coercions.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ requires `dyn Trait + Send`). The coercion is necessary as, again, these are
189189
Although no change to the vtable is required, this coercion can still
190190
[not happen in a nested context.](#no-nested-coercions)
191191

192-
## The reflective case
192+
## The reflexive case
193193

194194
You can cast `dyn Trait` to `dyn Trait`.
195195

@@ -198,8 +198,11 @@ where `'a: 'b`. This is important for
198198
[how borrowing works with `dyn Trait + '_`.](./dyn-covariance.md#unsizing-coercions-in-invariant-context)
199199

200200
As lifetimes are erased during compilation, the vtable is the same regardless of the lifetime.
201-
Despite that, this coercion can still
202-
[not happen in a nested context.](#no-nested-coercions)
201+
Despite that, this unsizing coercion can still [not happen in a nested context.](#no-nested-coercions)
202+
203+
However, [in a future section](http://127.0.0.1:3000/dyn-covariance.html) we'll see
204+
how variance can allow shortening the trait object lifetime even in nested context,
205+
provided that context is also covariant.
203206

204207
## Supertrait upcasting
205208

0 commit comments

Comments
 (0)