Skip to content

Commit 1fdfd3b

Browse files
Jakub Wieczorekgraydon
Jakub Wieczorek
authored andcommitted
Simplify the exhaustiveness check and add comments
1 parent 856dbac commit 1fdfd3b

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

src/librustc/middle/check_alt.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -392,49 +392,49 @@ fn missing_ctor(cx: @AltCheckCtxt,
392392
else { Some(val(const_bool(true))) }
393393
}
394394
ty::ty_unboxed_vec(*) | ty::ty_evec(*) => {
395-
let max_len = do m.foldr(0) |r, max_len| {
396-
match r[0].node {
397-
pat_vec(elems, _) => uint::max(elems.len(), max_len),
398-
_ => max_len
399-
}
400-
};
401-
let min_len_with_tail = do m.foldr(max_len + 1) |r, min_len| {
402-
match r[0].node {
403-
pat_vec(elems, tail) => {
404-
if tail.is_some() && elems.len() < min_len {
405-
elems.len()
406-
} else {
407-
min_len
408-
}
409-
}
410-
_ => min_len
411-
}
412-
};
413-
let vec_lens = do m.filter_map |r| {
414-
match r[0].node {
415-
pat_vec(elems, tail) => {
416-
match tail {
417-
None if elems.len() < min_len_with_tail => Some(elems.len()),
395+
396+
// Find the lengths and tails of all vector patterns.
397+
let vec_pat_lens = do m.filter_map |r| {
398+
match r[0].node {
399+
pat_vec(elems, tail) => {
400+
Some((elems.len(), tail.is_some()))
401+
}
418402
_ => None
419-
}
420403
}
421-
_ => None
422-
}
423-
};
424-
let mut sorted_vec_lens = do sort::merge_sort(vec_lens) |a, b| {
425-
a < b
426404
};
405+
406+
// Sort them by length such that for patterns of the same length,
407+
// those with a destructured tail come first.
408+
let mut sorted_vec_lens = sort::merge_sort(vec_pat_lens,
409+
|&(len1, tail1), &(len2, tail2)| {
410+
if len1 == len2 {
411+
tail1 > tail2
412+
} else {
413+
len1 <= len2
414+
}
415+
}
416+
);
427417
vec::dedup(&mut sorted_vec_lens);
428418

419+
let mut found_tail = false;
420+
let mut next = 0;
429421
let mut missing = None;
430-
for uint::range(0, min_len_with_tail) |i| {
431-
if i >= sorted_vec_lens.len() || i != sorted_vec_lens[i] {
432-
missing = Some(i);
433-
break;
434-
}
435-
};
436-
if missing.is_none() && min_len_with_tail > max_len {
437-
missing = Some(min_len_with_tail);
422+
for sorted_vec_lens.each |&(length, tail)| {
423+
if length != next {
424+
missing = Some(next);
425+
break;
426+
}
427+
if tail {
428+
found_tail = true;
429+
break;
430+
}
431+
next += 1;
432+
}
433+
434+
// We found patterns of all lengths within <0, next), yet there was no
435+
// pattern with a tail - therefore, we report vec(next) as missing.
436+
if !found_tail {
437+
missing = Some(next);
438438
}
439439
match missing {
440440
Some(k) => Some(vec(k)),

0 commit comments

Comments
 (0)