use helper method to compute size of int type
This commit is contained in:
parent
a6c28f0845
commit
2c94ad08d8
@ -10,6 +10,7 @@
|
|||||||
#![allow(incomplete_features)]
|
#![allow(incomplete_features)]
|
||||||
#![feature(const_generics)]
|
#![feature(const_generics)]
|
||||||
|
|
||||||
|
extern crate rustc_attr;
|
||||||
extern crate rustc_apfloat;
|
extern crate rustc_apfloat;
|
||||||
extern crate rustc_ast;
|
extern crate rustc_ast;
|
||||||
#[macro_use] extern crate rustc_middle;
|
#[macro_use] extern crate rustc_middle;
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
use std::iter;
|
use std::iter;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
|
use rustc_attr as attr;
|
||||||
use rustc_ast::ast::FloatTy;
|
use rustc_ast::ast::FloatTy;
|
||||||
use rustc_middle::{mir, ty};
|
use rustc_middle::{mir, ty};
|
||||||
|
use rustc_middle::ty::layout::IntegerExt;
|
||||||
use rustc_apfloat::{Float, Round};
|
use rustc_apfloat::{Float, Round};
|
||||||
use rustc_target::abi::{Align, LayoutOf, Size};
|
use rustc_target::abi::{Align, Integer, LayoutOf};
|
||||||
|
|
||||||
use crate::*;
|
use crate::*;
|
||||||
use helpers::check_arg_count;
|
use helpers::check_arg_count;
|
||||||
@ -563,11 +565,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
|||||||
Ok(match dest_ty.kind {
|
Ok(match dest_ty.kind {
|
||||||
// Unsigned
|
// Unsigned
|
||||||
ty::Uint(t) => {
|
ty::Uint(t) => {
|
||||||
let width = t.bit_width().unwrap_or_else(|| this.pointer_size().bits());
|
let size = Integer::from_attr(this, attr::IntType::UnsignedInt(t)).size();
|
||||||
let res = f.to_u128(usize::try_from(width).unwrap());
|
let res = f.to_u128(size.bits_usize());
|
||||||
if res.status.is_empty() {
|
if res.status.is_empty() {
|
||||||
// No status flags means there was no further rounding or other loss of precision.
|
// No status flags means there was no further rounding or other loss of precision.
|
||||||
Scalar::from_uint(res.value, Size::from_bits(width))
|
Scalar::from_uint(res.value, size)
|
||||||
} else {
|
} else {
|
||||||
// `f` was not representable in this integer type.
|
// `f` was not representable in this integer type.
|
||||||
throw_ub_format!(
|
throw_ub_format!(
|
||||||
@ -578,11 +580,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
|||||||
}
|
}
|
||||||
// Signed
|
// Signed
|
||||||
ty::Int(t) => {
|
ty::Int(t) => {
|
||||||
let width = t.bit_width().unwrap_or_else(|| this.pointer_size().bits());
|
let size = Integer::from_attr(this, attr::IntType::SignedInt(t)).size();
|
||||||
let res = f.to_i128(usize::try_from(width).unwrap());
|
let res = f.to_i128(size.bits_usize());
|
||||||
if res.status.is_empty() {
|
if res.status.is_empty() {
|
||||||
// No status flags means there was no further rounding or other loss of precision.
|
// No status flags means there was no further rounding or other loss of precision.
|
||||||
Scalar::from_int(res.value, Size::from_bits(width))
|
Scalar::from_int(res.value, size)
|
||||||
} else {
|
} else {
|
||||||
// `f` was not representable in this integer type.
|
// `f` was not representable in this integer type.
|
||||||
throw_ub_format!(
|
throw_ub_format!(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user