From 2c94ad08d8cbd065a3673cee5d894963c33236c0 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 24 May 2020 19:20:44 +0200 Subject: [PATCH] use helper method to compute size of int type --- src/lib.rs | 1 + src/shims/intrinsics.rs | 16 +++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e8cf507d35a..0ea0d57cacc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,7 @@ #![allow(incomplete_features)] #![feature(const_generics)] +extern crate rustc_attr; extern crate rustc_apfloat; extern crate rustc_ast; #[macro_use] extern crate rustc_middle; diff --git a/src/shims/intrinsics.rs b/src/shims/intrinsics.rs index 08087b0350d..1a8c9163211 100644 --- a/src/shims/intrinsics.rs +++ b/src/shims/intrinsics.rs @@ -1,10 +1,12 @@ use std::iter; use std::convert::TryFrom; +use rustc_attr as attr; use rustc_ast::ast::FloatTy; use rustc_middle::{mir, ty}; +use rustc_middle::ty::layout::IntegerExt; use rustc_apfloat::{Float, Round}; -use rustc_target::abi::{Align, LayoutOf, Size}; +use rustc_target::abi::{Align, Integer, LayoutOf}; use crate::*; use helpers::check_arg_count; @@ -563,11 +565,11 @@ fn float_to_int_unchecked( Ok(match dest_ty.kind { // Unsigned ty::Uint(t) => { - let width = t.bit_width().unwrap_or_else(|| this.pointer_size().bits()); - let res = f.to_u128(usize::try_from(width).unwrap()); + let size = Integer::from_attr(this, attr::IntType::UnsignedInt(t)).size(); + let res = f.to_u128(size.bits_usize()); if res.status.is_empty() { // 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 { // `f` was not representable in this integer type. throw_ub_format!( @@ -578,11 +580,11 @@ fn float_to_int_unchecked( } // Signed ty::Int(t) => { - let width = t.bit_width().unwrap_or_else(|| this.pointer_size().bits()); - let res = f.to_i128(usize::try_from(width).unwrap()); + let size = Integer::from_attr(this, attr::IntType::SignedInt(t)).size(); + let res = f.to_i128(size.bits_usize()); if res.status.is_empty() { // 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 { // `f` was not representable in this integer type. throw_ub_format!( -- 2.44.0