- LitKind::Int(n, _) => {
- enum Int {
- Signed(IntTy),
- Unsigned(UintTy),
- }
- let ity = match ty.sty {
- ty::Int(IntTy::Isize) => Int::Signed(tcx.sess.target.isize_ty),
- ty::Int(other) => Int::Signed(other),
- ty::Uint(UintTy::Usize) => Int::Unsigned(tcx.sess.target.usize_ty),
- ty::Uint(other) => Int::Unsigned(other),
- ty::Error => { // Avoid ICE (#51963)
- return Err(LitToConstError::Propagated);
- }
- _ => bug!("literal integer type with bad type ({:?})", ty.sty),
- };
- // This converts from LitKind::Int (which is sign extended) to
- // Scalar::Bytes (which is zero extended)
- let n = match ity {
- // FIXME(oli-obk): are these casts correct?
- Int::Signed(IntTy::I8) if neg =>
- (n as i8).overflowing_neg().0 as u8 as u128,
- Int::Signed(IntTy::I16) if neg =>
- (n as i16).overflowing_neg().0 as u16 as u128,
- Int::Signed(IntTy::I32) if neg =>
- (n as i32).overflowing_neg().0 as u32 as u128,
- Int::Signed(IntTy::I64) if neg =>
- (n as i64).overflowing_neg().0 as u64 as u128,
- Int::Signed(IntTy::I128) if neg =>
- (n as i128).overflowing_neg().0 as u128,
- Int::Signed(IntTy::I8) | Int::Unsigned(UintTy::U8) => n as u8 as u128,
- Int::Signed(IntTy::I16) | Int::Unsigned(UintTy::U16) => n as u16 as u128,
- Int::Signed(IntTy::I32) | Int::Unsigned(UintTy::U32) => n as u32 as u128,
- Int::Signed(IntTy::I64) | Int::Unsigned(UintTy::U64) => n as u64 as u128,
- Int::Signed(IntTy::I128)| Int::Unsigned(UintTy::U128) => n,
- _ => bug!(),
- };
- let size = tcx.layout_of(ty::ParamEnv::empty().and(ty)).unwrap().size.bytes() as u8;
- ConstValue::Scalar(Scalar::Bits {
- bits: n,
- size,
- })