diff options
| author | kennytm <kennytm@gmail.com> | 2019-02-20 01:13:47 +0800 |
|---|---|---|
| committer | kennytm <kennytm@gmail.com> | 2019-02-20 11:59:23 +0800 |
| commit | 95daca4aaf9ec8e4f1dd2188147c8958b1d1467f (patch) | |
| tree | 21701d5b5e079385ac0c1c701ef2bf79aedc5e23 | |
| parent | 2f9fa199d260138ba28178585a5c12df2d230588 (diff) | |
| parent | 94b6bf240a2e78c2f97d33adf433d0c9c03a11c4 (diff) | |
Rollup merge of #58569 - kenta7777:reduce-code-repetition, r=oli-obk
Reduce Some Code Repetitions like `(n << amt) >> amt` This Pull Request is related to [#49937](https://github.com/rust-lang/rust/issues/49937). This Pull Request has reduced repetition of `(n << amt) >> amt`.
| -rw-r--r-- | src/librustc_mir/interpret/operand.rs | 10 |
1 files changed, 4 insertions, 6 deletions
diff --git a/src/librustc_mir/interpret/operand.rs b/src/librustc_mir/interpret/operand.rs index 7da907028ee..c6902ddb3c6 100644 --- a/src/librustc_mir/interpret/operand.rs +++ b/src/librustc_mir/interpret/operand.rs @@ -10,6 +10,7 @@ use rustc::mir::interpret::{ GlobalId, AllocId, InboundsCheck, ConstValue, Pointer, Scalar, EvalResult, EvalErrorKind, + sign_extend, truncate, }; use super::{ EvalContext, Machine, AllocMap, Allocation, AllocationExtra, @@ -633,20 +634,17 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> Err(_) => return err!(InvalidDiscriminant(raw_discr.erase_tag())), }; let real_discr = if discr_val.layout.ty.is_signed() { - let i = bits_discr as i128; // going from layout tag type to typeck discriminant type // requires first sign extending with the layout discriminant - let shift = 128 - discr_val.layout.size.bits(); - let sexted = (i << shift) >> shift; + let sexted = sign_extend(bits_discr, discr_val.layout.size) as i128; // and then zeroing with the typeck discriminant type let discr_ty = rval.layout.ty .ty_adt_def().expect("tagged layout corresponds to adt") .repr .discr_type(); - let discr_ty = layout::Integer::from_attr(self, discr_ty); - let shift = 128 - discr_ty.size().bits(); + let size = layout::Integer::from_attr(self, discr_ty).size(); let truncatee = sexted as u128; - (truncatee << shift) >> shift + truncate(truncatee, size) } else { bits_discr }; |
