1
Fork 0

u128 truncation and sign extension are not just interpreter related

This commit is contained in:
oli 2020-11-04 13:41:58 +00:00
parent e67c768110
commit abacaf2aef
18 changed files with 73 additions and 89 deletions

View file

@ -17,7 +17,6 @@ use crate::build::Builder;
use crate::thir::{self, *};
use rustc_attr::{SignedInt, UnsignedInt};
use rustc_hir::RangeEnd;
use rustc_middle::mir::interpret::truncate;
use rustc_middle::mir::Place;
use rustc_middle::ty;
use rustc_middle::ty::layout::IntegerExt;
@ -161,13 +160,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
ty::Int(ity) => {
let size = Integer::from_attr(&tcx, SignedInt(ity)).size();
let max = truncate(u128::MAX, size);
let max = size.truncate(u128::MAX);
let bias = 1u128 << (size.bits() - 1);
(Some((0, max, size)), bias)
}
ty::Uint(uty) => {
let size = Integer::from_attr(&tcx, UnsignedInt(uty)).size();
let max = truncate(u128::MAX, size);
let max = size.truncate(u128::MAX);
(Some((0, max, size)), 0)
}
_ => (None, 0),

View file

@ -1,6 +1,6 @@
use rustc_ast as ast;
use rustc_middle::mir::interpret::{
truncate, Allocation, ConstValue, LitToConstError, LitToConstInput, Scalar,
Allocation, ConstValue, LitToConstError, LitToConstInput, Scalar,
};
use rustc_middle::ty::{self, ParamEnv, TyCtxt};
use rustc_span::symbol::Symbol;
@ -16,7 +16,7 @@ crate fn lit_to_const<'tcx>(
let param_ty = ParamEnv::reveal_all().and(ty);
let width = tcx.layout_of(param_ty).map_err(|_| LitToConstError::Reported)?.size;
trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
let result = truncate(n, width);
let result = width.truncate(n);
trace!("trunc result: {}", result);
Ok(ConstValue::Scalar(Scalar::from_uint(result, width)))
};

View file

@ -304,7 +304,7 @@ use rustc_arena::TypedArena;
use rustc_attr::{SignedInt, UnsignedInt};
use rustc_hir::def_id::DefId;
use rustc_hir::{HirId, RangeEnd};
use rustc_middle::mir::interpret::{truncate, ConstValue};
use rustc_middle::mir::interpret::ConstValue;
use rustc_middle::mir::Field;
use rustc_middle::ty::layout::IntegerExt;
use rustc_middle::ty::{self, Const, Ty, TyCtxt};
@ -1608,7 +1608,7 @@ fn all_constructors<'p, 'tcx>(pcx: PatCtxt<'_, 'p, 'tcx>) -> Vec<Constructor<'tc
}
&ty::Uint(uty) => {
let size = Integer::from_attr(&cx.tcx, UnsignedInt(uty)).size();
let max = truncate(u128::MAX, size);
let max = size.truncate(u128::MAX);
vec![make_range(0, max)]
}
_ if cx.is_uninhabited(pcx.ty) => vec![],

View file

@ -15,7 +15,7 @@ use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
use rustc_hir::pat_util::EnumerateAndAdjustIterator;
use rustc_hir::RangeEnd;
use rustc_index::vec::Idx;
use rustc_middle::mir::interpret::{get_slice_bytes, sign_extend, ConstValue};
use rustc_middle::mir::interpret::{get_slice_bytes, ConstValue};
use rustc_middle::mir::interpret::{ErrorHandled, LitToConstError, LitToConstInput};
use rustc_middle::mir::UserTypeProjection;
use rustc_middle::mir::{BorrowKind, Field, Mutability};
@ -1082,8 +1082,8 @@ crate fn compare_const_vals<'tcx>(
use rustc_attr::SignedInt;
use rustc_middle::ty::layout::IntegerExt;
let size = rustc_target::abi::Integer::from_attr(&tcx, SignedInt(ity)).size();
let a = sign_extend(a, size);
let b = sign_extend(b, size);
let a = size.sign_extend(a);
let b = size.sign_extend(b);
Some((a as i128).cmp(&(b as i128)))
}
_ => Some(a.cmp(&b)),