Skip to content

Commit f13f494

Browse files
Havvyldm0
authored andcommitted
Minor wording tweaks and movement in LUB coercions
1 parent afcb386 commit f13f494

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/type-coercions.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Type coercions
22

3-
Coercions are defined in [RFC 401]. [RFC 1558] then expanded on that.
4-
A coercion is implicit and has no syntax.
3+
Coercions are defined in [RFC 401]. [RFC 1558] then expanded on that. However,
4+
Least upper bound coercions are not described in an RFC. A coercion is implicit
5+
and has no syntax.
56

67
## Coercion sites
78

@@ -179,14 +180,13 @@ unsized coercion to `Foo<U>`.
179180
> has been stabilized, the traits themselves are not yet stable and therefore
180181
> can't be used directly in stable Rust.
181182
182-
## LUB Coercion
183+
## Least upper bound coercions
183184

184-
*Least upper bound coercion* (LUB coercion) is the type of coercion take place
185-
when several expressions of possibly different types need be unified to a
185+
*Least upper bound coercion* (LUB coercion) is the form of type coercion take
186+
place when several expressions of possibly different types need be unified to a
186187
single type. This happens in [match] arms, [if expressions] and [array literal
187188
expressions]. In LUB coercion, the compiler tries to find the least upper
188-
bound of given types. However, Least Upper Bound coercion is not described in
189-
any RFC.
189+
bound of given types.
190190

191191
For example:
192192

@@ -208,11 +208,12 @@ let bar = match 42 {
208208

209209
let baz = [a, b, c];
210210
```
211-
In this example, both `foo` and `bar` get the type
212-
`LubCoerce(typeof(a), typeof(a), typeof(c))` and `baz` get the type
211+
212+
In this example, both `foo` and `bar` have the type
213+
`LubCoerce(typeof(a), typeof(a), typeof(c))` and `baz` have the type
213214
`[LubCoerce(typeof(a), typeof(b), typeof(c)); 3]`.
214215

215-
Here is the pseudo code of `LubCoerce` in the rustc:
216+
LUB coercion is performed by the following algorithm:
216217

217218
```txt
218219
Lub(a: Type, b: Type):
@@ -222,31 +223,33 @@ Lub(a: Type, b: Type):
222223
&& b != capturing Closure:
223224
return FnPtr
224225
225-
// Coerce(x, y) returns true when x can be coerced to y
226+
// Coerce(x, y) returns true when x can be coerced to y by coercion described
227+
// in previous sections of this page.
226228
if Coerce(b, a):
227229
return a
228230
if Coerce(a, b):
229231
return b
230232
231233
// LubCoerce failed
232-
emits error
234+
emit type error
233235
234-
LubCoerce(vars):
236+
LubCoerce(vars...):
235237
result = vars.get(0)
236238
for var in vars[1..]:
237239
result = Lub(result, var)
238240
return result
239241
```
240242

241243
LUB coercion has the following properties:
244+
242245
1. Order independent: e.g. `LubCoerce(ty0, ty1, ty2, ty3)` equals to
243246
`LubCoerce(ty1, ty0, ty4, ty3)`.
244247
2. `LubCoerce(ty0, ty1, ty2, ...)` equals to
245248
`LubCoerce(LubCoerce(ty0, ty1), ty2, ...)`
246-
3. `LubCoerce(ty0, ty1) == Some(ty2)` means both `ty0` and `ty1` can be coerced to
247-
`ty2`.
249+
3. `LubCoerce(ty0, ty1) == Some(ty2)` means both `ty0` and `ty1` can be coerced
250+
to `ty2`.
248251

249-
Notice the feature No.3, it uses the word "means" rather than "if and only if".
252+
Note the feature No. 3, it uses the word "means" rather than "if and only if".
250253
That's because currently if `ty0` and `ty1` can be coerced to `ty2` and
251254
unfortunately `ty2` equals to neither `ty0` nor `ty1`, there are only one
252255
special situation where we can get `LubCoerce(ty0, ty1) == Some(ty2)`:

0 commit comments

Comments
 (0)