From 66dce1114d1014258c8cc586e176f2ea53ab4ed1 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sun, 24 Nov 2019 16:25:20 -0500 Subject: [PATCH] Store ptr_width as u32 on Config This removes the dependency on IntTy, UintTy from Session. --- src/librustc/session/config.rs | 16 +++++++--------- src/librustc_codegen_llvm/builder.rs | 4 ++-- src/librustc_codegen_llvm/intrinsic.rs | 4 ++-- src/librustc_lint/types.rs | 13 ++----------- src/libsyntax/ast.rs | 24 ++++++++++++++++++++++++ 5 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 2b1dfcaf68cec..a02f01d0c9764 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -12,7 +12,7 @@ use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel use rustc_target::spec::{Target, TargetTriple}; use syntax; -use syntax::ast::{self, IntTy, UintTy}; +use syntax::ast; use syntax::source_map::{FileName, FilePathMapping}; use syntax::edition::{Edition, EDITION_NAME_LIST, DEFAULT_EDITION}; use syntax::symbol::{sym, Symbol}; @@ -36,8 +36,7 @@ use std::path::{Path, PathBuf}; pub struct Config { pub target: Target, - pub isize_ty: IntTy, - pub usize_ty: UintTy, + pub ptr_width: u32, } #[derive(Clone, Hash, Debug)] @@ -1570,10 +1569,10 @@ pub fn build_target_config(opts: &Options, sp: &Handler) -> Config { FatalError.raise(); }); - let (isize_ty, usize_ty) = match &target.target_pointer_width[..] { - "16" => (ast::IntTy::I16, ast::UintTy::U16), - "32" => (ast::IntTy::I32, ast::UintTy::U32), - "64" => (ast::IntTy::I64, ast::UintTy::U64), + let ptr_width = match &target.target_pointer_width[..] { + "16" => 16, + "32" => 32, + "64" => 64, w => sp.fatal(&format!( "target specification was invalid: \ unrecognized target-pointer-width {}", @@ -1583,8 +1582,7 @@ pub fn build_target_config(opts: &Options, sp: &Handler) -> Config { Config { target, - isize_ty, - usize_ty, + ptr_width, } } diff --git a/src/librustc_codegen_llvm/builder.rs b/src/librustc_codegen_llvm/builder.rs index 312c41b88b092..6f72466c559dc 100644 --- a/src/librustc_codegen_llvm/builder.rs +++ b/src/librustc_codegen_llvm/builder.rs @@ -325,8 +325,8 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { use rustc::ty::{Int, Uint}; let new_kind = match ty.kind { - Int(Isize) => Int(self.tcx.sess.target.isize_ty), - Uint(Usize) => Uint(self.tcx.sess.target.usize_ty), + Int(t @ Isize) => Int(t.normalize(self.tcx.sess.target.ptr_width)), + Uint(t @ Usize) => Uint(t.normalize(self.tcx.sess.target.ptr_width)), ref t @ Uint(_) | ref t @ Int(_) => t.clone(), _ => panic!("tried to get overflow intrinsic for op applied to non-int type") }; diff --git a/src/librustc_codegen_llvm/intrinsic.rs b/src/librustc_codegen_llvm/intrinsic.rs index 12ec4e1074874..aa55f3a19e2be 100644 --- a/src/librustc_codegen_llvm/intrinsic.rs +++ b/src/librustc_codegen_llvm/intrinsic.rs @@ -1926,7 +1926,7 @@ unsupported {} from `{}` with element `{}` of size `{}` to `{}`"#, fn int_type_width_signed(ty: Ty<'_>, cx: &CodegenCx<'_, '_>) -> Option<(u64, bool)> { match ty.kind { ty::Int(t) => Some((match t { - ast::IntTy::Isize => cx.tcx.sess.target.isize_ty.bit_width().unwrap() as u64, + ast::IntTy::Isize => cx.tcx.sess.target.ptr_width as u64, ast::IntTy::I8 => 8, ast::IntTy::I16 => 16, ast::IntTy::I32 => 32, @@ -1934,7 +1934,7 @@ fn int_type_width_signed(ty: Ty<'_>, cx: &CodegenCx<'_, '_>) -> Option<(u64, boo ast::IntTy::I128 => 128, }, true)), ty::Uint(t) => Some((match t { - ast::UintTy::Usize => cx.tcx.sess.target.usize_ty.bit_width().unwrap() as u64, + ast::UintTy::Usize => cx.tcx.sess.target.ptr_width as u64, ast::UintTy::U8 => 8, ast::UintTy::U16 => 16, ast::UintTy::U32 => 32, diff --git a/src/librustc_lint/types.rs b/src/librustc_lint/types.rs index 9a4e981081fcf..f2e56c69fd79f 100644 --- a/src/librustc_lint/types.rs +++ b/src/librustc_lint/types.rs @@ -252,12 +252,7 @@ fn lint_int_literal<'a, 'tcx>( t: ast::IntTy, v: u128, ) { - let int_type = if let ast::IntTy::Isize = t { - cx.sess().target.isize_ty - } else { - t - }; - + let int_type = t.normalize(cx.sess().target.ptr_width); let (_, max) = int_ty_range(int_type); let max = max as u128; let negative = type_limits.negated_expr_id == e.hir_id; @@ -303,11 +298,7 @@ fn lint_uint_literal<'a, 'tcx>( lit: &hir::Lit, t: ast::UintTy, ) { - let uint_type = if let ast::UintTy::Usize = t { - cx.sess().target.usize_ty - } else { - t - }; + let uint_type = t.normalize(cx.sess().target.ptr_width); let (min, max) = uint_ty_range(uint_type); let lit_val: u128 = match lit.node { // _v is u8, within range by definition diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 142430769411f..b8561216896e2 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1693,6 +1693,18 @@ impl IntTy { IntTy::I128 => 128, }) } + + pub fn normalize(&self, target_width: u32) -> Self { + match self { + IntTy::Isize => match target_width { + 16 => IntTy::I16, + 32 => IntTy::I32, + 64 => IntTy::I64, + _ => unreachable!(), + }, + _ => *self, + } + } } #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable_Generic, @@ -1743,6 +1755,18 @@ impl UintTy { UintTy::U128 => 128, }) } + + pub fn normalize(&self, target_width: u32) -> Self { + match self { + UintTy::Usize => match target_width { + 16 => UintTy::U16, + 32 => UintTy::U32, + 64 => UintTy::U64, + _ => unreachable!(), + }, + _ => *self, + } + } } /// A constraint on an associated type (e.g., `A = Bar` in `Foo` or