Skip to content

Commit a9aca2b

Browse files
committed
rustc: replace usize with u64 and ConstUsize.
1 parent 7f32c88 commit a9aca2b

File tree

49 files changed

+265
-274
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+265
-274
lines changed

src/librustc/middle/const_val.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,15 @@ impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
250250
pub fn eval_length(tcx: TyCtxt,
251251
count: hir::BodyId,
252252
reason: &str)
253-
-> Result<usize, ErrorReported>
253+
-> Result<ConstUsize, ErrorReported>
254254
{
255255
let count_expr = &tcx.hir.body(count).value;
256256
let count_def_id = tcx.hir.body_owner_def_id(count);
257257
let param_env = ty::ParamEnv::empty(Reveal::UserFacing);
258258
let substs = Substs::identity_for_item(tcx.global_tcx(), count_def_id);
259259
match tcx.at(count_expr.span).const_eval(param_env.and((count_def_id, substs))) {
260-
Ok(&ty::Const { val: Integral(Usize(count)), .. }) => {
261-
let val = count.as_u64(tcx.sess.target.uint_type);
262-
assert_eq!(val as usize as u64, val);
263-
Ok(val as usize)
264-
},
265-
Ok(_) |
266-
Err(ConstEvalErr { kind: ErrKind::TypeckError, .. }) => Err(ErrorReported),
260+
Ok(&ty::Const { val: Integral(Usize(count)), .. }) => Ok(count),
261+
Ok(_) | Err(ConstEvalErr { kind: ErrKind::TypeckError, .. }) => Err(ErrorReported),
267262
Err(err) => {
268263
let mut diag = err.struct_error(tcx, count_expr.span, reason);
269264

src/librustc/middle/mem_categorization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
879879

880880
// Always promote `[T; 0]` (even when e.g. borrowed mutably).
881881
let promotable = match expr_ty.sty {
882-
ty::TyArray(_, 0) => true,
882+
ty::TyArray(_, len) if len.as_u64() == 0 => true,
883883
_ => promotable,
884884
};
885885

src/librustc/mir/tcx.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ impl<'a, 'gcx, 'tcx> LvalueTy<'tcx> {
7070
LvalueTy::Ty {
7171
ty: match ty.sty {
7272
ty::TyArray(inner, size) => {
73-
tcx.mk_array(inner, size-(from as usize)-(to as usize))
73+
let len = size.as_u64() - (from as u64) - (to as u64);
74+
tcx.mk_array(inner, len)
7475
}
7576
ty::TySlice(..) => ty,
7677
_ => {
@@ -146,11 +147,8 @@ impl<'tcx> Rvalue<'tcx> {
146147
{
147148
match *self {
148149
Rvalue::Use(ref operand) => operand.ty(local_decls, tcx),
149-
Rvalue::Repeat(ref operand, ref count) => {
150-
let op_ty = operand.ty(local_decls, tcx);
151-
let count = count.as_u64(tcx.sess.target.uint_type);
152-
assert_eq!(count as usize as u64, count);
153-
tcx.mk_array(op_ty, count as usize)
150+
Rvalue::Repeat(ref operand, count) => {
151+
tcx.mk_array(operand.ty(local_decls, tcx), count.as_u64())
154152
}
155153
Rvalue::Ref(reg, bk, ref lv) => {
156154
let lv_ty = lv.ty(local_decls, tcx).to_ty(tcx);
@@ -193,7 +191,7 @@ impl<'tcx> Rvalue<'tcx> {
193191
Rvalue::Aggregate(ref ak, ref ops) => {
194192
match **ak {
195193
AggregateKind::Array(ty) => {
196-
tcx.mk_array(ty, ops.len())
194+
tcx.mk_array(ty, ops.len() as u64)
197195
}
198196
AggregateKind::Tuple => {
199197
tcx.mk_tup(

src/librustc/session/config.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ use std::path::PathBuf;
4848

4949
pub struct Config {
5050
pub target: Target,
51-
pub int_type: IntTy,
52-
pub uint_type: UintTy,
51+
pub isize_ty: IntTy,
52+
pub usize_ty: UintTy,
5353
}
5454

5555
#[derive(Clone, Hash, Debug)]
@@ -1146,7 +1146,7 @@ pub fn build_target_config(opts: &Options, sp: &Handler) -> Config {
11461146
}
11471147
};
11481148

1149-
let (int_type, uint_type) = match &target.target_pointer_width[..] {
1149+
let (isize_ty, usize_ty) = match &target.target_pointer_width[..] {
11501150
"16" => (ast::IntTy::I16, ast::UintTy::U16),
11511151
"32" => (ast::IntTy::I32, ast::UintTy::U32),
11521152
"64" => (ast::IntTy::I64, ast::UintTy::U64),
@@ -1156,8 +1156,8 @@ pub fn build_target_config(opts: &Options, sp: &Handler) -> Config {
11561156

11571157
Config {
11581158
target,
1159-
int_type,
1160-
uint_type,
1159+
isize_ty,
1160+
usize_ty,
11611161
}
11621162
}
11631163

src/librustc/ty/context.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
5050
StableHasherResult};
5151

5252
use arena::{TypedArena, DroplessArena};
53+
use rustc_const_math::ConstUsize;
5354
use rustc_data_structures::indexed_vec::IndexVec;
5455
use std::borrow::Borrow;
5556
use std::cell::{Cell, RefCell};
@@ -1740,7 +1741,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
17401741
self.mk_imm_ptr(self.mk_nil())
17411742
}
17421743

1743-
pub fn mk_array(self, ty: Ty<'tcx>, n: usize) -> Ty<'tcx> {
1744+
pub fn mk_array(self, ty: Ty<'tcx>, n: u64) -> Ty<'tcx> {
1745+
let n = ConstUsize::new(n, self.sess.target.usize_ty).unwrap();
17441746
self.mk_ty(TyArray(ty, n))
17451747
}
17461748

src/librustc/ty/error.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use syntax::ast;
1818
use errors::DiagnosticBuilder;
1919
use syntax_pos::Span;
2020

21+
use rustc_const_math::ConstUsize;
22+
2123
use hir;
2224

2325
#[derive(Clone, Copy, Debug)]
@@ -34,7 +36,7 @@ pub enum TypeError<'tcx> {
3436
AbiMismatch(ExpectedFound<abi::Abi>),
3537
Mutability,
3638
TupleSize(ExpectedFound<usize>),
37-
FixedArraySize(ExpectedFound<usize>),
39+
FixedArraySize(ExpectedFound<ConstUsize>),
3840
ArgCount,
3941

4042
RegionsDoesNotOutlive(Region<'tcx>, Region<'tcx>),

src/librustc/ty/inhabitedness/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
205205
}))
206206
},
207207
TyArray(ty, len) => {
208-
if len == 0 {
208+
if len.as_u64() == 0 {
209209
DefIdForest::empty()
210210
} else {
211211
ty.uninhabited_from(visited, tcx)

src/librustc/ty/layout.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ impl<'a, 'tcx> Struct {
837837

838838
// Is this a fixed-size array of something non-zero
839839
// with at least one element?
840-
(_, &ty::TyArray(ety, d)) if d > 0 => {
840+
(_, &ty::TyArray(ety, d)) if d.as_u64() > 0 => {
841841
Struct::non_zero_field_paths(
842842
tcx,
843843
param_env,
@@ -1177,9 +1177,7 @@ impl<'a, 'tcx> Layout {
11771177
ty::TyArray(element, count) => {
11781178
let element = element.layout(tcx, param_env)?;
11791179
let element_size = element.size(dl);
1180-
// FIXME(eddyb) Don't use host `usize` for array lengths.
1181-
let usize_count: usize = count;
1182-
let count = usize_count as u64;
1180+
let count = count.as_u64();
11831181
if element_size.checked_mul(count, dl).is_none() {
11841182
return Err(LayoutError::SizeOverflow(ty));
11851183
}

src/librustc/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,11 +1661,11 @@ impl<'a, 'gcx, 'tcx> AdtDef {
16611661
match repr_type {
16621662
attr::UnsignedInt(ty) => {
16631663
ConstInt::new_unsigned_truncating(discr, ty,
1664-
tcx.sess.target.uint_type)
1664+
tcx.sess.target.usize_ty)
16651665
}
16661666
attr::SignedInt(ty) => {
16671667
ConstInt::new_signed_truncating(discr as i128, ty,
1668-
tcx.sess.target.int_type)
1668+
tcx.sess.target.isize_ty)
16691669
}
16701670
}
16711671
}

src/librustc/ty/relate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
429429
{
430430
let t = relation.relate(&a_t, &b_t)?;
431431
if sz_a == sz_b {
432-
Ok(tcx.mk_array(t, sz_a))
432+
Ok(tcx.mk_array(t, sz_a.as_u64()))
433433
} else {
434434
Err(TypeError::FixedArraySize(expected_found(relation, &sz_a, &sz_b)))
435435
}

src/librustc/ty/sty.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use syntax::ast::{self, Name};
2727
use syntax::symbol::keywords;
2828
use util::nodemap::FxHashMap;
2929

30+
use rustc_const_math::ConstUsize;
31+
3032
use serialize;
3133

3234
use hir;
@@ -110,7 +112,7 @@ pub enum TypeVariants<'tcx> {
110112
TyStr,
111113

112114
/// An array with the given length. Written as `[T; n]`.
113-
TyArray(Ty<'tcx>, usize),
115+
TyArray(Ty<'tcx>, ConstUsize),
114116

115117
/// The pointee of an array slice. Written as `[T]`.
116118
TySlice(Ty<'tcx>),

src/librustc/ty/util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ macro_rules! typed_literal {
5353
SignedInt(ast::IntTy::I32) => ConstInt::I32($lit),
5454
SignedInt(ast::IntTy::I64) => ConstInt::I64($lit),
5555
SignedInt(ast::IntTy::I128) => ConstInt::I128($lit),
56-
SignedInt(ast::IntTy::Is) => match $tcx.sess.target.int_type {
56+
SignedInt(ast::IntTy::Is) => match $tcx.sess.target.isize_ty {
5757
ast::IntTy::I16 => ConstInt::Isize(ConstIsize::Is16($lit)),
5858
ast::IntTy::I32 => ConstInt::Isize(ConstIsize::Is32($lit)),
5959
ast::IntTy::I64 => ConstInt::Isize(ConstIsize::Is64($lit)),
@@ -64,7 +64,7 @@ macro_rules! typed_literal {
6464
UnsignedInt(ast::UintTy::U32) => ConstInt::U32($lit),
6565
UnsignedInt(ast::UintTy::U64) => ConstInt::U64($lit),
6666
UnsignedInt(ast::UintTy::U128) => ConstInt::U128($lit),
67-
UnsignedInt(ast::UintTy::Us) => match $tcx.sess.target.uint_type {
67+
UnsignedInt(ast::UintTy::Us) => match $tcx.sess.target.usize_ty {
6868
ast::UintTy::U16 => ConstInt::Usize(ConstUsize::Us16($lit)),
6969
ast::UintTy::U32 => ConstInt::Usize(ConstUsize::Us32($lit)),
7070
ast::UintTy::U64 => ConstInt::Usize(ConstUsize::Us64($lit)),
@@ -638,7 +638,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
638638
}
639639

640640
pub fn const_usize(&self, val: u16) -> ConstInt {
641-
match self.sess.target.uint_type {
641+
match self.sess.target.usize_ty {
642642
ast::UintTy::U16 => ConstInt::Usize(ConstUsize::Us16(val as u16)),
643643
ast::UintTy::U32 => ConstInt::Usize(ConstUsize::Us32(val as u32)),
644644
ast::UintTy::U64 => ConstInt::Usize(ConstUsize::Us64(val as u64)),

src/librustc_const_eval/_match.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub enum Constructor<'tcx> {
235235
/// Ranges of literal values (`2...5` and `2..5`).
236236
ConstantRange(&'tcx ty::Const<'tcx>, &'tcx ty::Const<'tcx>, RangeEnd),
237237
/// Array patterns of length n.
238-
Slice(usize),
238+
Slice(u64),
239239
}
240240

241241
impl<'tcx> Constructor<'tcx> {
@@ -276,7 +276,7 @@ pub enum WitnessPreference {
276276
#[derive(Copy, Clone, Debug)]
277277
struct PatternContext<'tcx> {
278278
ty: Ty<'tcx>,
279-
max_slice_length: usize,
279+
max_slice_length: u64,
280280
}
281281

282282
/// A stack of patterns in reverse order of construction
@@ -330,8 +330,8 @@ impl<'tcx> Witness<'tcx> {
330330
{
331331
let arity = constructor_arity(cx, ctor, ty);
332332
let pat = {
333-
let len = self.0.len();
334-
let mut pats = self.0.drain(len-arity..).rev();
333+
let len = self.0.len() as u64;
334+
let mut pats = self.0.drain((len-arity) as usize..).rev();
335335

336336
match ty.sty {
337337
ty::TyAdt(..) |
@@ -423,10 +423,10 @@ fn all_constructors<'a, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
423423
}
424424
}
425425
ty::TyArray(ref sub_ty, length) => {
426-
if length > 0 && cx.is_uninhabited(sub_ty) {
426+
if length.as_u64() > 0 && cx.is_uninhabited(sub_ty) {
427427
vec![]
428428
} else {
429-
vec![Slice(length)]
429+
vec![Slice(length.as_u64())]
430430
}
431431
}
432432
ty::TyAdt(def, substs) if def.is_enum() && def.variants.len() != 1 => {
@@ -447,7 +447,7 @@ fn all_constructors<'a, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
447447

448448
fn max_slice_length<'p, 'a: 'p, 'tcx: 'a, I>(
449449
_cx: &mut MatchCheckCtxt<'a, 'tcx>,
450-
patterns: I) -> usize
450+
patterns: I) -> u64
451451
where I: Iterator<Item=&'p Pattern<'tcx>>
452452
{
453453
// The exhaustiveness-checking paper does not include any details on
@@ -521,15 +521,15 @@ fn max_slice_length<'p, 'a: 'p, 'tcx: 'a, I>(
521521
for row in patterns {
522522
match *row.kind {
523523
PatternKind::Constant { value: &ty::Const { val: ConstVal::ByteStr(b), .. } } => {
524-
max_fixed_len = cmp::max(max_fixed_len, b.data.len());
524+
max_fixed_len = cmp::max(max_fixed_len, b.data.len() as u64);
525525
}
526526
PatternKind::Slice { ref prefix, slice: None, ref suffix } => {
527-
let fixed_len = prefix.len() + suffix.len();
527+
let fixed_len = prefix.len() as u64 + suffix.len() as u64;
528528
max_fixed_len = cmp::max(max_fixed_len, fixed_len);
529529
}
530530
PatternKind::Slice { ref prefix, slice: Some(_), ref suffix } => {
531-
max_prefix_len = cmp::max(max_prefix_len, prefix.len());
532-
max_suffix_len = cmp::max(max_suffix_len, suffix.len());
531+
max_prefix_len = cmp::max(max_prefix_len, prefix.len() as u64);
532+
max_suffix_len = cmp::max(max_suffix_len, suffix.len() as u64);
533533
}
534534
_ => {}
535535
}
@@ -729,11 +729,11 @@ fn pat_constructors<'tcx>(_cx: &mut MatchCheckCtxt,
729729
PatternKind::Range { lo, hi, end } =>
730730
Some(vec![ConstantRange(lo, hi, end)]),
731731
PatternKind::Array { .. } => match pcx.ty.sty {
732-
ty::TyArray(_, length) => Some(vec![Slice(length)]),
732+
ty::TyArray(_, length) => Some(vec![Slice(length.as_u64())]),
733733
_ => span_bug!(pat.span, "bad ty {:?} for array pattern", pcx.ty)
734734
},
735735
PatternKind::Slice { ref prefix, ref slice, ref suffix } => {
736-
let pat_len = prefix.len() + suffix.len();
736+
let pat_len = prefix.len() as u64 + suffix.len() as u64;
737737
if slice.is_some() {
738738
Some((pat_len..pcx.max_slice_length+1).map(Slice).collect())
739739
} else {
@@ -748,18 +748,18 @@ fn pat_constructors<'tcx>(_cx: &mut MatchCheckCtxt,
748748
///
749749
/// For instance, a tuple pattern (_, 42, Some([])) has the arity of 3.
750750
/// A struct pattern's arity is the number of fields it contains, etc.
751-
fn constructor_arity(_cx: &MatchCheckCtxt, ctor: &Constructor, ty: Ty) -> usize {
751+
fn constructor_arity(_cx: &MatchCheckCtxt, ctor: &Constructor, ty: Ty) -> u64 {
752752
debug!("constructor_arity({:?}, {:?})", ctor, ty);
753753
match ty.sty {
754-
ty::TyTuple(ref fs, _) => fs.len(),
754+
ty::TyTuple(ref fs, _) => fs.len() as u64,
755755
ty::TySlice(..) | ty::TyArray(..) => match *ctor {
756756
Slice(length) => length,
757757
ConstantValue(_) => 0,
758758
_ => bug!("bad slice pattern {:?} {:?}", ctor, ty)
759759
},
760760
ty::TyRef(..) => 1,
761761
ty::TyAdt(adt, _) => {
762-
adt.variants[ctor.variant_index_for_adt(adt)].fields.len()
762+
adt.variants[ctor.variant_index_for_adt(adt)].fields.len() as u64
763763
}
764764
_ => 0
765765
}
@@ -777,7 +777,7 @@ fn constructor_sub_pattern_tys<'a, 'tcx: 'a>(cx: &MatchCheckCtxt<'a, 'tcx>,
777777
match ty.sty {
778778
ty::TyTuple(ref fs, _) => fs.into_iter().map(|t| *t).collect(),
779779
ty::TySlice(ty) | ty::TyArray(ty, _) => match *ctor {
780-
Slice(length) => repeat(ty).take(length).collect(),
780+
Slice(length) => (0..length).map(|_| ty).collect(),
781781
ConstantValue(_) => vec![],
782782
_ => bug!("bad slice pattern {:?} {:?}", ctor, ty)
783783
},

0 commit comments

Comments
 (0)