Skip to content

Commit de2d528

Browse files
authored
Merge pull request #1622 from WaffleLapkin/trait_upcast
Add trait_upcasting related languages changes
2 parents 3e2ffb0 + 2837b28 commit de2d528

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/expressions/operator-expr.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,12 @@ reference types and `mut` or `const` in pointer types.
472472
| [Function pointer] | Integer | Function pointer to address cast |
473473
| Closure \*\*\* | Function pointer | Closure to function pointer cast |
474474

475-
\* or `T` and `V` are compatible unsized types, e.g., both slices, both the same trait object.
475+
\* or `T` and `V` are unsized types with compatible metadata:
476+
477+
* Both slice metadata (`*[u16]` -> `*[u8]`, `*str` -> `*(u8, [u32])`).
478+
* Both the same trait object metadata, modulo dropping auto traits (`*dyn Debug` -> `*(u16, dyn Debug)`, `*dyn Debug + Send` -> `*dyn Debug`).
479+
* **Note**: Adding auto traits is only allowed if the principal trait has the auto trait as a super trait (given `trait T: Send {}`, `*dyn T` -> `*dyn T + Send` is valid, but `*dyn Debug` -> `*dyn Debug + Send` is not).
480+
* **Note**: Generics (including lifetimes) must match (`*dyn T<'a, A>` -> `*dyn T<'b, B>` requires `'a = 'b` and `A = B`).
476481

477482
\*\* only when `m₁` is `mut` or `m₂` is `const`. Casting `mut` reference to
478483
`const` pointer is allowed.

src/type-coercions.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ r[coerce.unsize]
191191

192192
r[coerce.unsize.intro]
193193
The following coercions are called `unsized coercions`, since they
194-
relate to converting sized types to unsized types, and are permitted in a few
194+
relate to converting types to unsized types, and are permitted in a few
195195
cases where other coercions are not, as described above. They can still happen
196196
anywhere else a coercion can occur.
197197

@@ -207,6 +207,11 @@ r[coerce.unsize.slice]
207207
r[coerce.unsize.trait-object]
208208
* `T` to `dyn U`, when `T` implements `U + Sized`, and `U` is [dyn compatible].
209209

210+
r[coerce.unsize.trait-upcast]
211+
* `dyn T` to `dyn U`, when `U` is one of `T`'s [supertraits].
212+
* This allows dropping auto traits, i.e. `dyn T + Auto` to `dyn U` is allowed.
213+
* This allows adding auto traits if the principal trait has the auto trait as a super trait, i.e. given `trait T: U + Send {}`, `dyn T` to `dyn T + Send` or to `dyn U + Send` coercions are allowed.
214+
210215
r[coerce.unsized.composite]
211216
* `Foo<..., T, ...>` to `Foo<..., U, ...>`, when:
212217
* `Foo` is a struct.
@@ -322,3 +327,4 @@ precisely.
322327
[`Unsize`]: std::marker::Unsize
323328
[`CoerceUnsized`]: std::ops::CoerceUnsized
324329
[method-call expressions]: expressions/method-call-expr.md
330+
[supertraits]: items/traits.md#supertraits

0 commit comments

Comments
 (0)