@@ -228,7 +228,37 @@ fn any_box_pat(m: &match, col: uint) -> bool {
228
228
}
229
229
230
230
type exit_node = { bound : bind_map , from : BasicBlockRef , to : BasicBlockRef } ;
231
- type mk_fail = fn ( ) -> BasicBlockRef ;
231
+ type mk_fail = fn ( ) -> BasicBlockRef ;
232
+
233
+ fn pick_col ( m : & match ) -> uint {
234
+ let scores = ivec:: init_elt_mut ( 0 u, ivec:: len ( m. ( 0 ) . pats ) ) ;
235
+ for br: match_branch in m {
236
+ let i = 0 u;
237
+ for p: @ast:: pat in br. pats {
238
+ alt p. node {
239
+ ast:: pat_lit ( _) | ast:: pat_tag ( _, _) { scores. ( i) += 1 u; }
240
+ _ { }
241
+ }
242
+ i += 1 u;
243
+ }
244
+ }
245
+ let max_score = 0 u;
246
+ let best_col = 0 u;
247
+ let i = 0 u;
248
+ for score: uint in scores {
249
+ // Irrefutable columns always go first, they'd only be duplicated in
250
+ // the branches.
251
+ if score == 0 u { ret i; }
252
+ // If no irrefutable ones are found, we pick the one with the biggest
253
+ // branching factor.
254
+ if score > max_score {
255
+ max_score = score;
256
+ best_col = i;
257
+ }
258
+ i += 1 u;
259
+ }
260
+ ret best_col;
261
+ }
232
262
233
263
fn compile_submatch ( bcx : @block_ctxt , m : & match , vals : ValueRef [ ] ,
234
264
f : & mk_fail , exits : & mutable exit_node[ ] ) {
@@ -239,10 +269,10 @@ fn compile_submatch(bcx: @block_ctxt, m: &match, vals: ValueRef[],
239
269
ret;
240
270
}
241
271
242
- // FIXME maybe be clever about picking a column.
243
- let col = 0 u;
272
+ let col = pick_col ( m) ;
244
273
let val = vals. ( col) ;
245
- let vals_left = ivec:: slice ( vals, 1 u, ivec:: len ( vals) ) ;
274
+ let vals_left = ivec:: slice ( vals, 0 u, col) +
275
+ ivec:: slice ( vals, col + 1 u, ivec:: len ( vals) ) ;
246
276
let ccx = bcx. fcx . lcx . ccx ;
247
277
let pat_id = 0 ;
248
278
for br: match_branch in m {
0 commit comments