Skip to content

Commit 1f8d93c

Browse files
committed
Extract constructor_intersects_pattern function
This reverses changes from rust-lang#65089, but I need this temporarily for rebase to be tractable.
1 parent 2687140 commit 1f8d93c

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,35 @@ fn should_treat_range_exhaustively(tcx: TyCtxt<'tcx>, ctor: &Constructor<'tcx>)
18251825
}
18261826
}
18271827

1828+
fn constructor_intersects_pattern<'p, 'tcx>(
1829+
tcx: TyCtxt<'tcx>,
1830+
param_env: ty::ParamEnv<'tcx>,
1831+
ctor: &Constructor<'tcx>,
1832+
pat: &'p Pat<'tcx>,
1833+
) -> Option<PatStack<'p, 'tcx>> {
1834+
if should_treat_range_exhaustively(tcx, ctor) {
1835+
match (IntRange::from_ctor(tcx, param_env, ctor), IntRange::from_pat(tcx, param_env, pat)) {
1836+
(Some(ctor), Some(pat)) => ctor.intersection(&pat).map(|_| {
1837+
let (pat_lo, pat_hi) = pat.range.into_inner();
1838+
let (ctor_lo, ctor_hi) = ctor.range.into_inner();
1839+
assert!(pat_lo <= ctor_lo && ctor_hi <= pat_hi);
1840+
PatStack::default()
1841+
}),
1842+
_ => None,
1843+
}
1844+
} else {
1845+
// Fallback for non-ranges and ranges that involve
1846+
// floating-point numbers, which are not conveniently handled
1847+
// by `IntRange`. For these cases, the constructor may not be a
1848+
// range so intersection actually devolves into being covered
1849+
// by the pattern.
1850+
match constructor_covered_by_range(tcx, param_env, ctor, pat) {
1851+
Ok(true) => Some(PatStack::default()),
1852+
Ok(false) | Err(ErrorReported) => None,
1853+
}
1854+
}
1855+
}
1856+
18281857
fn constructor_covered_by_range<'tcx>(
18291858
tcx: TyCtxt<'tcx>,
18301859
param_env: ty::ParamEnv<'tcx>,
@@ -2016,30 +2045,7 @@ fn specialize_one_pattern<'p, 'a: 'p, 'p2: 'p, 'tcx>(
20162045
// If the constructor is a:
20172046
// - Single value: add a row if the pattern contains the constructor.
20182047
// - Range: add a row if the constructor intersects the pattern.
2019-
if should_treat_range_exhaustively(cx.tcx, constructor) {
2020-
match (
2021-
IntRange::from_ctor(cx.tcx, cx.param_env, constructor),
2022-
IntRange::from_pat(cx.tcx, cx.param_env, pat),
2023-
) {
2024-
(Some(ctor), Some(pat)) => ctor.intersection(&pat).map(|_| {
2025-
let (pat_lo, pat_hi) = pat.range.into_inner();
2026-
let (ctor_lo, ctor_hi) = ctor.range.into_inner();
2027-
assert!(pat_lo <= ctor_lo && ctor_hi <= pat_hi);
2028-
PatStack::default()
2029-
}),
2030-
_ => None,
2031-
}
2032-
} else {
2033-
// Fallback for non-ranges and ranges that involve
2034-
// floating-point numbers, which are not conveniently handled
2035-
// by `IntRange`. For these cases, the constructor may not be a
2036-
// range so intersection actually devolves into being covered
2037-
// by the pattern.
2038-
match constructor_covered_by_range(cx.tcx, cx.param_env, constructor, pat) {
2039-
Ok(true) => Some(PatStack::default()),
2040-
Ok(false) | Err(ErrorReported) => None,
2041-
}
2042-
}
2048+
constructor_intersects_pattern(cx.tcx, cx.param_env, constructor, pat)
20432049
}
20442050

20452051
PatKind::Array { ref prefix, ref slice, ref suffix }

0 commit comments

Comments
 (0)