Skip to content

Commit 6b8bfef

Browse files
committed
Add IntRange::to_pat and use it instead of custom display()
1 parent 4232816 commit 6b8bfef

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,7 @@ impl<'tcx> Constructor<'tcx> {
956956
end,
957957
}),
958958
IntRange(range) => {
959-
// TODO: do it more directly
960-
return range.clone().into_ctor(cx.tcx).apply(cx, ty, None.into_iter());
959+
return range.to_pat(cx.tcx);
961960
}
962961
NonExhaustive => PatKind::Wild,
963962
};
@@ -1398,19 +1397,6 @@ impl<'tcx> IntRange<'tcx> {
13981397
}
13991398
}
14001399

1401-
/// Converts an `IntRange` to a `ConstantValue` or inclusive `ConstantRange`.
1402-
/// TODO: Deprecated
1403-
fn into_ctor(self, tcx: TyCtxt<'tcx>) -> Constructor<'tcx> {
1404-
let bias = IntRange::signed_bias(tcx, self.ty);
1405-
let (lo, hi) = self.range.into_inner();
1406-
if lo == hi {
1407-
let ty = ty::ParamEnv::empty().and(self.ty);
1408-
ConstantValue(ty::Const::from_bits(tcx, lo ^ bias, ty), self.span)
1409-
} else {
1410-
ConstantRange(lo ^ bias, hi ^ bias, self.ty, RangeEnd::Included, self.span)
1411-
}
1412-
}
1413-
14141400
/// Returns a collection of ranges that spans the values covered by `ranges`, subtracted
14151401
/// by the values covered by `self`: i.e., `ranges \ self` (in set notation).
14161402
fn subtract_from(self, ranges: Vec<IntRange<'tcx>>) -> Vec<IntRange<'tcx>> {
@@ -1474,7 +1460,7 @@ impl<'tcx> IntRange<'tcx> {
14741460
(lo == other_hi || hi == other_lo)
14751461
}
14761462

1477-
fn display(&self, tcx: TyCtxt<'tcx>) -> String {
1463+
fn to_pat(&self, tcx: TyCtxt<'tcx>) -> Pat<'tcx> {
14781464
let (lo, hi) = (self.range.start(), self.range.end());
14791465

14801466
let bias = IntRange::signed_bias(tcx, self.ty);
@@ -1484,11 +1470,14 @@ impl<'tcx> IntRange<'tcx> {
14841470
let lo_const = ty::Const::from_bits(tcx, lo, ty);
14851471
let hi_const = ty::Const::from_bits(tcx, hi, ty);
14861472

1487-
if lo == hi {
1488-
format!("{}", lo_const)
1473+
let kind = if lo == hi {
1474+
PatKind::Constant { value: lo_const }
14891475
} else {
1490-
format!("{}{}{}", lo_const, RangeEnd::Included, hi_const)
1491-
}
1476+
PatKind::Range(PatRange { lo: lo_const, hi: hi_const, end: RangeEnd::Included })
1477+
};
1478+
1479+
// This is a brand new pattern, so we don't reuse `self.span`.
1480+
Pat { ty: self.ty, span: DUMMY_SP, kind: Box::new(kind) }
14921481
}
14931482
}
14941483

@@ -2137,7 +2126,7 @@ fn lint_overlapping_patterns(
21372126
int_range.span,
21382127
&format!(
21392128
"this range overlaps on `{}`",
2140-
IntRange { range: int_range.range, ty, span: DUMMY_SP }.display(tcx),
2129+
IntRange { range: int_range.range, ty, span: DUMMY_SP }.to_pat(tcx),
21412130
),
21422131
);
21432132
}

0 commit comments

Comments
 (0)