Skip to content

Commit 933bb18

Browse files
Use rustc_type_ir::{IntTy,UintTy,FloatTy} instead of the rustc_ast` ones in types
1 parent 0724573 commit 933bb18

File tree

9 files changed

+143
-74
lines changed

9 files changed

+143
-74
lines changed

compiler/rustc_middle/src/ty/cast.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33

44
use crate::ty::{self, Ty};
55

6-
use rustc_ast as ast;
76
use rustc_macros::HashStable;
87

98
/// Types that are represented as ints.
109
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1110
pub enum IntTy {
12-
U(ast::UintTy),
11+
U(ty::UintTy),
1312
I,
1413
CEnum,
1514
Bool,

compiler/rustc_middle/src/ty/context.rs

+35-35
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ use crate::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, Substs
1919
use crate::ty::TyKind::*;
2020
use crate::ty::{
2121
self, AdtDef, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig, Const, ConstVid,
22-
DefIdTree, ExistentialPredicate, FloatVar, FloatVid, GenericParamDefKind, InferConst, InferTy,
23-
IntVar, IntVid, List, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind,
24-
ProjectionTy, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar,
25-
TyVid, TypeAndMut, Visibility,
22+
DefIdTree, ExistentialPredicate, FloatTy, FloatVar, FloatVid, GenericParamDefKind, InferConst,
23+
InferTy, IntTy, IntVar, IntVid, List, ParamConst, ParamTy, PolyFnSig, Predicate,
24+
PredicateInner, PredicateKind, ProjectionTy, Region, RegionKind, ReprOptions,
25+
TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy, Visibility,
2626
};
2727
use rustc_ast as ast;
2828
use rustc_ast::expand::allocator::AllocatorKind;
@@ -839,20 +839,20 @@ impl<'tcx> CommonTypes<'tcx> {
839839
bool: mk(Bool),
840840
char: mk(Char),
841841
never: mk(Never),
842-
isize: mk(Int(ast::IntTy::Isize)),
843-
i8: mk(Int(ast::IntTy::I8)),
844-
i16: mk(Int(ast::IntTy::I16)),
845-
i32: mk(Int(ast::IntTy::I32)),
846-
i64: mk(Int(ast::IntTy::I64)),
847-
i128: mk(Int(ast::IntTy::I128)),
848-
usize: mk(Uint(ast::UintTy::Usize)),
849-
u8: mk(Uint(ast::UintTy::U8)),
850-
u16: mk(Uint(ast::UintTy::U16)),
851-
u32: mk(Uint(ast::UintTy::U32)),
852-
u64: mk(Uint(ast::UintTy::U64)),
853-
u128: mk(Uint(ast::UintTy::U128)),
854-
f32: mk(Float(ast::FloatTy::F32)),
855-
f64: mk(Float(ast::FloatTy::F64)),
842+
isize: mk(Int(ty::IntTy::Isize)),
843+
i8: mk(Int(ty::IntTy::I8)),
844+
i16: mk(Int(ty::IntTy::I16)),
845+
i32: mk(Int(ty::IntTy::I32)),
846+
i64: mk(Int(ty::IntTy::I64)),
847+
i128: mk(Int(ty::IntTy::I128)),
848+
usize: mk(Uint(ty::UintTy::Usize)),
849+
u8: mk(Uint(ty::UintTy::U8)),
850+
u16: mk(Uint(ty::UintTy::U16)),
851+
u32: mk(Uint(ty::UintTy::U32)),
852+
u64: mk(Uint(ty::UintTy::U64)),
853+
u128: mk(Uint(ty::UintTy::U128)),
854+
f32: mk(Float(ty::FloatTy::F32)),
855+
f64: mk(Float(ty::FloatTy::F64)),
856856
str_: mk(Str),
857857
self_param: mk(ty::Param(ty::ParamTy { index: 0, name: kw::SelfUpper })),
858858

@@ -2102,32 +2102,32 @@ impl<'tcx> TyCtxt<'tcx> {
21022102
if pred.kind() != binder { self.mk_predicate(binder) } else { pred }
21032103
}
21042104

2105-
pub fn mk_mach_int(self, tm: ast::IntTy) -> Ty<'tcx> {
2105+
pub fn mk_mach_int(self, tm: IntTy) -> Ty<'tcx> {
21062106
match tm {
2107-
ast::IntTy::Isize => self.types.isize,
2108-
ast::IntTy::I8 => self.types.i8,
2109-
ast::IntTy::I16 => self.types.i16,
2110-
ast::IntTy::I32 => self.types.i32,
2111-
ast::IntTy::I64 => self.types.i64,
2112-
ast::IntTy::I128 => self.types.i128,
2107+
IntTy::Isize => self.types.isize,
2108+
IntTy::I8 => self.types.i8,
2109+
IntTy::I16 => self.types.i16,
2110+
IntTy::I32 => self.types.i32,
2111+
IntTy::I64 => self.types.i64,
2112+
IntTy::I128 => self.types.i128,
21132113
}
21142114
}
21152115

2116-
pub fn mk_mach_uint(self, tm: ast::UintTy) -> Ty<'tcx> {
2116+
pub fn mk_mach_uint(self, tm: UintTy) -> Ty<'tcx> {
21172117
match tm {
2118-
ast::UintTy::Usize => self.types.usize,
2119-
ast::UintTy::U8 => self.types.u8,
2120-
ast::UintTy::U16 => self.types.u16,
2121-
ast::UintTy::U32 => self.types.u32,
2122-
ast::UintTy::U64 => self.types.u64,
2123-
ast::UintTy::U128 => self.types.u128,
2118+
UintTy::Usize => self.types.usize,
2119+
UintTy::U8 => self.types.u8,
2120+
UintTy::U16 => self.types.u16,
2121+
UintTy::U32 => self.types.u32,
2122+
UintTy::U64 => self.types.u64,
2123+
UintTy::U128 => self.types.u128,
21242124
}
21252125
}
21262126

2127-
pub fn mk_mach_float(self, tm: ast::FloatTy) -> Ty<'tcx> {
2127+
pub fn mk_mach_float(self, tm: FloatTy) -> Ty<'tcx> {
21282128
match tm {
2129-
ast::FloatTy::F32 => self.types.f32,
2130-
ast::FloatTy::F64 => self.types.f64,
2129+
FloatTy::F32 => self.types.f32,
2130+
FloatTy::F64 => self.types.f64,
21312131
}
21322132
}
21332133

compiler/rustc_middle/src/ty/error.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::traits::{ObligationCause, ObligationCauseCode};
22
use crate::ty::diagnostics::suggest_constraining_type_param;
33
use crate::ty::{self, BoundRegionKind, Region, Ty, TyCtxt};
4-
use rustc_ast as ast;
54
use rustc_errors::Applicability::{MachineApplicable, MaybeIncorrect};
65
use rustc_errors::{pluralize, DiagnosticBuilder};
76
use rustc_hir as hir;
@@ -48,7 +47,7 @@ pub enum TypeError<'tcx> {
4847

4948
Sorts(ExpectedFound<Ty<'tcx>>),
5049
IntMismatch(ExpectedFound<ty::IntVarValue>),
51-
FloatMismatch(ExpectedFound<ast::FloatTy>),
50+
FloatMismatch(ExpectedFound<ty::FloatTy>),
5251
Traits(ExpectedFound<DefId>),
5352
VariadicMismatch(ExpectedFound<bool>),
5453

compiler/rustc_middle/src/ty/fast_reject.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::ich::StableHashingContext;
22
use crate::ty::{self, Ty, TyCtxt};
3-
use rustc_ast as ast;
43
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
54
use rustc_hir::def_id::DefId;
65
use std::fmt::Debug;
@@ -24,9 +23,9 @@ where
2423
{
2524
BoolSimplifiedType,
2625
CharSimplifiedType,
27-
IntSimplifiedType(ast::IntTy),
28-
UintSimplifiedType(ast::UintTy),
29-
FloatSimplifiedType(ast::FloatTy),
26+
IntSimplifiedType(ty::IntTy),
27+
UintSimplifiedType(ty::UintTy),
28+
FloatSimplifiedType(ty::FloatTy),
3029
AdtSimplifiedType(D),
3130
StrSimplifiedType,
3231
ArraySimplifiedType,

compiler/rustc_middle/src/ty/layout.rs

+34-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::mir::{GeneratorLayout, GeneratorSavedLocal};
44
use crate::ty::subst::Subst;
55
use crate::ty::{self, subst::SubstsRef, ReprOptions, Ty, TyCtxt, TypeFoldable};
66

7-
use rustc_ast::{self as ast, IntTy, UintTy};
7+
use rustc_ast as ast;
88
use rustc_attr as attr;
99
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1010
use rustc_hir as hir;
@@ -30,6 +30,8 @@ use std::ops::Bound;
3030
pub trait IntegerExt {
3131
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>, signed: bool) -> Ty<'tcx>;
3232
fn from_attr<C: HasDataLayout>(cx: &C, ity: attr::IntType) -> Integer;
33+
fn from_int_ty<C: HasDataLayout>(cx: &C, ity: ty::IntTy) -> Integer;
34+
fn from_uint_ty<C: HasDataLayout>(cx: &C, uty: ty::UintTy) -> Integer;
3335
fn repr_discr<'tcx>(
3436
tcx: TyCtxt<'tcx>,
3537
ty: Ty<'tcx>,
@@ -60,17 +62,38 @@ impl IntegerExt for Integer {
6062
let dl = cx.data_layout();
6163

6264
match ity {
63-
attr::SignedInt(IntTy::I8) | attr::UnsignedInt(UintTy::U8) => I8,
64-
attr::SignedInt(IntTy::I16) | attr::UnsignedInt(UintTy::U16) => I16,
65-
attr::SignedInt(IntTy::I32) | attr::UnsignedInt(UintTy::U32) => I32,
66-
attr::SignedInt(IntTy::I64) | attr::UnsignedInt(UintTy::U64) => I64,
67-
attr::SignedInt(IntTy::I128) | attr::UnsignedInt(UintTy::U128) => I128,
68-
attr::SignedInt(IntTy::Isize) | attr::UnsignedInt(UintTy::Usize) => {
65+
attr::SignedInt(ast::IntTy::I8) | attr::UnsignedInt(ast::UintTy::U8) => I8,
66+
attr::SignedInt(ast::IntTy::I16) | attr::UnsignedInt(ast::UintTy::U16) => I16,
67+
attr::SignedInt(ast::IntTy::I32) | attr::UnsignedInt(ast::UintTy::U32) => I32,
68+
attr::SignedInt(ast::IntTy::I64) | attr::UnsignedInt(ast::UintTy::U64) => I64,
69+
attr::SignedInt(ast::IntTy::I128) | attr::UnsignedInt(ast::UintTy::U128) => I128,
70+
attr::SignedInt(ast::IntTy::Isize) | attr::UnsignedInt(ast::UintTy::Usize) => {
6971
dl.ptr_sized_integer()
7072
}
7173
}
7274
}
7375

76+
fn from_int_ty<C: HasDataLayout>(cx: &C, ity: ty::IntTy) -> Integer {
77+
match ity {
78+
ty::IntTy::I8 => I8,
79+
ty::IntTy::I16 => I16,
80+
ty::IntTy::I32 => I32,
81+
ty::IntTy::I64 => I64,
82+
ty::IntTy::I128 => I128,
83+
ty::IntTy::Isize => cx.data_layout().ptr_sized_integer(),
84+
}
85+
}
86+
fn from_uint_ty<C: HasDataLayout>(cx: &C, ity: ty::UintTy) -> Integer {
87+
match ity {
88+
ty::UintTy::U8 => I8,
89+
ty::UintTy::U16 => I16,
90+
ty::UintTy::U32 => I32,
91+
ty::UintTy::U64 => I64,
92+
ty::UintTy::U128 => I128,
93+
ty::UintTy::Usize => cx.data_layout().ptr_sized_integer(),
94+
}
95+
}
96+
7497
/// Finds the appropriate Integer type and signedness for the given
7598
/// signed discriminant range and `#[repr]` attribute.
7699
/// N.B.: `u128` values above `i128::MAX` will be treated as signed, but
@@ -487,11 +510,11 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
487510
self,
488511
Scalar { value: Int(I32, false), valid_range: 0..=0x10FFFF },
489512
)),
490-
ty::Int(ity) => scalar(Int(Integer::from_attr(dl, attr::SignedInt(ity)), true)),
491-
ty::Uint(ity) => scalar(Int(Integer::from_attr(dl, attr::UnsignedInt(ity)), false)),
513+
ty::Int(ity) => scalar(Int(Integer::from_int_ty(dl, ity), true)),
514+
ty::Uint(ity) => scalar(Int(Integer::from_uint_ty(dl, ity), false)),
492515
ty::Float(fty) => scalar(match fty {
493-
ast::FloatTy::F32 => F32,
494-
ast::FloatTy::F64 => F64,
516+
ty::FloatTy::F32 => F32,
517+
ty::FloatTy::F64 => F64,
495518
}),
496519
ty::FnPtr(_) => {
497520
let mut ptr = scalar_unit(Pointer);

compiler/rustc_middle/src/ty/mod.rs

+51
Original file line numberDiff line numberDiff line change
@@ -3045,6 +3045,57 @@ pub fn is_impl_trait_defn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
30453045
None
30463046
}
30473047

3048+
pub fn int_ty(ity: ast::IntTy) -> IntTy {
3049+
match ity {
3050+
ast::IntTy::Isize => IntTy::Isize,
3051+
ast::IntTy::I8 => IntTy::I8,
3052+
ast::IntTy::I16 => IntTy::I16,
3053+
ast::IntTy::I32 => IntTy::I32,
3054+
ast::IntTy::I64 => IntTy::I64,
3055+
ast::IntTy::I128 => IntTy::I128,
3056+
}
3057+
}
3058+
3059+
pub fn uint_ty(uty: ast::UintTy) -> UintTy {
3060+
match uty {
3061+
ast::UintTy::Usize => UintTy::Usize,
3062+
ast::UintTy::U8 => UintTy::U8,
3063+
ast::UintTy::U16 => UintTy::U16,
3064+
ast::UintTy::U32 => UintTy::U32,
3065+
ast::UintTy::U64 => UintTy::U64,
3066+
ast::UintTy::U128 => UintTy::U128,
3067+
}
3068+
}
3069+
3070+
pub fn float_ty(fty: ast::FloatTy) -> FloatTy {
3071+
match fty {
3072+
ast::FloatTy::F32 => FloatTy::F32,
3073+
ast::FloatTy::F64 => FloatTy::F64,
3074+
}
3075+
}
3076+
3077+
pub fn ast_int_ty(ity: IntTy) -> ast::IntTy {
3078+
match ity {
3079+
IntTy::Isize => ast::IntTy::Isize,
3080+
IntTy::I8 => ast::IntTy::I8,
3081+
IntTy::I16 => ast::IntTy::I16,
3082+
IntTy::I32 => ast::IntTy::I32,
3083+
IntTy::I64 => ast::IntTy::I64,
3084+
IntTy::I128 => ast::IntTy::I128,
3085+
}
3086+
}
3087+
3088+
pub fn ast_uint_ty(uty: UintTy) -> ast::UintTy {
3089+
match uty {
3090+
UintTy::Usize => ast::UintTy::Usize,
3091+
UintTy::U8 => ast::UintTy::U8,
3092+
UintTy::U16 => ast::UintTy::U16,
3093+
UintTy::U32 => ast::UintTy::U32,
3094+
UintTy::U64 => ast::UintTy::U64,
3095+
UintTy::U128 => ast::UintTy::U128,
3096+
}
3097+
}
3098+
30483099
pub fn provide(providers: &mut ty::query::Providers) {
30493100
context::provide(providers);
30503101
erase_regions::provide(providers);

compiler/rustc_middle/src/ty/print/pretty.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::mir::interpret::{AllocId, ConstValue, GlobalAlloc, Pointer, Scalar};
33
use crate::ty::subst::{GenericArg, GenericArgKind, Subst};
44
use crate::ty::{self, ConstInt, DefIdTree, ParamConst, ScalarInt, Ty, TyCtxt, TypeFoldable};
55
use rustc_apfloat::ieee::{Double, Single};
6-
use rustc_ast as ast;
76
use rustc_data_structures::fx::FxHashMap;
87
use rustc_hir as hir;
98
use rustc_hir::def::{self, CtorKind, DefKind, Namespace};
@@ -973,7 +972,7 @@ pub trait PrettyPrinter<'tcx>:
973972
ty::TyS {
974973
kind:
975974
ty::Array(
976-
ty::TyS { kind: ty::Uint(ast::UintTy::U8), .. },
975+
ty::TyS { kind: ty::Uint(ty::UintTy::U8), .. },
977976
ty::Const {
978977
val: ty::ConstKind::Value(ConstValue::Scalar(int)),
979978
..
@@ -1002,10 +1001,10 @@ pub trait PrettyPrinter<'tcx>:
10021001
(Scalar::Int(int), ty::Bool) if int == ScalarInt::FALSE => p!("false"),
10031002
(Scalar::Int(int), ty::Bool) if int == ScalarInt::TRUE => p!("true"),
10041003
// Float
1005-
(Scalar::Int(int), ty::Float(ast::FloatTy::F32)) => {
1004+
(Scalar::Int(int), ty::Float(ty::FloatTy::F32)) => {
10061005
p!(write("{}f32", Single::try_from(int).unwrap()))
10071006
}
1008-
(Scalar::Int(int), ty::Float(ast::FloatTy::F64)) => {
1007+
(Scalar::Int(int), ty::Float(ty::FloatTy::F64)) => {
10091008
p!(write("{}f64", Double::try_from(int).unwrap()))
10101009
}
10111010
// Int

compiler/rustc_middle/src/ty/sty.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use crate::ty::{
1212
};
1313
use crate::ty::{DelaySpanBugEmitted, List, ParamEnv, TyS};
1414
use polonius_engine::Atom;
15-
use rustc_ast as ast;
1615
use rustc_data_structures::captures::Captures;
1716
use rustc_hir as hir;
1817
use rustc_hir::def_id::DefId;
@@ -104,13 +103,13 @@ pub enum TyKind<'tcx> {
104103
Char,
105104

106105
/// A primitive signed integer type. For example, `i32`.
107-
Int(ast::IntTy),
106+
Int(ty::IntTy),
108107

109108
/// A primitive unsigned integer type. For example, `u32`.
110-
Uint(ast::UintTy),
109+
Uint(ty::UintTy),
111110

112111
/// A primitive floating-point type. For example, `f64`.
113-
Float(ast::FloatTy),
112+
Float(ty::FloatTy),
114113

115114
/// Algebraic data types (ADT). For example: structures, enumerations and unions.
116115
///
@@ -1798,7 +1797,7 @@ impl<'tcx> TyS<'tcx> {
17981797
pub fn sequence_element_type(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
17991798
match self.kind() {
18001799
Array(ty, _) | Slice(ty) => ty,
1801-
Str => tcx.mk_mach_uint(ast::UintTy::U8),
1800+
Str => tcx.mk_mach_uint(ty::UintTy::U8),
18021801
_ => bug!("`sequence_element_type` called on non-sequence value: {}", self),
18031802
}
18041803
}
@@ -1938,7 +1937,7 @@ impl<'tcx> TyS<'tcx> {
19381937

19391938
#[inline]
19401939
pub fn is_ptr_sized_integral(&self) -> bool {
1941-
matches!(self.kind(), Int(ast::IntTy::Isize) | Uint(ast::UintTy::Usize))
1940+
matches!(self.kind(), Int(ty::IntTy::Isize) | Uint(ty::UintTy::Usize))
19421941
}
19431942

19441943
#[inline]
@@ -2126,9 +2125,9 @@ impl<'tcx> TyS<'tcx> {
21262125
pub fn to_opt_closure_kind(&self) -> Option<ty::ClosureKind> {
21272126
match self.kind() {
21282127
Int(int_ty) => match int_ty {
2129-
ast::IntTy::I8 => Some(ty::ClosureKind::Fn),
2130-
ast::IntTy::I16 => Some(ty::ClosureKind::FnMut),
2131-
ast::IntTy::I32 => Some(ty::ClosureKind::FnOnce),
2128+
ty::IntTy::I8 => Some(ty::ClosureKind::Fn),
2129+
ty::IntTy::I16 => Some(ty::ClosureKind::FnMut),
2130+
ty::IntTy::I32 => Some(ty::ClosureKind::FnOnce),
21322131
_ => bug!("cannot convert type `{:?}` to a closure kind", self),
21332132
},
21342133

0 commit comments

Comments
 (0)