Skip to content

Commit 6de7afd

Browse files
committed
Prefer IntRange::into_ctor to range_to_ctor
1 parent 0a18610 commit 6de7afd

File tree

1 file changed

+34
-47
lines changed

1 file changed

+34
-47
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,20 +1389,15 @@ impl<'tcx> IntRange<'tcx> {
13891389
}
13901390
}
13911391

1392-
/// Converts a `RangeInclusive` to a `ConstantValue` or inclusive `ConstantRange`.
1393-
fn range_to_ctor(
1394-
tcx: TyCtxt<'tcx>,
1395-
ty: Ty<'tcx>,
1396-
r: RangeInclusive<u128>,
1397-
span: Span,
1398-
) -> Constructor<'tcx> {
1399-
let bias = IntRange::signed_bias(tcx, ty);
1400-
let (lo, hi) = r.into_inner();
1392+
/// Converts an `IntRange` to a `ConstantValue` or inclusive `ConstantRange`.
1393+
fn into_ctor(self, tcx: TyCtxt<'tcx>) -> Constructor<'tcx> {
1394+
let bias = IntRange::signed_bias(tcx, self.ty);
1395+
let (lo, hi) = self.range.into_inner();
14011396
if lo == hi {
1402-
let ty = ty::ParamEnv::empty().and(ty);
1403-
ConstantValue(ty::Const::from_bits(tcx, lo ^ bias, ty), span)
1397+
let ty = ty::ParamEnv::empty().and(self.ty);
1398+
ConstantValue(ty::Const::from_bits(tcx, lo ^ bias, ty), self.span)
14041399
} else {
1405-
ConstantRange(lo ^ bias, hi ^ bias, ty, RangeEnd::Included, span)
1400+
ConstantRange(lo ^ bias, hi ^ bias, self.ty, RangeEnd::Included, self.span)
14061401
}
14071402
}
14081403

@@ -1419,38 +1414,27 @@ impl<'tcx> IntRange<'tcx> {
14191414
.filter_map(|r| IntRange::from_ctor(tcx, param_env, &r).map(|i| i.range));
14201415
let mut remaining_ranges = vec![];
14211416
let ty = self.ty;
1417+
let span = self.span;
14221418
let (lo, hi) = self.range.into_inner();
14231419
for subrange in ranges {
14241420
let (subrange_lo, subrange_hi) = subrange.into_inner();
14251421
if lo > subrange_hi || subrange_lo > hi {
14261422
// The pattern doesn't intersect with the subrange at all,
14271423
// so the subrange remains untouched.
1428-
remaining_ranges.push(Self::range_to_ctor(
1429-
tcx,
1430-
ty,
1431-
subrange_lo..=subrange_hi,
1432-
self.span,
1433-
));
1424+
remaining_ranges
1425+
.push(IntRange { range: subrange_lo..=subrange_hi, ty, span }.into_ctor(tcx));
14341426
} else {
14351427
if lo > subrange_lo {
14361428
// The pattern intersects an upper section of the
14371429
// subrange, so a lower section will remain.
1438-
remaining_ranges.push(Self::range_to_ctor(
1439-
tcx,
1440-
ty,
1441-
subrange_lo..=(lo - 1),
1442-
self.span,
1443-
));
1430+
remaining_ranges
1431+
.push(IntRange { range: subrange_lo..=(lo - 1), ty, span }.into_ctor(tcx));
14441432
}
14451433
if hi < subrange_hi {
14461434
// The pattern intersects a lower section of the
14471435
// subrange, so an upper section will remain.
1448-
remaining_ranges.push(Self::range_to_ctor(
1449-
tcx,
1450-
ty,
1451-
(hi + 1)..=subrange_hi,
1452-
self.span,
1453-
));
1436+
remaining_ranges
1437+
.push(IntRange { range: (hi + 1)..=subrange_hi, ty, span }.into_ctor(tcx));
14541438
}
14551439
}
14561440
}
@@ -1964,23 +1948,24 @@ fn split_grouped_constructors<'p, 'tcx>(
19641948
// We're going to iterate through every adjacent pair of borders, making sure that
19651949
// each represents an interval of nonnegative length, and convert each such
19661950
// interval into a constructor.
1967-
for IntRange { range, .. } in
1968-
borders.windows(2).filter_map(|window| match (window[0], window[1]) {
1969-
(Border::JustBefore(n), Border::JustBefore(m)) => {
1970-
if n < m {
1971-
Some(IntRange { range: n..=(m - 1), ty, span })
1972-
} else {
1973-
None
1951+
split_ctors.extend(
1952+
borders
1953+
.windows(2)
1954+
.filter_map(|window| match (window[0], window[1]) {
1955+
(Border::JustBefore(n), Border::JustBefore(m)) => {
1956+
if n < m {
1957+
Some(IntRange { range: n..=(m - 1), ty, span })
1958+
} else {
1959+
None
1960+
}
19741961
}
1975-
}
1976-
(Border::JustBefore(n), Border::AfterMax) => {
1977-
Some(IntRange { range: n..=u128::MAX, ty, span })
1978-
}
1979-
(Border::AfterMax, _) => None,
1980-
})
1981-
{
1982-
split_ctors.push(IntRange::range_to_ctor(tcx, ty, range, span));
1983-
}
1962+
(Border::JustBefore(n), Border::AfterMax) => {
1963+
Some(IntRange { range: n..=u128::MAX, ty, span })
1964+
}
1965+
(Border::AfterMax, _) => None,
1966+
})
1967+
.map(|range| range.into_ctor(tcx)),
1968+
);
19841969
}
19851970
VarLenSlice(self_prefix, self_suffix) => {
19861971
// The exhaustiveness-checking paper does not include any details on
@@ -2127,7 +2112,9 @@ fn lint_overlapping_patterns(
21272112
int_range.span,
21282113
&format!(
21292114
"this range overlaps on `{}`",
2130-
IntRange::range_to_ctor(tcx, ty, int_range.range, DUMMY_SP).display(tcx),
2115+
IntRange { range: int_range.range, ty, span: DUMMY_SP }
2116+
.into_ctor(tcx)
2117+
.display(tcx),
21312118
),
21322119
);
21332120
}

0 commit comments

Comments
 (0)