Skip to content

Commit 6208743

Browse files
committed
add Scalar::from methods for signed integers
1 parent 5ed3453 commit 6208743

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/librustc/mir/interpret/value.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,13 @@ impl<'tcx, Tag> Scalar<Tag> {
272272

273273
#[inline]
274274
pub fn from_bool(b: bool) -> Self {
275+
// Guaranteed to be truncated and does not need sign extension.
275276
Scalar::Raw { data: b as u128, size: 1 }
276277
}
277278

278279
#[inline]
279280
pub fn from_char(c: char) -> Self {
281+
// Guaranteed to be truncated and does not need sign extension.
280282
Scalar::Raw { data: c as u128, size: 4 }
281283
}
282284

@@ -299,21 +301,25 @@ impl<'tcx, Tag> Scalar<Tag> {
299301

300302
#[inline]
301303
pub fn from_u8(i: u8) -> Self {
304+
// Guaranteed to be truncated and does not need sign extension.
302305
Scalar::Raw { data: i as u128, size: 1 }
303306
}
304307

305308
#[inline]
306309
pub fn from_u16(i: u16) -> Self {
310+
// Guaranteed to be truncated and does not need sign extension.
307311
Scalar::Raw { data: i as u128, size: 2 }
308312
}
309313

310314
#[inline]
311315
pub fn from_u32(i: u32) -> Self {
316+
// Guaranteed to be truncated and does not need sign extension.
312317
Scalar::Raw { data: i as u128, size: 4 }
313318
}
314319

315320
#[inline]
316321
pub fn from_u64(i: u64) -> Self {
322+
// Guaranteed to be truncated and does not need sign extension.
317323
Scalar::Raw { data: i as u128, size: 8 }
318324
}
319325

@@ -341,6 +347,26 @@ impl<'tcx, Tag> Scalar<Tag> {
341347
.unwrap_or_else(|| bug!("Signed value {:#x} does not fit in {} bits", i, size.bits()))
342348
}
343349

350+
#[inline]
351+
pub fn from_i8(i: i8) -> Self {
352+
Self::from_int(i, Size::from_bits(8))
353+
}
354+
355+
#[inline]
356+
pub fn from_i16(i: i16) -> Self {
357+
Self::from_int(i, Size::from_bits(16))
358+
}
359+
360+
#[inline]
361+
pub fn from_i32(i: i32) -> Self {
362+
Self::from_int(i, Size::from_bits(32))
363+
}
364+
365+
#[inline]
366+
pub fn from_i64(i: i64) -> Self {
367+
Self::from_int(i, Size::from_bits(64))
368+
}
369+
344370
#[inline]
345371
pub fn from_machine_isize(i: i64, cx: &impl HasDataLayout) -> Self {
346372
Self::from_int(i, cx.data_layout().pointer_size)

0 commit comments

Comments
 (0)