]> git.lizzy.rs Git - rust.git/commitdiff
use helper method to compute size of int type
authorRalf Jung <post@ralfj.de>
Sun, 24 May 2020 17:20:44 +0000 (19:20 +0200)
committerRalf Jung <post@ralfj.de>
Sun, 24 May 2020 17:20:44 +0000 (19:20 +0200)
src/lib.rs
src/shims/intrinsics.rs

index e8cf507d35a0acb0edf30a39dff0c344e238f9b7..0ea0d57caccc0c169173b730439dc6060077b945 100644 (file)
@@ -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;
index 08087b0350d9c02d9be2745dfd2b80eb0753b482..1a8c9163211a8eb991437971ee3ef836bb12bc62 100644 (file)
@@ -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<F>(
         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<F>(
             }
             // 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!(