Skip to content

Commit 79efdb4

Browse files
committed
Remove unnecessary Const conversion
1 parent a2624f2 commit 79efdb4

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,63 +1387,48 @@ fn all_constructors<'a, 'tcx>(
13871387
.map(|v| Variant(v.def_id))
13881388
.collect(),
13891389
ty::Char => {
1390-
let param_env = ty::ParamEnv::empty().and(cx.tcx.types.char);
1391-
let to_const = |x| ty::Const::from_bits(cx.tcx, x as u128, param_env);
1390+
let to_const = |x| x;
13921391
vec![
13931392
// The valid Unicode Scalar Value ranges.
13941393
IntRange(
13951394
IntRange::from_range(
13961395
cx.tcx,
1397-
cx.param_env,
1398-
to_const('\u{0000}'),
1399-
to_const('\u{D7FF}'),
1400-
&RangeEnd::Included,
1396+
cx.tcx.types.char,
1397+
to_const('\u{0000}' as u128),
1398+
to_const('\u{D7FF}' as u128),
1399+
RangeEnd::Included,
14011400
)
14021401
.unwrap(),
14031402
),
14041403
IntRange(
14051404
IntRange::from_range(
14061405
cx.tcx,
1407-
cx.param_env,
1408-
to_const('\u{E000}'),
1409-
to_const('\u{10FFFF}'),
1410-
&RangeEnd::Included,
1406+
cx.tcx.types.char,
1407+
to_const('\u{E000}' as u128),
1408+
to_const('\u{10FFFF}' as u128),
1409+
RangeEnd::Included,
14111410
)
14121411
.unwrap(),
14131412
),
14141413
]
14151414
}
14161415
ty::Int(ity) => {
1417-
let param_env = ty::ParamEnv::empty().and(ty);
1418-
let to_const = |x| ty::Const::from_bits(cx.tcx, x, param_env);
1416+
let to_const = |x| x;
14191417
let bits = Integer::from_attr(&cx.tcx, SignedInt(ity)).size().bits() as u128;
14201418
let min = 1u128 << (bits - 1);
14211419
let max = min - 1;
14221420
vec![IntRange(
1423-
IntRange::from_range(
1424-
cx.tcx,
1425-
cx.param_env,
1426-
to_const(min),
1427-
to_const(max),
1428-
&RangeEnd::Included,
1429-
)
1430-
.unwrap(),
1421+
IntRange::from_range(cx.tcx, ty, to_const(min), to_const(max), RangeEnd::Included)
1422+
.unwrap(),
14311423
)]
14321424
}
14331425
ty::Uint(uty) => {
1434-
let param_env = ty::ParamEnv::empty().and(ty);
1435-
let to_const = |x| ty::Const::from_bits(cx.tcx, x, param_env);
1426+
let to_const = |x| x;
14361427
let size = Integer::from_attr(&cx.tcx, UnsignedInt(uty)).size();
14371428
let max = truncate(u128::max_value(), size);
14381429
vec![IntRange(
1439-
IntRange::from_range(
1440-
cx.tcx,
1441-
cx.param_env,
1442-
to_const(0),
1443-
to_const(max),
1444-
&RangeEnd::Included,
1445-
)
1446-
.unwrap(),
1430+
IntRange::from_range(cx.tcx, ty, to_const(0), to_const(max), RangeEnd::Included)
1431+
.unwrap(),
14471432
)]
14481433
}
14491434
_ => {
@@ -1535,7 +1520,7 @@ impl<'tcx> IntRange<'tcx> {
15351520
}
15361521

15371522
#[inline]
1538-
fn from_range(
1523+
fn from_const_range(
15391524
tcx: TyCtxt<'tcx>,
15401525
param_env: ty::ParamEnv<'tcx>,
15411526
lo: &Const<'tcx>,
@@ -1545,16 +1530,27 @@ impl<'tcx> IntRange<'tcx> {
15451530
let ty = lo.ty;
15461531
let lo = lo.eval_bits(tcx, param_env, lo.ty);
15471532
let hi = hi.eval_bits(tcx, param_env, hi.ty);
1533+
Self::from_range(tcx, ty, lo, hi, *end)
1534+
}
1535+
1536+
#[inline]
1537+
fn from_range(
1538+
tcx: TyCtxt<'tcx>,
1539+
ty: Ty<'tcx>,
1540+
lo: u128,
1541+
hi: u128,
1542+
end: RangeEnd,
1543+
) -> Option<IntRange<'tcx>> {
15481544
if Self::is_integral(ty) {
15491545
// Perform a shift if the underlying types are signed,
15501546
// which makes the interval arithmetic simpler.
15511547
let bias = IntRange::signed_bias(tcx, ty);
15521548
let (lo, hi) = (lo ^ bias, hi ^ bias);
15531549
// Make sure the interval is well-formed.
1554-
if lo > hi || lo == hi && *end == RangeEnd::Excluded {
1550+
if lo > hi || lo == hi && end == RangeEnd::Excluded {
15551551
None
15561552
} else {
1557-
let offset = (*end == RangeEnd::Excluded) as u128;
1553+
let offset = (end == RangeEnd::Excluded) as u128;
15581554
Some(IntRange { range: lo..=(hi - offset), ty })
15591555
}
15601556
} else {
@@ -1851,7 +1847,7 @@ fn pat_constructors<'tcx>(
18511847
}
18521848
}
18531849
PatKind::Range(PatRange { lo, hi, end }) => {
1854-
if let Some(range) = IntRange::from_range(tcx, param_env, &lo, &hi, &end) {
1850+
if let Some(range) = IntRange::from_const_range(tcx, param_env, &lo, &hi, &end) {
18551851
smallvec![IntRange(range)]
18561852
} else {
18571853
smallvec![ConstantRange(lo, hi, end)]
@@ -1951,7 +1947,7 @@ fn constructor_intersects_pattern<'p, 'tcx>(
19511947
let pat = match *pat.kind {
19521948
PatKind::Constant { value } => IntRange::from_const(cx.tcx, cx.param_env, value)?,
19531949
PatKind::Range(PatRange { lo, hi, end }) => {
1954-
IntRange::from_range(cx.tcx, cx.param_env, lo, hi, &end)?
1950+
IntRange::from_const_range(cx.tcx, cx.param_env, lo, hi, &end)?
19551951
}
19561952
_ => bug!("`constructor_intersects_pattern` called with {:?}", pat),
19571953
};

0 commit comments

Comments
 (0)