Skip to content

Commit f178d35

Browse files
authored
Rollup merge of rust-lang#66719 - Mark-Simulacrum:int-normalization, 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.
2 parents ac7e604 + 66dce11 commit f178d35

File tree

5 files changed

+37
-24
lines changed

5 files changed

+37
-24
lines changed

src/librustc/session/config.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel
1212
use rustc_target::spec::{Target, TargetTriple};
1313

1414
use syntax;
15-
use syntax::ast::{self, IntTy, UintTy};
15+
use syntax::ast;
1616
use syntax::source_map::{FileName, FilePathMapping};
1717
use syntax::edition::{Edition, EDITION_NAME_LIST, DEFAULT_EDITION};
1818
use syntax::symbol::{sym, Symbol};
@@ -36,8 +36,7 @@ use std::path::{Path, PathBuf};
3636

3737
pub struct Config {
3838
pub target: Target,
39-
pub isize_ty: IntTy,
40-
pub usize_ty: UintTy,
39+
pub ptr_width: u32,
4140
}
4241

4342
#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
@@ -1621,10 +1620,10 @@ pub fn build_target_config(opts: &Options, sp: &Handler) -> Config {
16211620
FatalError.raise();
16221621
});
16231622

1624-
let (isize_ty, usize_ty) = match &target.target_pointer_width[..] {
1625-
"16" => (ast::IntTy::I16, ast::UintTy::U16),
1626-
"32" => (ast::IntTy::I32, ast::UintTy::U32),
1627-
"64" => (ast::IntTy::I64, ast::UintTy::U64),
1623+
let ptr_width = match &target.target_pointer_width[..] {
1624+
"16" => 16,
1625+
"32" => 32,
1626+
"64" => 64,
16281627
w => sp.fatal(&format!(
16291628
"target specification was invalid: \
16301629
unrecognized target-pointer-width {}",
@@ -1634,8 +1633,7 @@ pub fn build_target_config(opts: &Options, sp: &Handler) -> Config {
16341633

16351634
Config {
16361635
target,
1637-
isize_ty,
1638-
usize_ty,
1636+
ptr_width,
16391637
}
16401638
}
16411639

src/librustc_codegen_llvm/builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
325325
use rustc::ty::{Int, Uint};
326326

327327
let new_kind = match ty.kind {
328-
Int(Isize) => Int(self.tcx.sess.target.isize_ty),
329-
Uint(Usize) => Uint(self.tcx.sess.target.usize_ty),
328+
Int(t @ Isize) => Int(t.normalize(self.tcx.sess.target.ptr_width)),
329+
Uint(t @ Usize) => Uint(t.normalize(self.tcx.sess.target.ptr_width)),
330330
ref t @ Uint(_) | ref t @ Int(_) => t.clone(),
331331
_ => panic!("tried to get overflow intrinsic for op applied to non-int type")
332332
};

src/librustc_codegen_llvm/intrinsic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1926,15 +1926,15 @@ unsupported {} from `{}` with element `{}` of size `{}` to `{}`"#,
19261926
fn int_type_width_signed(ty: Ty<'_>, cx: &CodegenCx<'_, '_>) -> Option<(u64, bool)> {
19271927
match ty.kind {
19281928
ty::Int(t) => Some((match t {
1929-
ast::IntTy::Isize => cx.tcx.sess.target.isize_ty.bit_width().unwrap() as u64,
1929+
ast::IntTy::Isize => cx.tcx.sess.target.ptr_width as u64,
19301930
ast::IntTy::I8 => 8,
19311931
ast::IntTy::I16 => 16,
19321932
ast::IntTy::I32 => 32,
19331933
ast::IntTy::I64 => 64,
19341934
ast::IntTy::I128 => 128,
19351935
}, true)),
19361936
ty::Uint(t) => Some((match t {
1937-
ast::UintTy::Usize => cx.tcx.sess.target.usize_ty.bit_width().unwrap() as u64,
1937+
ast::UintTy::Usize => cx.tcx.sess.target.ptr_width as u64,
19381938
ast::UintTy::U8 => 8,
19391939
ast::UintTy::U16 => 16,
19401940
ast::UintTy::U32 => 32,

src/librustc_lint/types.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,7 @@ fn lint_int_literal<'a, 'tcx>(
252252
t: ast::IntTy,
253253
v: u128,
254254
) {
255-
let int_type = if let ast::IntTy::Isize = t {
256-
cx.sess().target.isize_ty
257-
} else {
258-
t
259-
};
260-
255+
let int_type = t.normalize(cx.sess().target.ptr_width);
261256
let (_, max) = int_ty_range(int_type);
262257
let max = max as u128;
263258
let negative = type_limits.negated_expr_id == e.hir_id;
@@ -303,11 +298,7 @@ fn lint_uint_literal<'a, 'tcx>(
303298
lit: &hir::Lit,
304299
t: ast::UintTy,
305300
) {
306-
let uint_type = if let ast::UintTy::Usize = t {
307-
cx.sess().target.usize_ty
308-
} else {
309-
t
310-
};
301+
let uint_type = t.normalize(cx.sess().target.ptr_width);
311302
let (min, max) = uint_ty_range(uint_type);
312303
let lit_val: u128 = match lit.node {
313304
// _v is u8, within range by definition

src/libsyntax/ast.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,18 @@ impl IntTy {
17181718
IntTy::I128 => 128,
17191719
})
17201720
}
1721+
1722+
pub fn normalize(&self, target_width: u32) -> Self {
1723+
match self {
1724+
IntTy::Isize => match target_width {
1725+
16 => IntTy::I16,
1726+
32 => IntTy::I32,
1727+
64 => IntTy::I64,
1728+
_ => unreachable!(),
1729+
},
1730+
_ => *self,
1731+
}
1732+
}
17211733
}
17221734

17231735
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable_Generic,
@@ -1768,6 +1780,18 @@ impl UintTy {
17681780
UintTy::U128 => 128,
17691781
})
17701782
}
1783+
1784+
pub fn normalize(&self, target_width: u32) -> Self {
1785+
match self {
1786+
UintTy::Usize => match target_width {
1787+
16 => UintTy::U16,
1788+
32 => UintTy::U32,
1789+
64 => UintTy::U64,
1790+
_ => unreachable!(),
1791+
},
1792+
_ => *self,
1793+
}
1794+
}
17711795
}
17721796

17731797
/// A constraint on an associated type (e.g., `A = Bar` in `Foo<A = Bar>` or

0 commit comments

Comments
 (0)