Skip to content

Commit ff33414

Browse files
committed
ScalarInt: support conversion with signed int types and cmp::Ordering
1 parent 7238669 commit ff33414

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ impl<'tcx, Prov: Provenance> ImmTy<'tcx, Prov> {
310310
let ty = tcx.ty_ordering_enum(None);
311311
let layout =
312312
tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty)).unwrap();
313-
Self::from_scalar(Scalar::from_i8(c as i8), layout)
313+
Self::from_scalar(Scalar::Int(c.into()), layout)
314314
}
315315

316316
pub fn from_pair(a: Self, b: Self, cx: &(impl HasTypingEnv<'tcx> + HasTyCtxt<'tcx>)) -> Self {

compiler/rustc_middle/src/mir/interpret/value.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,27 +180,27 @@ impl<Prov> Scalar<Prov> {
180180

181181
#[inline]
182182
pub fn from_i8(i: i8) -> Self {
183-
Self::from_int(i, Size::from_bits(8))
183+
Self::Int(i.into())
184184
}
185185

186186
#[inline]
187187
pub fn from_i16(i: i16) -> Self {
188-
Self::from_int(i, Size::from_bits(16))
188+
Self::Int(i.into())
189189
}
190190

191191
#[inline]
192192
pub fn from_i32(i: i32) -> Self {
193-
Self::from_int(i, Size::from_bits(32))
193+
Self::Int(i.into())
194194
}
195195

196196
#[inline]
197197
pub fn from_i64(i: i64) -> Self {
198-
Self::from_int(i, Size::from_bits(64))
198+
Self::Int(i.into())
199199
}
200200

201201
#[inline]
202202
pub fn from_i128(i: i128) -> Self {
203-
Self::from_int(i, Size::from_bits(128))
203+
Self::Int(i.into())
204204
}
205205

206206
#[inline]

compiler/rustc_middle/src/ty/consts/int.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,9 @@ macro_rules! from_scalar_int_for_x {
422422
impl From<ScalarInt> for $ty {
423423
#[inline]
424424
fn from(int: ScalarInt) -> Self {
425-
// The `unwrap` cannot fail because to_bits (if it succeeds)
425+
// The `unwrap` cannot fail because to_uint (if it succeeds)
426426
// is guaranteed to return a value that fits into the size.
427-
int.to_bits(Size::from_bytes(size_of::<$ty>()))
427+
int.to_uint(Size::from_bytes(size_of::<$ty>()))
428428
.try_into().unwrap()
429429
}
430430
}
@@ -450,6 +450,49 @@ impl From<char> for ScalarInt {
450450
}
451451
}
452452

453+
macro_rules! from_x_for_scalar_int_signed {
454+
($($ty:ty),*) => {
455+
$(
456+
impl From<$ty> for ScalarInt {
457+
#[inline]
458+
fn from(u: $ty) -> Self {
459+
Self {
460+
data: u128::from(u.cast_unsigned()), // go via the unsigned type of the same size
461+
size: NonZero::new(size_of::<$ty>() as u8).unwrap(),
462+
}
463+
}
464+
}
465+
)*
466+
}
467+
}
468+
469+
macro_rules! from_scalar_int_for_x_signed {
470+
($($ty:ty),*) => {
471+
$(
472+
impl From<ScalarInt> for $ty {
473+
#[inline]
474+
fn from(int: ScalarInt) -> Self {
475+
// The `unwrap` cannot fail because to_int (if it succeeds)
476+
// is guaranteed to return a value that fits into the size.
477+
int.to_int(Size::from_bytes(size_of::<$ty>()))
478+
.try_into().unwrap()
479+
}
480+
}
481+
)*
482+
}
483+
}
484+
485+
from_x_for_scalar_int_signed!(i8, i16, i32, i64, i128);
486+
from_scalar_int_for_x_signed!(i8, i16, i32, i64, i128);
487+
488+
impl From<std::cmp::Ordering> for ScalarInt {
489+
#[inline]
490+
fn from(c: std::cmp::Ordering) -> Self {
491+
// Here we rely on `Ordering` having the same values in host and target!
492+
ScalarInt::from(c as i8)
493+
}
494+
}
495+
453496
/// Error returned when a conversion from ScalarInt to char fails.
454497
#[derive(Debug)]
455498
pub struct CharTryFromScalarInt;

0 commit comments

Comments
 (0)