1
1
# Type coercions
2
2
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.
5
6
6
7
## Coercion sites
7
8
@@ -179,14 +180,13 @@ unsized coercion to `Foo<U>`.
179
180
> has been stabilized, the traits themselves are not yet stable and therefore
180
181
> can't be used directly in stable Rust.
181
182
182
- ## LUB Coercion
183
+ ## Least upper bound coercions
183
184
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
186
187
single type. This happens in [ match] arms, [ if expressions] and [ array literal
187
188
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.
190
190
191
191
For example:
192
192
@@ -208,11 +208,12 @@ let bar = match 42 {
208
208
209
209
let baz = [a , b , c ];
210
210
```
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
213
214
` [LubCoerce(typeof(a), typeof(b), typeof(c)); 3] ` .
214
215
215
- Here is the pseudo code of ` LubCoerce ` in the rustc :
216
+ LUB coercion is performed by the following algorithm :
216
217
217
218
``` txt
218
219
Lub(a: Type, b: Type):
@@ -222,31 +223,33 @@ Lub(a: Type, b: Type):
222
223
&& b != capturing Closure:
223
224
return FnPtr
224
225
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.
226
228
if Coerce(b, a):
227
229
return a
228
230
if Coerce(a, b):
229
231
return b
230
232
231
233
// LubCoerce failed
232
- emits error
234
+ emit type error
233
235
234
- LubCoerce(vars):
236
+ LubCoerce(vars... ):
235
237
result = vars.get(0)
236
238
for var in vars[1..]:
237
239
result = Lub(result, var)
238
240
return result
239
241
```
240
242
241
243
LUB coercion has the following properties:
244
+
242
245
1 . Order independent: e.g. ` LubCoerce(ty0, ty1, ty2, ty3) ` equals to
243
246
` LubCoerce(ty1, ty0, ty4, ty3) ` .
244
247
2 . ` LubCoerce(ty0, ty1, ty2, ...) ` equals to
245
248
` 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 ` .
248
251
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".
250
253
That's because currently if ` ty0 ` and ` ty1 ` can be coerced to ` ty2 ` and
251
254
unfortunately ` ty2 ` equals to neither ` ty0 ` nor ` ty1 ` , there are only one
252
255
special situation where we can get ` LubCoerce(ty0, ty1) == Some(ty2) ` :
0 commit comments