mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 11:48:30 +00:00
Simplify lit_to_mir_constant a bit
This commit is contained in:
parent
c58c06a6f5
commit
8dc0c0ece9
@ -105,23 +105,19 @@ fn lit_to_mir_constant<'tcx>(tcx: TyCtxt<'tcx>, lit_input: LitToConstInput<'tcx>
|
|||||||
return Const::Ty(Ty::new_error(tcx, guar), ty::Const::new_error(tcx, guar));
|
return Const::Ty(Ty::new_error(tcx, guar), ty::Const::new_error(tcx, guar));
|
||||||
}
|
}
|
||||||
|
|
||||||
let trunc = |n, width: ty::UintTy| {
|
let lit_ty = match *ty.kind() {
|
||||||
let width = width
|
ty::Pat(base, _) => base,
|
||||||
.normalize(tcx.data_layout.pointer_size.bits().try_into().unwrap())
|
_ => ty,
|
||||||
.bit_width()
|
};
|
||||||
.unwrap();
|
|
||||||
let width = Size::from_bits(width);
|
let trunc = |n| {
|
||||||
|
let width = lit_ty.primitive_size(tcx);
|
||||||
trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
|
trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
|
||||||
let result = width.truncate(n);
|
let result = width.truncate(n);
|
||||||
trace!("trunc result: {}", result);
|
trace!("trunc result: {}", result);
|
||||||
ConstValue::Scalar(Scalar::from_uint(result, width))
|
ConstValue::Scalar(Scalar::from_uint(result, width))
|
||||||
};
|
};
|
||||||
|
|
||||||
let lit_ty = match *ty.kind() {
|
|
||||||
ty::Pat(base, _) => base,
|
|
||||||
_ => ty,
|
|
||||||
};
|
|
||||||
|
|
||||||
let value = match (lit, lit_ty.kind()) {
|
let value = match (lit, lit_ty.kind()) {
|
||||||
(ast::LitKind::Str(s, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => {
|
(ast::LitKind::Str(s, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => {
|
||||||
let s = s.as_str();
|
let s = s.as_str();
|
||||||
@ -149,11 +145,10 @@ fn lit_to_mir_constant<'tcx>(tcx: TyCtxt<'tcx>, lit_input: LitToConstInput<'tcx>
|
|||||||
(ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
|
(ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
|
||||||
ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1)))
|
ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1)))
|
||||||
}
|
}
|
||||||
(ast::LitKind::Int(n, _), ty::Uint(ui)) if !neg => trunc(n.get(), *ui),
|
(ast::LitKind::Int(n, _), ty::Uint(_)) if !neg => trunc(n.get()),
|
||||||
(ast::LitKind::Int(n, _), ty::Int(i)) => trunc(
|
(ast::LitKind::Int(n, _), ty::Int(_)) => {
|
||||||
if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() },
|
trunc(if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() })
|
||||||
i.to_unsigned(),
|
}
|
||||||
),
|
|
||||||
(ast::LitKind::Float(n, _), ty::Float(fty)) => {
|
(ast::LitKind::Float(n, _), ty::Float(fty)) => {
|
||||||
parse_float_into_constval(*n, *fty, neg).unwrap()
|
parse_float_into_constval(*n, *fty, neg).unwrap()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user