Don't default fully_matched to false to avoid mistakes

This commit is contained in:
Nadrieril 2024-02-19 21:31:15 +01:00
parent 15072766af
commit 0ca1d220d2

View File

@ -589,8 +589,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// away.) // away.)
let (match_pair_index, match_pair) = let (match_pair_index, match_pair) =
candidate.match_pairs.iter().enumerate().find(|&(_, mp)| mp.place == *test_place)?; candidate.match_pairs.iter().enumerate().find(|&(_, mp)| mp.place == *test_place)?;
let mut fully_matched = false;
let fully_matched;
let ret = match (&test.kind, &match_pair.pattern.kind) { let ret = match (&test.kind, &match_pair.pattern.kind) {
// If we are performing a variant switch, then this // If we are performing a variant switch, then this
// informs variant patterns, but nothing else. // informs variant patterns, but nothing else.
@ -602,8 +602,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
fully_matched = true; fully_matched = true;
Some(variant_index.as_usize()) Some(variant_index.as_usize())
} }
(&TestKind::Switch { .. }, _) => {
(&TestKind::Switch { .. }, _) => None, fully_matched = false;
None
}
// If we are performing a switch over integers, then this informs integer // If we are performing a switch over integers, then this informs integer
// equality, but nothing else. // equality, but nothing else.
@ -617,8 +619,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let index = options.get_index_of(value).unwrap(); let index = options.get_index_of(value).unwrap();
Some(index) Some(index)
} }
(TestKind::SwitchInt { switch_ty: _, options }, PatKind::Range(range)) => { (TestKind::SwitchInt { switch_ty: _, options }, PatKind::Range(range)) => {
fully_matched = false;
let not_contained = let not_contained =
self.values_not_contained_in_range(&*range, options).unwrap_or(false); self.values_not_contained_in_range(&*range, options).unwrap_or(false);
@ -628,8 +630,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
options.len() options.len()
}) })
} }
(&TestKind::SwitchInt { .. }, _) => {
(&TestKind::SwitchInt { .. }, _) => None, fully_matched = false;
None
}
( (
&TestKind::Len { len: test_len, op: BinOp::Eq }, &TestKind::Len { len: test_len, op: BinOp::Eq },
@ -647,21 +651,23 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// test_len < pat_len. If $actual_len = test_len, // test_len < pat_len. If $actual_len = test_len,
// then $actual_len < pat_len and we don't have // then $actual_len < pat_len and we don't have
// enough elements. // enough elements.
fully_matched = false;
Some(1) Some(1)
} }
(Ordering::Equal | Ordering::Greater, &Some(_)) => { (Ordering::Equal | Ordering::Greater, &Some(_)) => {
// This can match both if $actual_len = test_len >= pat_len, // This can match both if $actual_len = test_len >= pat_len,
// and if $actual_len > test_len. We can't advance. // and if $actual_len > test_len. We can't advance.
fully_matched = false;
None None
} }
(Ordering::Greater, &None) => { (Ordering::Greater, &None) => {
// test_len != pat_len, so if $actual_len = test_len, then // test_len != pat_len, so if $actual_len = test_len, then
// $actual_len != pat_len. // $actual_len != pat_len.
fully_matched = false;
Some(1) Some(1)
} }
} }
} }
( (
&TestKind::Len { len: test_len, op: BinOp::Ge }, &TestKind::Len { len: test_len, op: BinOp::Ge },
PatKind::Slice { prefix, slice, suffix }, PatKind::Slice { prefix, slice, suffix },
@ -679,16 +685,19 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// test_len <= pat_len. If $actual_len < test_len, // test_len <= pat_len. If $actual_len < test_len,
// then it is also < pat_len, so the test passing is // then it is also < pat_len, so the test passing is
// necessary (but insufficient). // necessary (but insufficient).
fully_matched = false;
Some(0) Some(0)
} }
(Ordering::Greater, &None) => { (Ordering::Greater, &None) => {
// test_len > pat_len. If $actual_len >= test_len > pat_len, // test_len > pat_len. If $actual_len >= test_len > pat_len,
// then we know we won't have a match. // then we know we won't have a match.
fully_matched = false;
Some(1) Some(1)
} }
(Ordering::Greater, &Some(_)) => { (Ordering::Greater, &Some(_)) => {
// test_len < pat_len, and is therefore less // test_len < pat_len, and is therefore less
// strict. This can still go both ways. // strict. This can still go both ways.
fully_matched = false;
None None
} }
} }
@ -699,13 +708,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
fully_matched = true; fully_matched = true;
Some(0) Some(0)
} else { } else {
fully_matched = false;
// If the testing range does not overlap with pattern range, // If the testing range does not overlap with pattern range,
// the pattern can be matched only if this test fails. // the pattern can be matched only if this test fails.
if !test.overlaps(pat, self.tcx, self.param_env)? { Some(1) } else { None } if !test.overlaps(pat, self.tcx, self.param_env)? { Some(1) } else { None }
} }
} }
(TestKind::Range(range), &PatKind::Constant { value }) => { (TestKind::Range(range), &PatKind::Constant { value }) => {
fully_matched = false;
if !range.contains(value, self.tcx, self.param_env)? { if !range.contains(value, self.tcx, self.param_env)? {
// `value` is not contained in the testing range, // `value` is not contained in the testing range,
// so `value` can be matched only if this test fails. // so `value` can be matched only if this test fails.
@ -714,8 +724,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
None None
} }
} }
(&TestKind::Range { .. }, _) => {
(&TestKind::Range { .. }, _) => None, fully_matched = false;
None
}
(&TestKind::Eq { .. } | &TestKind::Len { .. }, _) => { (&TestKind::Eq { .. } | &TestKind::Len { .. }, _) => {
// The call to `self.test(&match_pair)` below is not actually used to generate any // The call to `self.test(&match_pair)` below is not actually used to generate any
@ -737,6 +749,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
fully_matched = true; fully_matched = true;
Some(0) Some(0)
} else { } else {
fully_matched = false;
None None
} }
} }