You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: src/expressions/method-call-expr.md
+2-3
Original file line number
Diff line number
Diff line change
@@ -25,12 +25,12 @@ The following procedure is used:
25
25
26
26
r[expr.method.candidate-receivers]
27
27
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.
29
29
30
30
r[expr.method.candidate-receivers-refs]
31
31
Then, for each candidate `T`, add `&T` and `&mut T` to the list immediately after `T`.
32
32
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]`.
34
34
35
35
r[expr.method.candidate-search]
36
36
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
See [unsizingcoercion](#unsizing-coercions) formoredetails.
145
+
137
146
r[coerce.types.deref]
138
147
* `&T` or `&mutT` to `&U` if `T` implements `Deref<Target=U>`.Forexample:
139
148
@@ -163,20 +172,6 @@ r[coerce.types.deref]
163
172
r[coerce.types.deref-mut]
164
173
*`&mut T` to `&mut U` if `T` implements `DerefMut<Target = U>`.
165
174
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
-
180
175
r[coerce.types.fn]
181
176
* Function item types to `fn` pointers
182
177
@@ -187,43 +182,165 @@ r[coerce.types.never]
187
182
*`!` to any `T`
188
183
189
184
r[coerce.unsize]
190
-
### Unsized Coercions
185
+
### Unsizing Coercions
191
186
192
187
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
+
usestd::cell::Cell;
193
+
194
+
fnmain() {
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
+
traitA:Super {}
203
+
implAfor () {}
204
+
205
+
traitSuper {}
206
+
implSuperfor () {}
207
+
208
+
// `&()` can be coerced to `&dyn A`, losing the type information.
209
+
leta:&dynA=&();
210
+
211
+
// `&dyn A` can be coerced to `&dyn Super`,
212
+
// loosing the fact that the underlying type (unit) implements `A` too.
213
+
let_:&dynSuper=a;
214
+
215
+
// The same coercions work with other pointer-like types and wrappers over them:
// 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_:*constdynA=&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>>`.
197
245
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:
203
247
204
248
r[coerce.unsize.slice]
205
-
*`[T; n]` to `[T]`.
249
+
*`[T; n]: Unsize<[T]>`.
206
250
207
251
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].
209
253
210
254
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].
212
256
* This allows dropping auto traits, i.e. `dyn T + Auto` to `dyn U` is allowed.
213
257
* 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
258
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.
218
262
*`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`):
* 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].
> 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.
0 commit comments