diff --git a/src/librustc/mir/interpret/value.rs b/src/librustc/mir/interpret/value.rs index 9dc0b24cd2f3..1e630c96dd40 100644 --- a/src/librustc/mir/interpret/value.rs +++ b/src/librustc/mir/interpret/value.rs @@ -272,11 +272,13 @@ impl<'tcx, Tag> Scalar { #[inline] pub fn from_bool(b: bool) -> Self { + // Guaranteed to be truncated and does not need sign extension. Scalar::Raw { data: b as u128, size: 1 } } #[inline] pub fn from_char(c: char) -> Self { + // Guaranteed to be truncated and does not need sign extension. Scalar::Raw { data: c as u128, size: 4 } } @@ -299,21 +301,25 @@ impl<'tcx, Tag> Scalar { #[inline] pub fn from_u8(i: u8) -> Self { + // Guaranteed to be truncated and does not need sign extension. Scalar::Raw { data: i as u128, size: 1 } } #[inline] pub fn from_u16(i: u16) -> Self { + // Guaranteed to be truncated and does not need sign extension. Scalar::Raw { data: i as u128, size: 2 } } #[inline] pub fn from_u32(i: u32) -> Self { + // Guaranteed to be truncated and does not need sign extension. Scalar::Raw { data: i as u128, size: 4 } } #[inline] pub fn from_u64(i: u64) -> Self { + // Guaranteed to be truncated and does not need sign extension. Scalar::Raw { data: i as u128, size: 8 } } @@ -341,6 +347,26 @@ impl<'tcx, Tag> Scalar { .unwrap_or_else(|| bug!("Signed value {:#x} does not fit in {} bits", i, size.bits())) } + #[inline] + pub fn from_i8(i: i8) -> Self { + Self::from_int(i, Size::from_bits(8)) + } + + #[inline] + pub fn from_i16(i: i16) -> Self { + Self::from_int(i, Size::from_bits(16)) + } + + #[inline] + pub fn from_i32(i: i32) -> Self { + Self::from_int(i, Size::from_bits(32)) + } + + #[inline] + pub fn from_i64(i: i64) -> Self { + Self::from_int(i, Size::from_bits(64)) + } + #[inline] pub fn from_machine_isize(i: i64, cx: &impl HasDataLayout) -> Self { Self::from_int(i, cx.data_layout().pointer_size)