@@ -3661,47 +3661,71 @@ sites are:
3661
3661
3662
3662
* ` let ` statements where an explicit type is given.
3663
3663
3664
- In ` let _: U = e; ` , ` e ` is coerced to have type ` U ` .
3664
+ For example, ` 128 ` is coerced to have type ` i8 ` in the following:
3665
+
3666
+ ``` rust
3667
+ let _ : i8 = 128 ;
3668
+ ```
3665
3669
3666
3670
* ` static ` and ` const ` statements (similar to ` let ` statements).
3667
3671
3668
- * arguments for function calls.
3672
+ * Arguments for function calls
3673
+
3674
+ The value being coerced is the actual parameter, and it is coerced to
3675
+ the type of the formal parameter.
3676
+
3677
+ For example, ` 128 ` is coerced to have type ` i8 ` in the following:
3678
+
3679
+ ``` rust
3680
+ fn bar (_ : i8 ) { }
3669
3681
3670
- The value being coerced is the
3671
- actual parameter and it is coerced to the type of the formal parameter. For
3672
- example, let ` foo ` be defined as ` fn foo(x: U) { ... } ` and call it as
3673
- ` foo(e); ` . Then ` e ` is coerced to have type ` U ` ;
3682
+ fn main () {
3683
+ bar ( 128 );
3684
+ }
3685
+ ```
3674
3686
3675
- * instantiations of struct or variant fields.
3687
+ * Instantiations of struct or variant fields
3676
3688
3677
- Assume we have a `struct
3678
- Foo { x: U }` and instantiate it as ` Foo { x: e }` . Then ` e` is coerced to
3679
- have type ` U ` .
3689
+ For example, ` 128 ` is coerced to have type ` i8 ` in the following:
3680
3690
3681
- * function results (either the final line of a block if it is not semicolon
3682
- terminated or any expression in a ` return ` statement).
3691
+ ``` rust
3692
+ struct Foo { x : i8 }
3683
3693
3684
- In `fn foo() -> U { e }`, `e` is coerced to to have type `U`.
3694
+ fn main () {
3695
+ Foo { x : 128 };
3696
+ }
3697
+ ```
3698
+
3699
+ * Function results, either the final line of a block if it is not
3700
+ semicolon-terminated or any expression in a ` return ` statement
3701
+
3702
+ For example, ` 128 ` is coerced to have type ` i8 ` in the following:
3703
+
3704
+ ``` rust
3705
+ fn foo () -> i8 {
3706
+ 128
3707
+ }
3708
+ ```
3685
3709
3686
3710
If the expression in one of these coercion sites is a coercion-propagating
3687
3711
expression, then the relevant sub-expressions in that expression are also
3688
3712
coercion sites. Propagation recurses from these new coercion sites.
3689
3713
Propagating expressions and their relevant sub-expressions are:
3690
3714
3691
- * array literals, where the array has type ` [U; n] ` . Each sub-expression in
3715
+ * Array literals, where the array has type ` [U; n] ` . Each sub-expression in
3692
3716
the array literal is a coercion site for coercion to type ` U ` .
3693
3717
3694
- * array literals with repeating syntax, where the array has type ` [U; n] ` . The
3718
+ * Array literals with repeating syntax, where the array has type ` [U; n] ` . The
3695
3719
repeated sub-expression is a coercion site for coercion to type ` U ` .
3696
3720
3697
- * tuples , where a tuple is a coercion site to type ` (U_0, U_1, ..., U_n) ` .
3721
+ * Tuples , where a tuple is a coercion site to type ` (U_0, U_1, ..., U_n) ` .
3698
3722
Each sub-expression is a coercion site to the respective type, e.g. the
3699
3723
zeroth sub-expression is a coercion site to type ` U_0 ` .
3700
3724
3701
- * parenthesised sub-expressions (` (e) ` ). If the expression has type ` U ` , then
3725
+ * Parenthesised sub-expressions (` (e) ` ): if the expression has type ` U ` , then
3702
3726
the sub-expression is a coercion site to ` U ` .
3703
3727
3704
- * blocks. If a block has type ` U ` , then the last expression in the block (if
3728
+ * Blocks: if a block has type ` U ` , then the last expression in the block (if
3705
3729
it is not semicolon-terminated) is a coercion site to ` U ` . This includes
3706
3730
blocks which are part of control flow statements, such as ` if ` /` else ` , if
3707
3731
the block has a known type.
@@ -3710,45 +3734,46 @@ the block has a known type.
3710
3734
3711
3735
Coercion is allowed between the following types:
3712
3736
3713
- * ` T ` to ` U ` if ` T ` is a subtype of ` U ` (* reflexive case* ).
3737
+ * ` T ` to ` U ` if ` T ` is a subtype of ` U ` (* reflexive case* )
3714
3738
3715
3739
* ` T_1 ` to ` T_3 ` where ` T_1 ` coerces to ` T_2 ` and ` T_2 ` coerces to ` T_3 `
3716
- (* transitive case* ).
3740
+ (* transitive case* )
3717
3741
3718
3742
Note that this is not fully supported yet
3719
3743
3720
- * ` &mut T ` to ` &T ` .
3744
+ * ` &mut T ` to ` &T `
3721
3745
3722
- * ` *mut T ` to ` *const T ` .
3746
+ * ` *mut T ` to ` *const T `
3723
3747
3724
- * ` &T ` to ` *const T ` .
3748
+ * ` &T ` to ` *const T `
3725
3749
3726
- * ` &mut T ` to ` *mut T ` .
3750
+ * ` &mut T ` to ` *mut T `
3727
3751
3728
3752
* ` &T ` to ` &U ` if ` T ` implements ` Deref<Target = U> ` . For example:
3729
3753
3730
- ``` rust
3731
- use std :: ops :: Deref ;
3754
+ ``` rust
3755
+ use std :: ops :: Deref ;
3732
3756
3733
- struct CharContainer {
3734
- value : char
3735
- }
3757
+ struct CharContainer {
3758
+ value : char
3759
+ }
3736
3760
3737
- impl Deref for CharContainer {
3738
- type Target = char ;
3761
+ impl Deref for CharContainer {
3762
+ type Target = char ;
3739
3763
3740
- fn deref <'a >(& 'a self ) -> & 'a char {
3741
- & self . value
3742
- }
3743
- }
3764
+ fn deref <'a >(& 'a self ) -> & 'a char {
3765
+ & self . value
3766
+ }
3767
+ }
3744
3768
3745
- fn foo (arg : & char ) {}
3769
+ fn foo (arg : & char ) {}
3770
+
3771
+ fn main () {
3772
+ let x = & mut CharContainer { value : 'y' };
3773
+ foo (x ); // &mut CharContainer is coerced to &char.
3774
+ }
3775
+ ```
3746
3776
3747
- fn main () {
3748
- let x = & mut CharContainer { value : 'y' };
3749
- foo (x ); // &mut CharContainer is coerced to &char.
3750
- }
3751
- ```
3752
3777
* ` &mut T ` to ` &mut U ` if ` T ` implements ` DerefMut<Target = U> ` .
3753
3778
3754
3779
* TyCtor(` T ` ) to TyCtor(coerce_inner(` T ` )), where TyCtor(` T ` ) is one of
0 commit comments