Skip to content

Commit 3ce31eb

Browse files
committed
rustc: replace usize with u64 and ConstUsize.
1 parent 932289c commit 3ce31eb

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
@@ -876,7 +876,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
876876

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

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)]
@@ -1149,7 +1149,7 @@ pub fn build_target_config(opts: &Options, sp: &Handler) -> Config {
11491149
}
11501150
};
11511151

1152-
let (int_type, uint_type) = match &target.target_pointer_width[..] {
1152+
let (isize_ty, usize_ty) = match &target.target_pointer_width[..] {
11531153
"16" => (ast::IntTy::I16, ast::UintTy::U16),
11541154
"32" => (ast::IntTy::I32, ast::UintTy::U32),
11551155
"64" => (ast::IntTy::I64, ast::UintTy::U64),
@@ -1159,8 +1159,8 @@ pub fn build_target_config(opts: &Options, sp: &Handler) -> Config {
11591159

11601160
Config {
11611161
target,
1162-
int_type,
1163-
uint_type,
1162+
isize_ty,
1163+
usize_ty,
11641164
}
11651165
}
11661166

src/librustc/ty/context.rs

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

5151
use arena::{TypedArena, DroplessArena};
52+
use rustc_const_math::ConstUsize;
5253
use rustc_data_structures::indexed_vec::IndexVec;
5354
use std::borrow::Borrow;
5455
use std::cell::{Cell, RefCell};
@@ -1754,7 +1755,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
17541755
self.mk_imm_ptr(self.mk_nil())
17551756
}
17561757

1757-
pub fn mk_array(self, ty: Ty<'tcx>, n: usize) -> Ty<'tcx> {
1758+
pub fn mk_array(self, ty: Ty<'tcx>, n: u64) -> Ty<'tcx> {
1759+
let n = ConstUsize::new(n, self.sess.target.usize_ty).unwrap();
17581760
self.mk_ty(TyArray(ty, n))
17591761
}
17601762

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
@@ -1665,11 +1665,11 @@ impl<'a, 'gcx, 'tcx> AdtDef {
16651665
match repr_type {
16661666
attr::UnsignedInt(ty) => {
16671667
ConstInt::new_unsigned_truncating(discr, ty,
1668-
tcx.sess.target.uint_type)
1668+
tcx.sess.target.usize_ty)
16691669
}
16701670
attr::SignedInt(ty) => {
16711671
ConstInt::new_signed_truncating(discr as i128, ty,
1672-
tcx.sess.target.int_type)
1672+
tcx.sess.target.isize_ty)
16731673
}
16741674
}
16751675
}

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
}

0 commit comments

Comments
 (0)