Skip to content

Commit eafc9db

Browse files
committed
Inline now-trivial IntRange::from_ctor
1 parent ddf0e85 commit eafc9db

File tree

1 file changed

+28
-56
lines changed

1 file changed

+28
-56
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 28 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -649,12 +649,7 @@ impl<'tcx> Constructor<'tcx> {
649649

650650
// Returns the set of constructors covered by `self` but not by
651651
// anything in `other_ctors`.
652-
fn subtract_ctors(
653-
&self,
654-
tcx: TyCtxt<'tcx>,
655-
param_env: ty::ParamEnv<'tcx>,
656-
other_ctors: &Vec<Constructor<'tcx>>,
657-
) -> Vec<Constructor<'tcx>> {
652+
fn subtract_ctors(&self, other_ctors: &Vec<Constructor<'tcx>>) -> Vec<Constructor<'tcx>> {
658653
match self {
659654
// Those constructors can only match themselves.
660655
Single | Variant(_) => {
@@ -737,22 +732,22 @@ impl<'tcx> Constructor<'tcx> {
737732
}
738733
IntRange(self_range) => {
739734
let mut remaining_ranges = vec![self_range.clone()];
740-
let other_ranges =
741-
other_ctors.into_iter().filter_map(|c| IntRange::from_ctor(tcx, param_env, c));
742-
for other_range in other_ranges {
743-
if other_range == *self_range {
744-
// If the `self` range appears directly in a `match` arm, we can
745-
// eliminate it straight away.
746-
remaining_ranges = vec![];
747-
} else {
748-
// Otherwise explicitely compute the remaining ranges.
749-
remaining_ranges = other_range.subtract_from(remaining_ranges);
750-
}
735+
for other_ctor in other_ctors {
736+
if let IntRange(other_range) = other_ctor {
737+
if other_range == self_range {
738+
// If the `self` range appears directly in a `match` arm, we can
739+
// eliminate it straight away.
740+
remaining_ranges = vec![];
741+
} else {
742+
// Otherwise explicitely compute the remaining ranges.
743+
remaining_ranges = other_range.subtract_from(remaining_ranges);
744+
}
751745

752-
// If the ranges that have been considered so far already cover the entire
753-
// range of values, we can return early.
754-
if remaining_ranges.is_empty() {
755-
break;
746+
// If the ranges that have been considered so far already cover the entire
747+
// range of values, we can return early.
748+
if remaining_ranges.is_empty() {
749+
break;
750+
}
756751
}
757752
}
758753

@@ -763,7 +758,7 @@ impl<'tcx> Constructor<'tcx> {
763758
if other_ctors.iter().any(|c| {
764759
c == self
765760
// FIXME(Nadrieril): This condition looks fishy
766-
|| IntRange::from_ctor(tcx, param_env, c).is_some()
761+
|| c.is_integral_range()
767762
}) {
768763
vec![]
769764
} else {
@@ -1310,26 +1305,15 @@ impl<'tcx> IntRange<'tcx> {
13101305
}
13111306
}
13121307

1313-
fn from_ctor(
1314-
_tcx: TyCtxt<'tcx>,
1315-
_param_env: ty::ParamEnv<'tcx>,
1316-
ctor: &Constructor<'tcx>,
1317-
) -> Option<IntRange<'tcx>> {
1318-
// Floating-point ranges are permitted and we don't want
1319-
// to consider them when constructing integer ranges.
1320-
match ctor {
1321-
IntRange(range) => Some(range.clone()),
1322-
_ => None,
1323-
}
1324-
}
1325-
13261308
fn from_pat(
13271309
tcx: TyCtxt<'tcx>,
13281310
param_env: ty::ParamEnv<'tcx>,
13291311
pat: &Pat<'tcx>,
13301312
) -> Option<IntRange<'tcx>> {
1331-
let ctor = pat_constructor(tcx, param_env, pat)?;
1332-
IntRange::from_ctor(tcx, param_env, &ctor)
1313+
match pat_constructor(tcx, param_env, pat)? {
1314+
IntRange(range) => Some(range),
1315+
_ => None,
1316+
}
13331317
}
13341318

13351319
// The return value of `signed_bias` should be XORed with an endpoint to encode/decode it.
@@ -1436,20 +1420,13 @@ impl<'tcx> std::cmp::PartialEq for IntRange<'tcx> {
14361420

14371421
// A struct to compute a set of constructors equivalent to `all_ctors \ used_ctors`.
14381422
struct MissingConstructors<'tcx> {
1439-
tcx: TyCtxt<'tcx>,
1440-
param_env: ty::ParamEnv<'tcx>,
14411423
all_ctors: Vec<Constructor<'tcx>>,
14421424
used_ctors: Vec<Constructor<'tcx>>,
14431425
}
14441426

14451427
impl<'tcx> MissingConstructors<'tcx> {
1446-
fn new(
1447-
tcx: TyCtxt<'tcx>,
1448-
param_env: ty::ParamEnv<'tcx>,
1449-
all_ctors: Vec<Constructor<'tcx>>,
1450-
used_ctors: Vec<Constructor<'tcx>>,
1451-
) -> Self {
1452-
MissingConstructors { tcx, param_env, all_ctors, used_ctors }
1428+
fn new(all_ctors: Vec<Constructor<'tcx>>, used_ctors: Vec<Constructor<'tcx>>) -> Self {
1429+
MissingConstructors { all_ctors, used_ctors }
14531430
}
14541431

14551432
fn into_inner(self) -> (Vec<Constructor<'tcx>>, Vec<Constructor<'tcx>>) {
@@ -1467,9 +1444,7 @@ impl<'tcx> MissingConstructors<'tcx> {
14671444

14681445
/// Iterate over all_ctors \ used_ctors
14691446
fn iter<'a>(&'a self) -> impl Iterator<Item = Constructor<'tcx>> + Captures<'a> {
1470-
self.all_ctors.iter().flat_map(move |req_ctor| {
1471-
req_ctor.subtract_ctors(self.tcx, self.param_env, &self.used_ctors)
1472-
})
1447+
self.all_ctors.iter().flat_map(move |req_ctor| req_ctor.subtract_ctors(&self.used_ctors))
14731448
}
14741449
}
14751450

@@ -1610,7 +1585,7 @@ pub fn is_useful<'p, 'a, 'tcx>(
16101585
// non-wildcard patterns in the current column. To determine if
16111586
// the set is empty, we can check that `.peek().is_none()`, so
16121587
// we only fully construct them on-demand, because they're rarely used and can be big.
1613-
let missing_ctors = MissingConstructors::new(cx.tcx, cx.param_env, all_ctors, used_ctors);
1588+
let missing_ctors = MissingConstructors::new(all_ctors, used_ctors);
16141589

16151590
debug!(
16161591
"missing_ctors.empty()={:#?} is_privately_empty={:#?} is_declared_nonexhaustive={:#?}",
@@ -2274,12 +2249,9 @@ fn specialize_one_pattern<'p, 'a: 'p, 'q: 'p, 'tcx>(
22742249
// If the constructor is a:
22752250
// - Single value: add a row if the pattern contains the constructor.
22762251
// - Range: add a row if the constructor intersects the pattern.
2277-
if constructor.is_integral_range() {
2278-
match (
2279-
IntRange::from_ctor(cx.tcx, cx.param_env, constructor),
2280-
IntRange::from_pat(cx.tcx, cx.param_env, pat),
2281-
) {
2282-
(Some(ctor), Some(pat)) => ctor.intersection(cx.tcx, &pat).map(|_| {
2252+
if let IntRange(ctor) = constructor {
2253+
match IntRange::from_pat(cx.tcx, cx.param_env, pat) {
2254+
Some(pat) => ctor.intersection(cx.tcx, &pat).map(|_| {
22832255
// Constructor splitting should ensure that all intersections we encounter
22842256
// are actually inclusions.
22852257
let (pat_lo, pat_hi) = pat.boundaries();

0 commit comments

Comments
 (0)