@@ -5,48 +5,9 @@ generally just *weakening* of types, largely focused around pointers and
5
5
lifetimes. They mostly exist to make Rust "just work" in more cases, and are
6
6
largely harmless.
7
7
8
- Here's all the kinds of coercion:
8
+ For an exhaustive list of all the types of coercions, see the [ Coercion types ] section on the reference.
9
9
10
- Coercion is allowed between the following types:
11
-
12
- * Transitivity: ` T_1 ` to ` T_3 ` where ` T_1 ` coerces to ` T_2 ` and ` T_2 ` coerces to
13
- ` T_3 `
14
- * Pointer Weakening:
15
- * ` &mut T ` to ` &T `
16
- * ` *mut T ` to ` *const T `
17
- * ` &T ` to ` *const T `
18
- * ` &mut T ` to ` *mut T `
19
- * Unsizing: ` T ` to ` U ` if ` T ` implements ` CoerceUnsized<U> `
20
- * Deref coercion: Expression ` &x ` of type ` &T ` to ` &*x ` of type ` &U ` if ` T ` derefs to ` U ` (i.e. ` T: Deref<Target=U> ` )
21
- * Non-capturing closure to a function pointer ([ RFC 1558] , e.g. ` || 8usize ` to ` fn() -> usize ` )
22
-
23
- [ RFC 1558 ] : https://rust-lang.github.io/rfcs/1558-closure-to-fn-coercion.html
24
-
25
- ` CoerceUnsized<Pointer<U>> for Pointer<T> where T: Unsize<U> ` is implemented
26
- for all pointer types (including smart pointers like Box and Rc). Unsize is
27
- only implemented automatically, and enables the following transformations:
28
-
29
- * ` [T; n] ` => ` [T] `
30
- * ` T ` => ` dyn Trait ` where ` T: Trait `
31
- * ` Foo<..., T, ...> ` => ` Foo<..., U, ...> ` where:
32
- * ` T: Unsize<U> `
33
- * ` Foo ` is a struct
34
- * Only the last field of ` Foo ` has type involving ` T `
35
- * ` T ` is not part of the type of any other fields
36
- * ` Bar<T>: Unsize<Bar<U>> ` , if the last field of ` Foo ` has type ` Bar<T> `
37
-
38
- Coercions occur at a * coercion site* . Any location that is explicitly typed
39
- will cause a coercion to its type. If inference is necessary, the coercion will
40
- not be performed. Exhaustively, the coercion sites for an expression ` e ` to
41
- type ` U ` are:
42
-
43
- * let statements, statics, and consts: ` let x: U = e `
44
- * Arguments to functions: ` takes_a_U(e) `
45
- * Any expression that will be returned: ` fn foo() -> U { e } `
46
- * Struct literals: ` Foo { some_u: e } `
47
- * Array literals: ` let x: [U; 10] = [e, ..] `
48
- * Tuple literals: ` let x: (U, ..) = (e, ..) `
49
- * The last expression in a block: ` let x: U = { ..; e } `
10
+ ## Cases where coercions do not perform
50
11
51
12
Note that we do not perform coercions when matching traits (except for
52
13
receivers, see below). If there is an impl for some type ` U ` and ` T ` coerces to
@@ -69,18 +30,17 @@ fn main() {
69
30
70
31
``` text
71
32
error[E0277]: the trait bound `&mut i32: Trait` is not satisfied
72
- --> src/main.rs:9:5
33
+ --> src/main.rs:9:9
73
34
|
35
+ 3 | fn foo<X: Trait>(t: X) {}
36
+ | ----- required by this bound in `foo`
37
+ ...
74
38
9 | foo(t);
75
- | ^^ ^ the trait `Trait` is not implemented for `&mut i32`
39
+ | ^ the trait `Trait` is not implemented for `&mut i32`
76
40
|
77
41
= help: the following implementations were found:
78
42
<&'a i32 as Trait>
79
- note: required by `foo`
80
- --> src/main.rs:3:1
81
- |
82
- 3 | fn foo<X: Trait>(t: X) {}
83
- | ^^^^^^^^^^^^^^^^^^^^^^
84
-
85
- error: aborting due to previous error
43
+ = note: `Trait` is implemented for `&i32`, but not for `&mut i32`
86
44
```
45
+
46
+ [ Coercion types ] : ../reference/type-coercions.html#coercion-types
0 commit comments