Skip to content

Commit 9271fdd

Browse files
committed
refactor unsizing coercion documentation
The old one was quite confusing and also incorrect in a few places.
1 parent dda31c8 commit 9271fdd

File tree

2 files changed

+158
-40
lines changed

2 files changed

+158
-40
lines changed

src/expressions/method-call-expr.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ The following procedure is used:
2525

2626
r[expr.method.candidate-receivers]
2727
The first step is to build a list of candidate receiver types.
28-
Obtain these by repeatedly [dereferencing][dereference] the receiver expression's type, adding each type encountered to the list, then finally attempting an [unsized coercion] at the end, and adding the result type if that is successful.
28+
Obtain these by repeatedly [dereferencing][dereference] the receiver expression's type, adding each type encountered to the list, then finally attempting an [unsizing coercion][coerce.unsize] at the end, and adding the result type if that is successful.
2929

3030
r[expr.method.candidate-receivers-refs]
3131
Then, for each candidate `T`, add `&T` and `&mut T` to the list immediately after `T`.
3232

33-
For instance, if the receiver has type `Box<[i32;2]>`, then the candidate types will be `Box<[i32;2]>`, `&Box<[i32;2]>`, `&mut Box<[i32;2]>`, `[i32; 2]` (by dereferencing), `&[i32; 2]`, `&mut [i32; 2]`, `[i32]` (by unsized coercion), `&[i32]`, and finally `&mut [i32]`.
33+
For instance, if the receiver has type `Box<[i32;2]>`, then the candidate types will be `Box<[i32;2]>`, `&Box<[i32;2]>`, `&mut Box<[i32;2]>`, `[i32; 2]` (by dereferencing), `&[i32; 2]`, `&mut [i32; 2]`, `[i32]` (by unsizing coercion), `&[i32]`, and finally `&mut [i32]`.
3434

3535
r[expr.method.candidate-search]
3636
Then, for each candidate type `T`, search for a [visible] method with a receiver of that type in the following places:
@@ -101,5 +101,4 @@ These cases require a [disambiguating function call syntax] for method and funct
101101
[disambiguating function call syntax]: call-expr.md#disambiguating-function-calls
102102
[dereference]: operator-expr.md#the-dereference-operator
103103
[methods]: ../items/associated-items.md#methods
104-
[unsized coercion]: ../type-coercions.md#unsized-coercions
105104
[`IntoIterator`]: std::iter::IntoIterator

src/type-coercions.md

+156-37
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ r[coerce.types.ref-to-pointer]
134134
r[coerce.types.mut-to-pointer]
135135
* `&mut T` to `*mut T`
136136

137+
r[coerce.types.unsize]
138+
* `T` to `U` if `T: CoerceUnsized<U>`. For example:
139+
```rust
140+
const _: &dyn std::fmt::Display = &0u8; // &u8 -> &dyn Display
141+
const _: &[u32] = &[0, 1, 2, 3, 4, 5]; // &[u32; 4] -> &[u32]
142+
```
143+
144+
See [unsizing coercion](#unsizing-coercions) for more details.
145+
137146
r[coerce.types.deref]
138147
* `&T` or `&mut T` to `&U` if `T` implements `Deref<Target = U>`. For example:
139148

@@ -163,20 +172,6 @@ r[coerce.types.deref]
163172
r[coerce.types.deref-mut]
164173
* `&mut T` to `&mut U` if `T` implements `DerefMut<Target = U>`.
165174

166-
r[coerce.types.unsize]
167-
* TyCtor(`T`) to TyCtor(`U`), where TyCtor(`T`) is one of
168-
- `&T`
169-
- `&mut T`
170-
- `*const T`
171-
- `*mut T`
172-
- `Box<T>`
173-
174-
and where `U` can be obtained from `T` by [unsized coercion](#unsized-coercions).
175-
176-
<!--In the future, coerce_inner will be recursively extended to tuples and
177-
structs. In addition, coercions from subtraits to supertraits will be
178-
added. See [RFC 401] for more details.-->
179-
180175
r[coerce.types.fn]
181176
* Function item types to `fn` pointers
182177

@@ -187,43 +182,165 @@ r[coerce.types.never]
187182
* `!` to any `T`
188183

189184
r[coerce.unsize]
190-
### Unsized Coercions
185+
### Unsizing Coercions
191186

192187
r[coerce.unsize.intro]
193-
The following coercions are called `unsized coercions`, since they
194-
relate to converting types to unsized types, and are permitted in a few
195-
cases where other coercions are not, as described above. They can still happen
196-
anywhere else a coercion can occur.
188+
The following coercions are called "Unsizing coercions", since their targets contain an unsized type.
189+
Unsizing coercions apply to pointer-like types which point to types which can lose some of their compile-time known information (such as size or implemented traits). For example:
190+
191+
```rust
192+
use std::cell::Cell;
193+
194+
fn main() {
195+
// `&[u8; 0]` can be coerced to `&[u8]`.
196+
//
197+
// here `&_` is the pointer-like type,
198+
// `[u8; 0]` is the original pointee,
199+
// and `[u8]` is more erased pointee (it lost the length information).
200+
let _: &[u8] = &[];
201+
202+
trait A: Super {}
203+
impl A for () {}
204+
205+
trait Super {}
206+
impl Super for () {}
207+
208+
// `&()` can be coerced to `&dyn A`, losing the type information.
209+
let a: &dyn A = &();
210+
211+
// `&dyn A` can be coerced to `&dyn Super`,
212+
// loosing the fact that the underlying type (unit) implements `A` too.
213+
let _: &dyn Super = a;
214+
215+
// The same coercions work with other pointer-like types and wrappers over them:
216+
let _: Box<[u8]> = Box::<[u8; 0]>::new([]);
217+
let _: Cell<Box<[u8]>> = Cell::new(Box::<[u8; 0]>::new([]));
218+
219+
// The result of the coercion doesn't *have* to be the same pointer-like type,
220+
// alhtough this is only allowed for certain pairs of pointer-like types.
221+
let _: *const dyn A = &mut ();
222+
}
223+
```
224+
225+
r[coerce.unsize.confusion]
226+
> [!NOTE]
227+
> The term "unsizing" might be quite confusing, since the coercion works on sized types (pointers) and the source pointer might point to an unsized type in the first place (`&dyn A -> &dyn Super` in the example above).
228+
>
229+
> "Unsizing" refers to the main purpose of these coercions --- converting (pointers to) sized types to (pointers to) unsized types. The pointers being not the focus, since unsized types can't exist without them.
230+
231+
r[coerce.unsize.metadata]
232+
When performing unsizing coercion, the pointer metadata type changes. For example, when unsizing `&u32` to `&dyn Debug` metadate type changes from `()` to `DynMetadata<dyn Debug>` (note that exact metadata types are not yet stable). This can also lead to a change in the pointer size -- `&u32` is half the size of `&dyn Debug`.
233+
234+
r[coerce.unsize.traits]
235+
Three traits, [`Unsize`], [`CoerceUnsized`], and [`PinCoerceUnsized`] are used to assist in this process and expose it for library use.
236+
237+
r[coerce.unsize.traits.unsize]
238+
[`Unsize`] represents the fact that the target type is layout compatible with the source type and the pointer metadata of the target type can be derived from the metadata of the source, meaning that a pointer to the source type can be converted to a pointer to the target type. For example `[T; N]` implements `Unsize<[T]>` meaning that you can *unsize* former into the later, allowing coercions such as `&[T; N] -> &[T]`.
239+
240+
r[coerce.unsize.traits.coerce-unsized]
241+
[`CoerceUnsized`] represents the fact that a pointer-like type can be coerced to another pointer-like type, due to `Unsize` being implemented for their pointees. For example, `&T` implements `CoerceUnsized<&U>` when `T: Unsize<U>`.
242+
243+
r[coerce.unsize.traits.pin-coerce-unsized]
244+
[`PinCoerceUnsized`] is an unsafe marker trait for pointer-like types unsizing coercion of which does not break [`Pin`] guarantees. It is a requirement of the [`CoerceUnsized` implementation for `Pin`][coerce.unsize.coerce-unsized-impls.pin-pin]. That is, `&D: PinCoerceUnsized` implies `Pin<&T>: CoerceUnsized<Pin<&U>>`.
197245

198-
r[coerce.unsize.trait]
199-
Two traits, [`Unsize`] and [`CoerceUnsized`], are used
200-
to assist in this process and expose it for library use. The following
201-
coercions are built-ins and, if `T` can be coerced to `U` with one of them, then
202-
an implementation of `Unsize<U>` for `T` will be provided:
246+
The following implementations of [`Unsize`] are built-in:
203247

204248
r[coerce.unsize.slice]
205-
* `[T; n]` to `[T]`.
249+
* `[T; n]: Unsize<[T]>`.
206250

207251
r[coerce.unsize.trait-object]
208-
* `T` to `dyn U`, when `T` implements `U + Sized`, and `U` is [dyn compatible].
252+
* `T: Unsize<dyn U>`, when `T` implements `U + Sized`, and `U` is [dyn compatible].
209253

210254
r[coerce.unsize.trait-upcast]
211-
* `dyn T` to `dyn U`, when `U` is one of `T`'s [supertraits].
255+
* `dyn T: Unsize<dyn U>`, when `U` is one of `T`'s [supertraits].
212256
* This allows dropping auto traits, i.e. `dyn T + Auto` to `dyn U` is allowed.
213257
* 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.
214258

215-
r[coerce.unsized.composite]
216-
* `Foo<..., T, ...>` to `Foo<..., U, ...>`, when:
217-
* `Foo` is a struct.
259+
r[coerce.unsize.composite]
260+
* `S<..., T, ...>: Unsize<S<..., U, ...>>`, when:
261+
* `S` is a struct.
218262
* `T` implements `Unsize<U>`.
219-
* The last field of `Foo` has a type involving `T`.
220-
* If that field has type `Bar<T>`, then `Bar<T>` implements `Unsize<Bar<U>>`.
221-
* T is not part of the type of any other fields.
263+
* The last field of `S` has a type involving `T`. i.e. it's either of `T` or `C<..., T, ...>` where `C` is a type constructor and `T` is only present in it once (`C<T, T>` is disallowed).
264+
* The last field is the *only* field which type involves `T`.
265+
* The type of the last field implements `Unsize<F>` where `F` is the same type with `T` replaced by `U`. i.e. if the field has type `Bar<T>`, then `Bar<T>` implements `Unsize<Bar<U>>`.
266+
267+
r[coerce.unsize.pointer]
268+
Additionally, a type `Foo<T>` can implement `CoerceUnsized<Foo<U>>` when `T` implements `Unsize<U>` or `CoerceUnsized<U>`. This allows it to provide an unsized coercion to `Foo<U>`.
269+
270+
<!-- FIXME: are there more requirements for `CoerceUnsized`? -->
271+
272+
r[coerce.unsize.coerce-unsized-impls]
273+
Types which currently implement `CoerceUnsized<_>` (assuming `T: Unsize<U>`, `'a: 'b`, `A: CoerceUnsized<B>`, `Al: Allocator`):
274+
275+
r[coerce.unsize.coerce-unsized-impls.ref-ref]
276+
* `&'a T: CoerceUnsized<&'b U>`
277+
278+
r[coerce.unsize.coerce-unsized-impls.refmut-ref]
279+
* `&'a mut T: CoerceUnsized<&'b U>`
280+
281+
r[coerce.unsize.coerce-unsized-impls.ref-constptr]
282+
* `&T: CoerceUnsized<*const U>`
283+
284+
r[coerce.unsize.coerce-unsized-impls.refmut-constptr]
285+
* `&mut T: CoerceUnsized<*const U>`
286+
287+
r[coerce.unsize.coerce-unsized-impls.refmut-mutptr]
288+
* `&mut T: CoerceUnsized<*mut U>`
289+
290+
r[coerce.unsize.coerce-unsized-impls.refmut-refmut]
291+
* `&'a mut T: CoerceUnsized<&'a mut U>`
292+
* Note: the lifetimes of source and target must match, which is different from the impls where the target is a [shared reference][type.pointer.reference.shared].
293+
294+
r[coerce.unsize.coerce-unsized-impls.cellref-cellref]
295+
* `core::cell::Ref<'a, A>: CoerceUnsized<core::cell::Ref<'a, B>>`
296+
297+
r[coerce.unsize.coerce-unsized-impls.cellrefmut-cellrefmut]
298+
* `core::cell::RefMut<'a, A>: CoerceUnsized<core::cell::RefMut<'a, B>>`
299+
300+
r[coerce.unsize.coerce-unsized-impls.pin-pin]
301+
* `Pin<A>: CoerceUnsized<Pin<B>>` where `A: PinCoerceUnsized, B: PinCoerceUnsized`
302+
303+
r[coerce.unsize.coerce-unsized-impls.constptr-constptr]
304+
* `*const T: CoerceUnsized<*const U>`
305+
306+
r[coerce.unsize.coerce-unsized-impls.mutptr-constptr]
307+
* `*mut T: CoerceUnsized<*const U>`
308+
309+
r[coerce.unsize.coerce-unsized-impls.mutptr-mutptr]
310+
* `*mut T: CoerceUnsized<*mut U>`
311+
312+
r[coerce.unsize.coerce-unsized-impls.cell-cell]
313+
* `core::cell::Cell<A>: CoerceUnsized<core::cell::Cell<B>>`
314+
315+
r[coerce.unsize.coerce-unsized-impls.refcell-refcell]
316+
* `RefCell<A>: CoerceUnsized<RefCell<B>>`
317+
318+
r[coerce.unsize.coerce-unsized-impls.syncunsafecell-syncunsafecell]
319+
* `SyncUnsafeCell<A>: CoerceUnsized<SyncUnsafeCell<B>>`
320+
321+
r[coerce.unsize.coerce-unsized-impls.unsafecell-unsafecell]
322+
* `UnsafeCell<A>: CoerceUnsized<UnsafeCell<B>>`
323+
324+
r[coerce.unsize.coerce-unsized-impls.nonnull-nonnull]
325+
* `NonNull<T>: CoerceUnsized<NonNull<U>>`
326+
327+
r[coerce.unsize.coerce-unsized-impls.box-box]
328+
* `Box<T, Al>: CoerceUnsized<Box<U, Al>>`
329+
330+
r[coerce.unsize.coerce-unsized-impls.rc-rc]
331+
* `Rc<T, Al>: CoerceUnsized<Rc<U, Al>>`
332+
333+
r[coerce.unsize.coerce-unsized-impls.uniquerc-unieuqrc]
334+
* `UniqueRc<T, Al>: CoerceUnsized<UniqueRc<U, Al>>`
335+
336+
r[coerce.unsize.coerce-unsized-impls.rcweak-rcweak]
337+
* `alloc::rc::Weak<T, Al>: CoerceUnsized<alloc::rc::Weak<U, Al>>`
338+
339+
r[coerce.unsize.coerce-unsized-impls.arc-arc]
340+
* `Arc<T, Al>: CoerceUnsized<Arc<U, Al>>`
222341

223-
r[coerce.unsized.pointer]
224-
Additionally, a type `Foo<T>` can implement `CoerceUnsized<Foo<U>>` when `T`
225-
implements `Unsize<U>` or `CoerceUnsized<Foo<U>>`. This allows it to provide an
226-
unsized coercion to `Foo<U>`.
342+
r[coerce.unsize.coerce-unsized-impls.arcweak-arcweak]
343+
* `alloc::sync::Weak<T, Al>: CoerceUnsized<alloc::sync::Weak<U, Al>>`
227344

228345
> [!NOTE]
229346
> While the definition of the unsized coercions and their implementation has been stabilized, the traits themselves are not yet stable and therefore can't be used directly in stable Rust.
@@ -323,6 +440,8 @@ precisely.
323440
[subtype]: subtyping.md
324441
[dyn compatible]: items/traits.md#dyn-compatibility
325442
[type cast operator]: expressions/operator-expr.md#type-cast-expressions
443+
[`Pin`]: std::pin::Pin
444+
[`PinCoerceUnsized`]: std::pin::PinCoerceUnsized
326445
[`Unsize`]: std::marker::Unsize
327446
[`CoerceUnsized`]: std::ops::CoerceUnsized
328447
[method-call expressions]: expressions/method-call-expr.md

0 commit comments

Comments
 (0)