Skip to content

Commit 60021ce

Browse files
committed
Cleanup constructor_intersects_pattern
1 parent 94227f5 commit 60021ce

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,7 +1910,10 @@ fn constructor_intersects_pattern<'p, 'tcx>(
19101910
ctor: &Constructor<'tcx>,
19111911
pat: &'p Pat<'tcx>,
19121912
) -> Option<PatStack<'p, 'tcx>> {
1913-
if should_treat_range_exhaustively(tcx, ctor) {
1913+
trace!("constructor_intersects_pattern {:#?}, {:#?}", ctor, pat);
1914+
if let Single = ctor {
1915+
Some(PatStack::default())
1916+
} else if should_treat_range_exhaustively(tcx, ctor) {
19141917
let range = match *pat.kind {
19151918
PatKind::Constant { value } => ConstantValue(value),
19161919
PatKind::Range(PatRange { lo, hi, end }) => ConstantRange(lo, hi, end),
@@ -1929,34 +1932,22 @@ fn constructor_intersects_pattern<'p, 'tcx>(
19291932
// Fallback for non-ranges and ranges that involve floating-point numbers, which are not
19301933
// conveniently handled by `IntRange`. For these cases, the constructor may not be a range
19311934
// so intersection actually devolves into being covered by the pattern.
1932-
let (from, to, end) = match *pat.kind {
1935+
let (pat_from, pat_to, pat_end) = match *pat.kind {
19331936
PatKind::Constant { value } => (value, value, RangeEnd::Included),
19341937
PatKind::Range(PatRange { lo, hi, end }) => (lo, hi, end),
19351938
_ => bug!("`constructor_intersects_pattern` called with {:?}", pat),
19361939
};
1937-
let ty = from.ty;
1938-
trace!("constructor_intersects_pattern {:#?}, {:#?}, {:#?}, {}", ctor, from, to, ty);
1939-
let cmp_from = |c_from| compare_const_vals(tcx, c_from, from, param_env, ty);
1940-
let cmp_to = |c_to| compare_const_vals(tcx, c_to, to, param_env, ty);
1941-
let result = match *ctor {
1942-
ConstantValue(value) => {
1943-
let to = cmp_to(value)?;
1944-
let from = cmp_from(value)?;
1945-
let end =
1946-
(to == Ordering::Less) || (end == RangeEnd::Included && to == Ordering::Equal);
1947-
(from != Ordering::Less) && end
1948-
}
1949-
ConstantRange(from, to, range_end) => {
1950-
let to = cmp_to(to)?;
1951-
let from = cmp_from(from)?;
1952-
let end = (to == Ordering::Less) || (end == range_end && to == Ordering::Equal);
1953-
(from != Ordering::Less) && end
1954-
}
1955-
Single => true,
1956-
_ => bug!(),
1940+
let (ctor_from, ctor_to, ctor_end) = match *ctor {
1941+
ConstantValue(value) => (value, value, RangeEnd::Included),
1942+
ConstantRange(from, to, range_end) => (from, to, range_end),
1943+
_ => bug!("`constructor_intersects_pattern` called with {:?}", ctor),
19571944
};
1958-
1959-
if result { Some(PatStack::default()) } else { None }
1945+
let order_to = compare_const_vals(tcx, ctor_to, pat_to, param_env, pat_from.ty)?;
1946+
let order_from = compare_const_vals(tcx, ctor_from, pat_from, param_env, pat_from.ty)?;
1947+
let intersects = (order_from != Ordering::Less)
1948+
&& ((order_to == Ordering::Less)
1949+
|| (pat_end == ctor_end && order_to == Ordering::Equal));
1950+
if intersects { Some(PatStack::default()) } else { None }
19601951
}
19611952
}
19621953

0 commit comments

Comments
 (0)