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