Skip to content

Commit 0eacc7a

Browse files
tshepangManishearth
authored andcommitted
reference: improve coercions section
1 parent cfa8081 commit 0eacc7a

File tree

1 file changed

+66
-41
lines changed

1 file changed

+66
-41
lines changed

reference.md

Lines changed: 66 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3661,47 +3661,71 @@ sites are:
36613661

36623662
* `let` statements where an explicit type is given.
36633663

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+
```
36653669

36663670
* `static` and `const` statements (similar to `let` statements).
36673671

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) { }
36693681

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+
```
36743686

3675-
* instantiations of struct or variant fields.
3687+
* Instantiations of struct or variant fields
36763688

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:
36803690

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 }
36833693

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+
```
36853709

36863710
If the expression in one of these coercion sites is a coercion-propagating
36873711
expression, then the relevant sub-expressions in that expression are also
36883712
coercion sites. Propagation recurses from these new coercion sites.
36893713
Propagating expressions and their relevant sub-expressions are:
36903714

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
36923716
the array literal is a coercion site for coercion to type `U`.
36933717

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
36953719
repeated sub-expression is a coercion site for coercion to type `U`.
36963720

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)`.
36983722
Each sub-expression is a coercion site to the respective type, e.g. the
36993723
zeroth sub-expression is a coercion site to type `U_0`.
37003724

3701-
* parenthesised sub-expressions (`(e)`). If the expression has type `U`, then
3725+
* Parenthesised sub-expressions (`(e)`): if the expression has type `U`, then
37023726
the sub-expression is a coercion site to `U`.
37033727

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
37053729
it is not semicolon-terminated) is a coercion site to `U`. This includes
37063730
blocks which are part of control flow statements, such as `if`/`else`, if
37073731
the block has a known type.
@@ -3710,45 +3734,46 @@ the block has a known type.
37103734

37113735
Coercion is allowed between the following types:
37123736

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*)
37143738

37153739
* `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*)
37173741

37183742
Note that this is not fully supported yet
37193743

3720-
* `&mut T` to `&T`.
3744+
* `&mut T` to `&T`
37213745

3722-
* `*mut T` to `*const T`.
3746+
* `*mut T` to `*const T`
37233747

3724-
* `&T` to `*const T`.
3748+
* `&T` to `*const T`
37253749

3726-
* `&mut T` to `*mut T`.
3750+
* `&mut T` to `*mut T`
37273751

37283752
* `&T` to `&U` if `T` implements `Deref<Target = U>`. For example:
37293753

3730-
```rust
3731-
use std::ops::Deref;
3754+
```rust
3755+
use std::ops::Deref;
37323756

3733-
struct CharContainer {
3734-
value: char
3735-
}
3757+
struct CharContainer {
3758+
value: char
3759+
}
37363760

3737-
impl Deref for CharContainer {
3738-
type Target = char;
3761+
impl Deref for CharContainer {
3762+
type Target = char;
37393763

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+
}
37443768

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+
```
37463776

3747-
fn main() {
3748-
let x = &mut CharContainer { value: 'y' };
3749-
foo(x); //&mut CharContainer is coerced to &char.
3750-
}
3751-
```
37523777
* `&mut T` to `&mut U` if `T` implements `DerefMut<Target = U>`.
37533778

37543779
* TyCtor(`T`) to TyCtor(coerce_inner(`T`)), where TyCtor(`T`) is one of

0 commit comments

Comments
 (0)