Skip to content

Commit effb3d0

Browse files
committed
Improve the comments
1 parent a9f2c5a commit effb3d0

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -470,20 +470,21 @@ fn all_constructors<'a, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
470470
]
471471
}
472472
ty::TyInt(_) if exhaustive_integer_patterns => {
473-
let size = cx.tcx.layout_of(ty::ParamEnv::reveal_all().and(pcx.ty))
473+
// FIXME(49937): refactor these bit manipulations into interpret.
474+
let bits = cx.tcx.layout_of(ty::ParamEnv::reveal_all().and(pcx.ty))
474475
.unwrap().size.bits() as u128;
475-
let min = (1u128 << (size - 1)).wrapping_neg();
476-
let max = (1u128 << (size - 1)).wrapping_sub(1);
476+
let min = 1u128 << (bits - 1);
477+
let max = (1u128 << (bits - 1)) - 1;
477478
value_constructors = true;
478479
vec![ConstantRange(ty::Const::from_bits(cx.tcx, min as u128, pcx.ty),
479480
ty::Const::from_bits(cx.tcx, max as u128, pcx.ty),
480481
RangeEnd::Included)]
481482
}
482483
ty::TyUint(_) if exhaustive_integer_patterns => {
483-
let size = cx.tcx.layout_of(ty::ParamEnv::reveal_all().and(pcx.ty))
484+
// FIXME(49937): refactor these bit manipulations into interpret.
485+
let bits = cx.tcx.layout_of(ty::ParamEnv::reveal_all().and(pcx.ty))
484486
.unwrap().size.bits() as u32;
485-
let shift = 1u128.overflowing_shl(size);
486-
let max = shift.0.wrapping_sub(1 + (shift.1 as u128));
487+
let max = (!0u128).wrapping_shr(128 - bits);
487488
value_constructors = true;
488489
vec![ConstantRange(ty::Const::from_bits(cx.tcx, 0u128, pcx.ty),
489490
ty::Const::from_bits(cx.tcx, max as u128, pcx.ty),
@@ -603,8 +604,12 @@ fn max_slice_length<'p, 'a: 'p, 'tcx: 'a, I>(
603604
}
604605

605606
/// An inclusive interval, used for precise integer exhaustiveness checking.
606-
/// `Interval`s always store a contiguous range of integers. That means that
607-
/// signed integers are offset (see `offset_sign`) by their minimum value.
607+
/// `Interval`s always store a contiguous range of integers. This means that
608+
/// signed values are encoded by offsetting them such that `0` represents the
609+
/// minimum value for the integer, regardless of sign.
610+
/// For example, the range `-128...127` is encoded as `0...255`.
611+
/// This makes comparisons and arithmetic on interval endpoints much more
612+
/// straightforward. See `offset_sign` for the conversion technique.
608613
struct Interval<'tcx> {
609614
pub range: RangeInclusive<u128>,
610615
pub ty: Ty<'tcx>,
@@ -661,10 +666,11 @@ impl<'tcx> Interval<'tcx> {
661666
let (lo, hi) = range.into_inner();
662667
match ty.sty {
663668
ty::TyInt(_) => {
664-
let size = tcx.layout_of(ty::ParamEnv::reveal_all().and(ty))
669+
// FIXME(49937): refactor these bit manipulations into interpret.
670+
let bits = tcx.layout_of(ty::ParamEnv::reveal_all().and(ty))
665671
.unwrap().size.bits() as u128;
666-
let min = (1u128 << (size - 1)).wrapping_neg();
667-
let shift = 1u128.overflowing_shl(size as u32);
672+
let min = 1u128 << (bits - 1);
673+
let shift = 1u128.overflowing_shl(bits as u32);
668674
let mask = shift.0.wrapping_sub(1 + (shift.1 as u128));
669675
if encode {
670676
let offset = |x: u128| x.wrapping_sub(min) & mask;

0 commit comments

Comments
 (0)