Skip to content

Commit

Permalink
use new try_from methods
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Dec 24, 2019
1 parent b1e97df commit 2db6a3c
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,35 +501,22 @@ fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
Ok(&OsStr::new(s))
}

// FIXME: change `ImmTy::from_int` so it returns an `InterpResult` instead and remove this
// function.
pub fn immty_from_int_checked<'tcx>(
int: impl Into<i128>,
layout: TyLayout<'tcx>,
) -> InterpResult<'tcx, ImmTy<'tcx, Tag>> {
let int = int.into();
// If `int` does not fit in `size` bits, we error instead of letting
// `ImmTy::from_int` panic.
let size = layout.size;
let truncated = truncate(int as u128, size);
if sign_extend(truncated, size) as i128 != int {
throw_unsup_format!("Signed value {:#x} does not fit in {} bits", int, size.bits())
}
Ok(ImmTy::from_int(int, layout))
Ok(ImmTy::try_from_int(int, layout).ok_or_else(||
err_unsup_format!("Signed value {:#x} does not fit in {} bits", int, layout.size.bits())
)?)
}

// FIXME: change `ImmTy::from_uint` so it returns an `InterpResult` instead and remove this
// function.
pub fn immty_from_uint_checked<'tcx>(
int: impl Into<u128>,
layout: TyLayout<'tcx>,
) -> InterpResult<'tcx, ImmTy<'tcx, Tag>> {
let int = int.into();
// If `int` does not fit in `size` bits, we error instead of letting
// `ImmTy::from_int` panic.
let size = layout.size;
if truncate(int, size) != int {
throw_unsup_format!("Unsigned value {:#x} does not fit in {} bits", int, size.bits())
}
Ok(ImmTy::from_uint(int, layout))
Ok(ImmTy::try_from_uint(int, layout).ok_or_else(||
err_unsup_format!("Signed value {:#x} does not fit in {} bits", int, layout.size.bits())
)?)
}

0 comments on commit 2db6a3c

Please sign in to comment.