-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Improve MIR match generation for ranges #56810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
use build::Builder; | ||
use build::matches::{Candidate, MatchPair, Test, TestKind}; | ||
use hair::*; | ||
use hair::pattern::compare_const_vals; | ||
use rustc_data_structures::bit_set::BitSet; | ||
use rustc_data_structures::fx::FxHashMap; | ||
use rustc::ty::{self, Ty}; | ||
|
@@ -136,7 +137,15 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { | |
PatternKind::Variant { .. } => { | ||
panic!("you should have called add_variants_to_switch instead!"); | ||
} | ||
PatternKind::Range { .. } | | ||
PatternKind::Range { ty, lo, hi, end } => { | ||
indices | ||
.keys() | ||
.all(|value| { | ||
!self | ||
.const_range_contains(ty, lo, hi, end, value) | ||
.unwrap_or(true) | ||
}) | ||
} | ||
PatternKind::Slice { .. } | | ||
PatternKind::Array { .. } | | ||
PatternKind::Wild | | ||
|
@@ -200,20 +209,18 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { | |
for (idx, discr) in adt_def.discriminants(tcx) { | ||
target_blocks.push(if variants.contains(idx) { | ||
values.push(discr.val); | ||
targets.push(self.cfg.start_new_block()); | ||
*targets.last().unwrap() | ||
let block = self.cfg.start_new_block(); | ||
targets.push(block); | ||
block | ||
} else { | ||
if otherwise_block.is_none() { | ||
otherwise_block = Some(self.cfg.start_new_block()); | ||
} | ||
otherwise_block.unwrap() | ||
*otherwise_block | ||
.get_or_insert_with(|| self.cfg.start_new_block()) | ||
}); | ||
} | ||
if let Some(otherwise_block) = otherwise_block { | ||
targets.push(otherwise_block); | ||
} else { | ||
targets.push(self.unreachable_block()); | ||
} | ||
targets.push( | ||
otherwise_block | ||
.unwrap_or_else(|| self.unreachable_block()), | ||
); | ||
debug!("num_enum_variants: {}, tested variants: {:?}, variants: {:?}", | ||
num_enum_variants, values, variants); | ||
let discr_ty = adt_def.repr.discr_type().to_ty(tcx); | ||
|
@@ -490,8 +497,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { | |
// away.) | ||
let tested_match_pair = candidate.match_pairs.iter() | ||
.enumerate() | ||
.filter(|&(_, mp)| mp.place == *test_place) | ||
.next(); | ||
.find(|&(_, mp)| mp.place == *test_place); | ||
let (match_pair_index, match_pair) = match tested_match_pair { | ||
Some(pair) => pair, | ||
None => { | ||
|
@@ -532,6 +538,28 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { | |
resulting_candidates[index].push(new_candidate); | ||
true | ||
} | ||
|
||
(&TestKind::SwitchInt { switch_ty: _, ref options, ref indices }, | ||
&PatternKind::Range { ty, lo, hi, end }) => { | ||
let not_contained = indices | ||
sinkuu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.keys() | ||
.all(|value| { | ||
!self | ||
.const_range_contains(ty, lo, hi, end, value) | ||
.unwrap_or(true) | ||
}); | ||
|
||
if not_contained { | ||
// No values are contained in the pattern range, | ||
// so the pattern can be matched only if this test fails. | ||
let otherwise = options.len(); | ||
resulting_candidates[otherwise].push(candidate.clone()); | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
(&TestKind::SwitchInt { .. }, _) => false, | ||
|
||
|
||
|
@@ -610,8 +638,70 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { | |
} | ||
} | ||
|
||
(&TestKind::Range { | ||
lo: test_lo, hi: test_hi, ty: test_ty, end: test_end, | ||
}, &PatternKind::Range { | ||
lo: pat_lo, hi: pat_hi, ty: _, end: pat_end, | ||
}) => { | ||
if (test_lo, test_hi, test_end) == (pat_lo, pat_hi, pat_end) { | ||
sinkuu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resulting_candidates[0] | ||
.push(self.candidate_without_match_pair( | ||
match_pair_index, | ||
candidate, | ||
)); | ||
return true; | ||
} | ||
|
||
let no_overlap = (|| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
use std::cmp::Ordering::*; | ||
use rustc::hir::RangeEnd::*; | ||
|
||
let param_env = ty::ParamEnv::empty().and(test_ty); | ||
let tcx = self.hir.tcx(); | ||
|
||
let lo = compare_const_vals(tcx, test_lo, pat_hi, param_env)?; | ||
let hi = compare_const_vals(tcx, test_hi, pat_lo, param_env)?; | ||
|
||
match (test_end, pat_end, lo, hi) { | ||
// pat < test | ||
(_, _, Greater, _) | | ||
(_, Excluded, Equal, _) | | ||
// pat > test | ||
(_, _, _, Less) | | ||
(Excluded, _, _, Equal) => Some(true), | ||
_ => Some(false), | ||
} | ||
})(); | ||
|
||
if no_overlap == Some(true) { | ||
// Testing range does not overlap with pattern range, | ||
// so the pattern can be matched only if this test fails. | ||
resulting_candidates[1].push(candidate.clone()); | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
(&TestKind::Range { | ||
lo, hi, ty, end | ||
}, &PatternKind::Constant { | ||
ref value | ||
}) => { | ||
if self.const_range_contains(ty, lo, hi, end, value) == Some(false) { | ||
// `value` is not contained in the testing range, | ||
// so `value` can be matched only if this test fails. | ||
resulting_candidates[1].push(candidate.clone()); | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
(&TestKind::Range { .. }, _) => false, | ||
|
||
|
||
(&TestKind::Eq { .. }, _) | | ||
(&TestKind::Range { .. }, _) | | ||
(&TestKind::Len { .. }, _) => { | ||
// These are all binary tests. | ||
// | ||
|
@@ -722,6 +812,29 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { | |
"simplifyable pattern found: {:?}", | ||
match_pair.pattern) | ||
} | ||
|
||
fn const_range_contains( | ||
&self, | ||
ty: Ty<'tcx>, | ||
lo: &'tcx ty::Const<'tcx>, | ||
hi: &'tcx ty::Const<'tcx>, | ||
end: RangeEnd, | ||
value: &'tcx ty::Const<'tcx>, | ||
) -> Option<bool> { | ||
use std::cmp::Ordering::*; | ||
|
||
let param_env = ty::ParamEnv::empty().and(ty); | ||
let tcx = self.hir.tcx(); | ||
|
||
let a = compare_const_vals(tcx, lo, value, param_env)?; | ||
let b = compare_const_vals(tcx, value, hi, param_env)?; | ||
|
||
match (b, end) { | ||
(Less, _) | | ||
(Equal, RangeEnd::Included) if a != Greater => Some(true), | ||
_ => Some(false), | ||
} | ||
} | ||
} | ||
|
||
fn is_switch_ty<'tcx>(ty: Ty<'tcx>) -> bool { | ||
|
This file contains hidden or 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 hidden or 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,83 @@ | ||
#![feature(exclusive_range_pattern)] | ||
|
||
// run-pass | ||
|
||
fn main() { | ||
let incl_range = |x, b| { | ||
match x { | ||
0..=5 if b => 0, | ||
5..=10 if b => 1, | ||
1..=4 if !b => 2, | ||
_ => 3, | ||
} | ||
}; | ||
assert_eq!(incl_range(3, false), 2); | ||
assert_eq!(incl_range(3, true), 0); | ||
assert_eq!(incl_range(5, false), 3); | ||
assert_eq!(incl_range(5, true), 0); | ||
|
||
let excl_range = |x, b| { | ||
match x { | ||
0..5 if b => 0, | ||
5..10 if b => 1, | ||
1..4 if !b => 2, | ||
_ => 3, | ||
} | ||
}; | ||
assert_eq!(excl_range(3, false), 2); | ||
assert_eq!(excl_range(3, true), 0); | ||
assert_eq!(excl_range(5, false), 3); | ||
assert_eq!(excl_range(5, true), 1); | ||
|
||
let incl_range_vs_const = |x, b| { | ||
match x { | ||
0..=5 if b => 0, | ||
7 => 1, | ||
3 => 2, | ||
_ => 3, | ||
} | ||
}; | ||
assert_eq!(incl_range_vs_const(5, false), 3); | ||
assert_eq!(incl_range_vs_const(5, true), 0); | ||
assert_eq!(incl_range_vs_const(3, false), 2); | ||
assert_eq!(incl_range_vs_const(3, true), 0); | ||
assert_eq!(incl_range_vs_const(7, false), 1); | ||
assert_eq!(incl_range_vs_const(7, true), 1); | ||
|
||
let excl_range_vs_const = |x, b| { | ||
match x { | ||
0..5 if b => 0, | ||
7 => 1, | ||
3 => 2, | ||
_ => 3, | ||
} | ||
}; | ||
assert_eq!(excl_range_vs_const(5, false), 3); | ||
assert_eq!(excl_range_vs_const(5, true), 3); | ||
assert_eq!(excl_range_vs_const(3, false), 2); | ||
assert_eq!(excl_range_vs_const(3, true), 0); | ||
assert_eq!(excl_range_vs_const(7, false), 1); | ||
assert_eq!(excl_range_vs_const(7, true), 1); | ||
|
||
let const_vs_incl_range = |x, b| { | ||
match x { | ||
3 if b => 0, | ||
5..=7 => 2, | ||
1..=4 => 1, | ||
_ => 3, | ||
} | ||
}; | ||
assert_eq!(const_vs_incl_range(3, false), 1); | ||
assert_eq!(const_vs_incl_range(3, true), 0); | ||
|
||
let const_vs_excl_range = |x, b| { | ||
match x { | ||
3 if b => 0, | ||
5..7 => 2, | ||
1..4 => 1, | ||
_ => 3, | ||
} | ||
}; | ||
assert_eq!(const_vs_excl_range(3, false), 1); | ||
assert_eq!(const_vs_excl_range(3, true), 0); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.