Skip to content

Commit 64f4ba8

Browse files
edwardwalexcrichton
authored andcommitted
---
yaml --- r: 152667 b: refs/heads/try2 c: b1df9aa h: refs/heads/master i: 152665: ea8acb9 152663: bdd963f v: v3
1 parent 55f9ec5 commit 64f4ba8

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: bde851e969d327397ac1ab4bca05692069ff62a5
8+
refs/heads/try2: b1df9aa16fe0f801f2f56afdddd6faa0461a3b0e
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/librustc/middle/trans/_match.rs

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,7 @@ fn enter_default<'a, 'b>(
518518
dm: &DefMap,
519519
m: &'a [Match<'a, 'b>],
520520
col: uint,
521-
val: ValueRef,
522-
chk: &FailureHandler)
521+
val: ValueRef)
523522
-> Vec<Match<'a, 'b>> {
524523
debug!("enter_default(bcx={}, m={}, col={}, val={})",
525524
bcx.to_str(),
@@ -529,35 +528,13 @@ fn enter_default<'a, 'b>(
529528
let _indenter = indenter();
530529

531530
// Collect all of the matches that can match against anything.
532-
let matches = enter_match(bcx, dm, m, col, val, |p| {
531+
enter_match(bcx, dm, m, col, val, |p| {
533532
match p.node {
534533
ast::PatWild | ast::PatWildMulti => Some(Vec::new()),
535534
ast::PatIdent(_, _, None) if pat_is_binding(dm, &*p) => Some(Vec::new()),
536535
_ => None
537536
}
538-
});
539-
540-
// Ok, now, this is pretty subtle. A "default" match is a match
541-
// that needs to be considered if none of the actual checks on the
542-
// value being considered succeed. The subtlety lies in that sometimes
543-
// identifier/wildcard matches are *not* default matches. Consider:
544-
// "match x { _ if something => foo, true => bar, false => baz }".
545-
// There is a wildcard match, but it is *not* a default case. The boolean
546-
// case on the value being considered is exhaustive. If the case is
547-
// exhaustive, then there are no defaults.
548-
//
549-
// We detect whether the case is exhaustive in the following
550-
// somewhat kludgy way: if the last wildcard/binding match has a
551-
// guard, then by non-redundancy, we know that there aren't any
552-
// non guarded matches, and thus by exhaustiveness, we know that
553-
// we don't need any default cases. If the check *isn't* nonexhaustive
554-
// (because chk is Some), then we need the defaults anyways.
555-
let is_exhaustive = match matches.last() {
556-
Some(m) if m.data.arm.guard.is_some() && chk.is_infallible() => true,
557-
_ => false
558-
};
559-
560-
if is_exhaustive { Vec::new() } else { matches }
537+
})
561538
}
562539

563540
// <pcwalton> nmatsakis: what does enter_opt do?
@@ -1448,15 +1425,12 @@ fn compile_submatch<'a, 'b>(
14481425
m.repr(bcx.tcx()),
14491426
vec_map_to_str(vals, |v| bcx.val_to_str(*v)));
14501427
let _indenter = indenter();
1451-
1452-
/*
1453-
For an empty match, a fall-through case must exist
1454-
*/
1455-
assert!((m.len() > 0u || chk.is_fallible()));
14561428
let _icx = push_ctxt("match::compile_submatch");
14571429
let mut bcx = bcx;
14581430
if m.len() == 0u {
1459-
Br(bcx, chk.handle_fail());
1431+
if chk.is_fallible() {
1432+
Br(bcx, chk.handle_fail());
1433+
}
14601434
return;
14611435
}
14621436
if m[0].pats.len() == 0u {
@@ -1658,7 +1632,7 @@ fn compile_submatch_continue<'a, 'b>(
16581632
C_int(ccx, 0) // Placeholder for when not using a switch
16591633
};
16601634

1661-
let defaults = enter_default(else_cx, dm, m, col, val, chk);
1635+
let defaults = enter_default(else_cx, dm, m, col, val);
16621636
let exhaustive = chk.is_infallible() && defaults.len() == 0u;
16631637
let len = opts.len();
16641638

@@ -1947,18 +1921,14 @@ fn trans_match_inner<'a>(scope_cx: &'a Block<'a>,
19471921

19481922
// `compile_submatch` works one column of arm patterns a time and
19491923
// then peels that column off. So as we progress, it may become
1950-
// impossible to know whether we have a genuine default arm, i.e.
1924+
// impossible to tell whether we have a genuine default arm, i.e.
19511925
// `_ => foo` or not. Sometimes it is important to know that in order
19521926
// to decide whether moving on to the next condition or falling back
19531927
// to the default arm.
1954-
let has_default = arms.len() > 0 && {
1955-
let ref pats = arms.last().unwrap().pats;
1956-
1957-
pats.len() == 1
1958-
&& match pats.last().unwrap().node {
1959-
ast::PatWild => true, _ => false
1960-
}
1961-
};
1928+
let has_default = arms.last().map_or(false, |arm| {
1929+
arm.pats.len() == 1
1930+
&& arm.pats.last().unwrap().node == ast::PatWild
1931+
});
19621932

19631933
compile_submatch(bcx, matches.as_slice(), [discr_datum.val], &chk, has_default);
19641934

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
enum X {
12+
Foo(uint),
13+
Bar(bool)
14+
}
15+
16+
fn main() {
17+
let x = match Foo(42) {
18+
Foo(..) => 1,
19+
_ if true => 0,
20+
Bar(..) => fail!("Oh dear")
21+
};
22+
assert_eq!(x, 1);
23+
24+
let x = match Foo(42) {
25+
_ if true => 0,
26+
Foo(..) => 1,
27+
Bar(..) => fail!("Oh dear")
28+
};
29+
assert_eq!(x, 0);
30+
}

0 commit comments

Comments
 (0)