Skip to content

Commit

Permalink
Rollup merge of rust-lang#66719 - Mark-Simulacrum:int-normalization, …
Browse files Browse the repository at this point in the history
…r=Centril

Store pointer width as u32 on Config

This removes the dependency on IntTy, UintTy from Session.

It's not obviously a win, but it seems a bit odd to store the AST IntTy/UintTy in Session, rather we store the pointer width as an integer and add normalization methods to IntTy and UintTy.
  • Loading branch information
tmandry authored Nov 26, 2019
2 parents ac7e604 + 66dce11 commit f178d35
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 24 deletions.
16 changes: 7 additions & 9 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
Expand Down Expand Up @@ -1621,10 +1620,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 {}",
Expand All @@ -1634,8 +1633,7 @@ pub fn build_target_config(opts: &Options, sp: &Handler) -> Config {

Config {
target,
isize_ty,
usize_ty,
ptr_width,
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_llvm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
};
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_llvm/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1926,15 +1926,15 @@ 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,
ast::IntTy::I64 => 64,
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,
Expand Down
13 changes: 2 additions & 11 deletions src/librustc_lint/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,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,
Expand Down Expand Up @@ -1768,6 +1780,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<A = Bar>` or
Expand Down

0 comments on commit f178d35

Please sign in to comment.