Skip to content

Commit 8c1835d

Browse files
committed
IntRange::from_pat is redundant with pat_constructors
1 parent 34ad52e commit 8c1835d

File tree

1 file changed

+13
-29
lines changed

1 file changed

+13
-29
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,29 +1374,10 @@ impl<'tcx> IntRange<'tcx> {
13741374
fn from_pat(
13751375
tcx: TyCtxt<'tcx>,
13761376
param_env: ty::ParamEnv<'tcx>,
1377-
mut pat: &Pat<'tcx>,
1377+
pat: &Pat<'tcx>,
13781378
) -> Option<IntRange<'tcx>> {
1379-
loop {
1380-
match pat.kind {
1381-
box PatKind::Constant { value } => {
1382-
return Self::from_const(tcx, param_env, value, pat.span);
1383-
}
1384-
box PatKind::Range(PatRange { lo, hi, end }) => {
1385-
return Self::from_range(
1386-
tcx,
1387-
lo.eval_bits(tcx, param_env, lo.ty),
1388-
hi.eval_bits(tcx, param_env, hi.ty),
1389-
&lo.ty,
1390-
&end,
1391-
pat.span,
1392-
);
1393-
}
1394-
box PatKind::AscribeUserType { ref subpattern, .. } => {
1395-
pat = subpattern;
1396-
}
1397-
_ => return None,
1398-
}
1399-
}
1379+
let ctor = pat_constructor(tcx, param_env, pat)?;
1380+
IntRange::from_ctor(tcx, param_env, &ctor)
14001381
}
14011382

14021383
// The return value of `signed_bias` should be XORed with an endpoint to encode/decode it.
@@ -1632,7 +1613,7 @@ pub fn is_useful<'p, 'a, 'tcx>(
16321613

16331614
debug!("is_useful_expand_first_col: pcx={:#?}, expanding {:#?}", pcx, v.head());
16341615

1635-
if let Some(constructor) = pat_constructor(cx, v.head()) {
1616+
if let Some(constructor) = pat_constructor(cx.tcx, cx.param_env, v.head()) {
16361617
debug!("is_useful - expanding constructor: {:#?}", constructor);
16371618
split_grouped_constructors(
16381619
cx.tcx,
@@ -1651,7 +1632,7 @@ pub fn is_useful<'p, 'a, 'tcx>(
16511632
debug!("is_useful - expanding wildcard");
16521633

16531634
let used_ctors: Vec<Constructor<'_>> =
1654-
matrix.heads().filter_map(|p| pat_constructor(cx, p)).collect();
1635+
matrix.heads().filter_map(|p| pat_constructor(cx.tcx, cx.param_env, p)).collect();
16551636
debug!("used_ctors = {:#?}", used_ctors);
16561637
// `all_ctors` are all the constructors for the given type, which
16571638
// should all be represented (or caught with the wild pattern `_`).
@@ -1754,26 +1735,29 @@ fn is_useful_specialized<'p, 'a, 'tcx>(
17541735
/// Determines the constructor that the given pattern can be specialized to.
17551736
/// Returns `None` in case of a catch-all, which can't be specialized.
17561737
fn pat_constructor<'tcx>(
1757-
cx: &mut MatchCheckCtxt<'_, 'tcx>,
1738+
tcx: TyCtxt<'tcx>,
1739+
param_env: ty::ParamEnv<'tcx>,
17581740
pat: &Pat<'tcx>,
17591741
) -> Option<Constructor<'tcx>> {
17601742
match *pat.kind {
1761-
PatKind::AscribeUserType { ref subpattern, .. } => pat_constructor(cx, subpattern),
1743+
PatKind::AscribeUserType { ref subpattern, .. } => {
1744+
pat_constructor(tcx, param_env, subpattern)
1745+
}
17621746
PatKind::Binding { .. } | PatKind::Wild => None,
17631747
PatKind::Leaf { .. } | PatKind::Deref { .. } => Some(Single),
17641748
PatKind::Variant { adt_def, variant_index, .. } => {
17651749
Some(Variant(adt_def.variants[variant_index].def_id))
17661750
}
17671751
PatKind::Constant { value } => Some(ConstantValue(value, pat.span)),
17681752
PatKind::Range(PatRange { lo, hi, end }) => Some(ConstantRange(
1769-
lo.eval_bits(cx.tcx, cx.param_env, lo.ty),
1770-
hi.eval_bits(cx.tcx, cx.param_env, hi.ty),
1753+
lo.eval_bits(tcx, param_env, lo.ty),
1754+
hi.eval_bits(tcx, param_env, hi.ty),
17711755
lo.ty,
17721756
end,
17731757
pat.span,
17741758
)),
17751759
PatKind::Array { .. } => match pat.ty.kind {
1776-
ty::Array(_, length) => Some(FixedLenSlice(length.eval_usize(cx.tcx, cx.param_env))),
1760+
ty::Array(_, length) => Some(FixedLenSlice(length.eval_usize(tcx, param_env))),
17771761
_ => span_bug!(pat.span, "bad ty {:?} for array pattern", pat.ty),
17781762
},
17791763
PatKind::Slice { ref prefix, ref slice, ref suffix } => {

0 commit comments

Comments
 (0)