-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
_match.rs: prune sub-match tree too aggressively
The `_match.rs` takes advantage of passes prior to `trans` and aggressively prunes the sub-match tree based on exact equality. When it comes to literal or range, the strategy may lead to wrong result if there's guard function or multiple patterns inside tuple. Closes #12582. Closes #13027.
- Loading branch information
Showing
3 changed files
with
280 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
pub fn main() { | ||
let x = 1; | ||
let y = 2; | ||
|
||
assert_eq!(3, match (x, y) { | ||
(1, 1) => 1, | ||
(2, 2) => 2, | ||
(1..2, 2) => 3, | ||
_ => 4, | ||
}); | ||
|
||
// nested tuple | ||
assert_eq!(3, match ((x, y),) { | ||
((1, 1),) => 1, | ||
((2, 2),) => 2, | ||
((1..2, 2),) => 3, | ||
_ => 4, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// Tests that match expression handles overlapped literal and range | ||
// properly in the presence of guard function. | ||
|
||
fn val() -> uint { 1 } | ||
|
||
static CONST: uint = 1; | ||
|
||
pub fn main() { | ||
lit_shadow_range(); | ||
range_shadow_lit(); | ||
range_shadow_range(); | ||
multi_pats_shadow_lit(); | ||
multi_pats_shadow_range(); | ||
lit_shadow_multi_pats(); | ||
range_shadow_multi_pats(); | ||
} | ||
|
||
fn lit_shadow_range() { | ||
assert_eq!(2, match 1 { | ||
1 if false => 1, | ||
1..2 => 2, | ||
_ => 3 | ||
}); | ||
|
||
let x = 0; | ||
assert_eq!(2, match x+1 { | ||
0 => 0, | ||
1 if false => 1, | ||
1..2 => 2, | ||
_ => 3 | ||
}); | ||
|
||
assert_eq!(2, match val() { | ||
1 if false => 1, | ||
1..2 => 2, | ||
_ => 3 | ||
}); | ||
|
||
assert_eq!(2, match CONST { | ||
0 => 0, | ||
1 if false => 1, | ||
1..2 => 2, | ||
_ => 3 | ||
}); | ||
|
||
// value is out of the range of second arm, should match wildcard pattern | ||
assert_eq!(3, match 3 { | ||
1 if false => 1, | ||
1..2 => 2, | ||
_ => 3 | ||
}); | ||
} | ||
|
||
fn range_shadow_lit() { | ||
assert_eq!(2, match 1 { | ||
1..2 if false => 1, | ||
1 => 2, | ||
_ => 3 | ||
}); | ||
|
||
let x = 0; | ||
assert_eq!(2, match x+1 { | ||
0 => 0, | ||
1..2 if false => 1, | ||
1 => 2, | ||
_ => 3 | ||
}); | ||
|
||
assert_eq!(2, match val() { | ||
1..2 if false => 1, | ||
1 => 2, | ||
_ => 3 | ||
}); | ||
|
||
assert_eq!(2, match CONST { | ||
0 => 0, | ||
1..2 if false => 1, | ||
1 => 2, | ||
_ => 3 | ||
}); | ||
|
||
// ditto | ||
assert_eq!(3, match 3 { | ||
1..2 if false => 1, | ||
1 => 2, | ||
_ => 3 | ||
}); | ||
} | ||
|
||
fn range_shadow_range() { | ||
assert_eq!(2, match 1 { | ||
0..2 if false => 1, | ||
1..3 => 2, | ||
_ => 3, | ||
}); | ||
|
||
let x = 0; | ||
assert_eq!(2, match x+1 { | ||
100 => 0, | ||
0..2 if false => 1, | ||
1..3 => 2, | ||
_ => 3, | ||
}); | ||
|
||
assert_eq!(2, match val() { | ||
0..2 if false => 1, | ||
1..3 => 2, | ||
_ => 3, | ||
}); | ||
|
||
assert_eq!(2, match CONST { | ||
100 => 0, | ||
0..2 if false => 1, | ||
1..3 => 2, | ||
_ => 3, | ||
}); | ||
|
||
// ditto | ||
assert_eq!(3, match 5 { | ||
0..2 if false => 1, | ||
1..3 => 2, | ||
_ => 3, | ||
}); | ||
} | ||
|
||
fn multi_pats_shadow_lit() { | ||
assert_eq!(2, match 1 { | ||
100 => 0, | ||
0 | 1..10 if false => 1, | ||
1 => 2, | ||
_ => 3, | ||
}); | ||
} | ||
|
||
fn multi_pats_shadow_range() { | ||
assert_eq!(2, match 1 { | ||
100 => 0, | ||
0 | 1..10 if false => 1, | ||
1..3 => 2, | ||
_ => 3, | ||
}); | ||
} | ||
|
||
fn lit_shadow_multi_pats() { | ||
assert_eq!(2, match 1 { | ||
100 => 0, | ||
1 if false => 1, | ||
0 | 1..10 => 2, | ||
_ => 3, | ||
}); | ||
} | ||
|
||
fn range_shadow_multi_pats() { | ||
assert_eq!(2, match 1 { | ||
100 => 0, | ||
1..3 if false => 1, | ||
0 | 1..10 => 2, | ||
_ => 3, | ||
}); | ||
} |
4112941
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
saw approval from nikomatsakis
at edwardw@4112941
4112941
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merging edwardw/rust/match = 4112941 into auto
4112941
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
edwardw/rust/match = 4112941 merged ok, testing candidate = e560db7
4112941
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all tests pass:
success: http://buildbot.rust-lang.org/builders/auto-mac-32-opt/builds/4910
success: http://buildbot.rust-lang.org/builders/auto-mac-64-opt/builds/4911
success: http://buildbot.rust-lang.org/builders/auto-mac-64-nopt-c/builds/4004
success: http://buildbot.rust-lang.org/builders/auto-mac-64-nopt-t/builds/4014
success: http://buildbot.rust-lang.org/builders/auto-linux-32-opt/builds/5011
success: http://buildbot.rust-lang.org/builders/auto-linux-32-nopt-c/builds/4097
success: http://buildbot.rust-lang.org/builders/auto-linux-32-nopt-t/builds/4105
success: http://buildbot.rust-lang.org/builders/auto-linux-64-opt/builds/5012
success: http://buildbot.rust-lang.org/builders/auto-linux-64-nopt-c/builds/4097
success: http://buildbot.rust-lang.org/builders/auto-linux-64-nopt-t/builds/4102
success: http://buildbot.rust-lang.org/builders/auto-linux-64-x-android/builds/4168
success: http://buildbot.rust-lang.org/builders/auto-linux-64-x-android-t/builds/1901
success: http://buildbot.rust-lang.org/builders/auto-win-32-opt/builds/5008
success: http://buildbot.rust-lang.org/builders/auto-win-32-nopt-c/builds/4108
success: http://buildbot.rust-lang.org/builders/auto-win-32-nopt-t/builds/4116
success: http://buildbot.rust-lang.org/builders/auto-bsd-64-opt/builds/4773
4112941
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fast-forwarding master to auto = e560db7