@@ -184,3 +184,48 @@ unsized coercion to `Foo<U>`.
184
184
185
185
[ `Unsize` ] : ../std/marker/trait.Unsize.html
186
186
[ `CoerceUnsized` ] : ../std/ops/trait.CoerceUnsized.html
187
+
188
+ ## LUB Coercion
189
+
190
+ This is not in the RFC 401, but it's the behavior of current compiler. In LUB
191
+ Coercion, we tried to find the least upper bound of given types. It can be
192
+ observed in array collection, if-else/match arms.
193
+
194
+ For example:
195
+
196
+ ``` rust
197
+ # let (A , B , C ) = (0 , 1 , 2 );
198
+ let emm = if true {
199
+ A
200
+ } else if false {
201
+ B
202
+ } else {
203
+ C
204
+ };
205
+
206
+ let foo = match 42 {
207
+ 0 => A ,
208
+ 1 => B ,
209
+ _ => C ,
210
+ };
211
+
212
+ let bar = [A , B , C ];
213
+ ```
214
+ In this example, both ` emm ` and ` foo ` has type
215
+ ` LubCoerce(typeof(A), typeof(B), typeof(C)) ` and ` bar ` has type
216
+ ` [LubCoerce(typeof(A), typeof(B), typeof(C)); 3] ` .
217
+
218
+ LUB Coercion has some features:
219
+ 1 . Order independent: e.g. ` LubCoerce(ty0, ty1, ty2, ty3) ` equals to
220
+ ` LubCoerce(ty1, ty0, ty4, ty3) ` .
221
+ 2 . ` LubCoerce(ty0, ty1, ty2, ...) ` equals to
222
+ ` LubCoerce(LubCoerce(ty0, ty1), ty2, ...) `
223
+ 4 . ` LubCoerce(ty0, ty1) == ty2 ` means both ` ty0 ` and ` ty1 ` can be coerced to
224
+ ` ty2 ` .
225
+
226
+ Notice the feature No.3, it uses the word "means" rather than "if and only if".
227
+ That's because currently if ` ty0 ` and ` ty1 ` can be coerced to ` ty2 ` and ` ty2 `
228
+ equals to neither ` ty0 ` nor ` ty1 ` , there are only two special cases we can get
229
+ ` LubCoerce(ty0, ty1) == ty2 ` :
230
+ 1 . ` LubCoerce(FnDef, FnDef) == FnPtr ` ,
231
+ 2 . ` LubCoerce(Closure, Closure) == FnPtr ` (where Closure is non-capturing).
0 commit comments