Skip to content

Commit 0d3954f

Browse files
committed
reference: improve coercions section
1 parent 912ab64 commit 0d3954f

File tree

1 file changed

+66
-41
lines changed

1 file changed

+66
-41
lines changed

src/doc/reference.md

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

36533653
* `let` statements where an explicit type is given.
36543654

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

36573661
* `static` and `const` statements (similar to `let` statements).
36583662

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

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

3666-
* instantiations of struct or variant fields.
3678+
* Instantiations of struct or variant fields
36673679

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

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

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

36773701
If the expression in one of these coercion sites is a coercion-propagating
36783702
expression, then the relevant sub-expressions in that expression are also
36793703
coercion sites. Propagation recurses from these new coercion sites.
36803704
Propagating expressions and their relevant sub-expressions are:
36813705

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

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

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

3692-
* parenthesised sub-expressions (`(e)`). If the expression has type `U`, then
3716+
* Parenthesised sub-expressions (`(e)`): if the expression has type `U`, then
36933717
the sub-expression is a coercion site to `U`.
36943718

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
36963720
it is not semicolon-terminated) is a coercion site to `U`. This includes
36973721
blocks which are part of control flow statements, such as `if`/`else`, if
36983722
the block has a known type.
@@ -3701,45 +3725,46 @@ the block has a known type.
37013725

37023726
Coercion is allowed between the following types:
37033727

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

37063730
* `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*)
37083732

37093733
Note that this is not fully supported yet
37103734

3711-
* `&mut T` to `&T`.
3735+
* `&mut T` to `&T`
37123736

3713-
* `*mut T` to `*const T`.
3737+
* `*mut T` to `*const T`
37143738

3715-
* `&T` to `*const T`.
3739+
* `&T` to `*const T`
37163740

3717-
* `&mut T` to `*mut T`.
3741+
* `&mut T` to `*mut T`
37183742

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

3721-
```rust
3722-
use std::ops::Deref;
3745+
```rust
3746+
use std::ops::Deref;
37233747

3724-
struct CharContainer {
3725-
value: char
3726-
}
3748+
struct CharContainer {
3749+
value: char
3750+
}
37273751

3728-
impl Deref for CharContainer {
3729-
type Target = char;
3752+
impl Deref for CharContainer {
3753+
type Target = char;
37303754

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

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

3738-
fn main() {
3739-
let x = &mut CharContainer { value: 'y' };
3740-
foo(x); //&mut CharContainer is coerced to &char.
3741-
}
3742-
```
37433768
* `&mut T` to `&mut U` if `T` implements `DerefMut<Target = U>`.
37443769

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

0 commit comments

Comments
 (0)