Skip to content

Commit 85801fa

Browse files
authored
Merge pull request rust-lang#87 from oli-obk/no_u_size_is_wong
replace most uses of `usize` with `u64`
2 parents 0b632d5 + 0039ebc commit 85801fa

File tree

10 files changed

+207
-174
lines changed

10 files changed

+207
-174
lines changed

src/bin/miri.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
5353
NestedMetaItemKind::MetaItem(ref inner) => match inner.node {
5454
MetaItemKind::NameValue(ref name, ref value) => {
5555
match &**name {
56-
"memory_size" => memory_size = extract_int(value) as usize,
56+
"memory_size" => memory_size = extract_int(value),
5757
"step_limit" => step_limit = extract_int(value),
5858
"stack_limit" => stack_limit = extract_int(value) as usize,
5959
_ => state.session.span_err(item.span, "unknown miri attribute"),

src/error.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ pub enum EvalError<'tcx> {
1818
InvalidDiscriminant,
1919
PointerOutOfBounds {
2020
ptr: Pointer,
21-
size: usize,
22-
allocation_size: usize,
21+
size: u64,
22+
allocation_size: u64,
2323
},
2424
ReadPointerAsBytes,
2525
InvalidPointerMath,
@@ -32,15 +32,15 @@ pub enum EvalError<'tcx> {
3232
Math(Span, ConstMathErr),
3333
InvalidChar(u64),
3434
OutOfMemory {
35-
allocation_size: usize,
36-
memory_size: usize,
37-
memory_usage: usize,
35+
allocation_size: u64,
36+
memory_size: u64,
37+
memory_usage: u64,
3838
},
3939
ExecutionTimeLimitReached,
4040
StackFrameLimitReached,
4141
AlignmentCheckFailed {
42-
required: usize,
43-
has: usize,
42+
required: u64,
43+
has: u64,
4444
},
4545
CalledClosureAsFunction,
4646
VtableForArgumentlessMethod,

src/interpreter/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
6767
TyChar if v as u8 as u64 == v => Ok(PrimVal::new(v, Char)),
6868
TyChar => Err(EvalError::InvalidChar(v)),
6969

70-
TyRawPtr(_) => Ok(PrimVal::from_ptr(Pointer::from_int(v as usize))),
70+
TyRawPtr(_) => Ok(PrimVal::from_ptr(Pointer::from_int(v))),
7171

7272
_ => Err(EvalError::Unimplemented(format!("int to {:?} cast", ty))),
7373
}

src/interpreter/mod.rs

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub enum StackPopCleanup {
167167
}
168168

169169
impl<'a, 'tcx> EvalContext<'a, 'tcx> {
170-
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>, memory_size: usize, stack_limit: usize, step_limit: u64) -> Self {
170+
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>, memory_size: u64, stack_limit: usize, step_limit: u64) -> Self {
171171
EvalContext {
172172
tcx: tcx,
173173
memory: Memory::new(&tcx.data_layout, memory_size),
@@ -211,7 +211,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
211211

212212
fn str_to_value(&mut self, s: &str) -> EvalResult<'tcx, Value> {
213213
// FIXME: cache these allocs
214-
let ptr = self.memory.allocate(s.len(), 1)?;
214+
let ptr = self.memory.allocate(s.len() as u64, 1)?;
215215
self.memory.write_bytes(ptr, s.as_bytes())?;
216216
self.memory.freeze(ptr.alloc_id)?;
217217
Ok(Value::ByValPair(PrimVal::from_ptr(ptr), self.usize_primval(s.len() as u64)))
@@ -255,7 +255,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
255255
Str(ref s) => return self.str_to_value(s),
256256

257257
ByteStr(ref bs) => {
258-
let ptr = self.memory.allocate(bs.len(), 1)?;
258+
let ptr = self.memory.allocate(bs.len() as u64, 1)?;
259259
self.memory.write_bytes(ptr, bs)?;
260260
self.memory.freeze(ptr.alloc_id)?;
261261
PrimVal::from_ptr(ptr)
@@ -292,25 +292,25 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
292292
self.tcx.normalize_associated_type(&substituted)
293293
}
294294

295-
fn type_size(&self, ty: Ty<'tcx>) -> EvalResult<'tcx, Option<usize>> {
295+
fn type_size(&self, ty: Ty<'tcx>) -> EvalResult<'tcx, Option<u64>> {
296296
self.type_size_with_substs(ty, self.substs())
297297
}
298298

299-
fn type_align(&self, ty: Ty<'tcx>) -> EvalResult<'tcx, usize> {
299+
fn type_align(&self, ty: Ty<'tcx>) -> EvalResult<'tcx, u64> {
300300
self.type_align_with_substs(ty, self.substs())
301301
}
302302

303-
fn type_size_with_substs(&self, ty: Ty<'tcx>, substs: &'tcx Substs<'tcx>) -> EvalResult<'tcx, Option<usize>> {
303+
fn type_size_with_substs(&self, ty: Ty<'tcx>, substs: &'tcx Substs<'tcx>) -> EvalResult<'tcx, Option<u64>> {
304304
let layout = self.type_layout_with_substs(ty, substs)?;
305305
if layout.is_unsized() {
306306
Ok(None)
307307
} else {
308-
Ok(Some(layout.size(&self.tcx.data_layout).bytes() as usize))
308+
Ok(Some(layout.size(&self.tcx.data_layout).bytes()))
309309
}
310310
}
311311

312-
fn type_align_with_substs(&self, ty: Ty<'tcx>, substs: &'tcx Substs<'tcx>) -> EvalResult<'tcx, usize> {
313-
self.type_layout_with_substs(ty, substs).map(|layout| layout.align(&self.tcx.data_layout).abi() as usize)
312+
fn type_align_with_substs(&self, ty: Ty<'tcx>, substs: &'tcx Substs<'tcx>) -> EvalResult<'tcx, u64> {
313+
self.type_layout_with_substs(ty, substs).map(|layout| layout.align(&self.tcx.data_layout).abi())
314314
}
315315

316316
fn type_layout(&self, ty: Ty<'tcx>) -> EvalResult<'tcx, &'tcx Layout> {
@@ -464,7 +464,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
464464
for (offset, operand) in offsets.into_iter().zip(operands) {
465465
let value = self.eval_operand(operand)?;
466466
let value_ty = self.operand_ty(operand);
467-
let field_dest = dest.offset(offset as isize);
467+
let field_dest = dest.offset(offset);
468468
self.write_value_to_ptr(value, field_dest, value_ty)?;
469469
}
470470
Ok(())
@@ -525,8 +525,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
525525
General { discr, ref variants, .. } => {
526526
if let mir::AggregateKind::Adt(adt_def, variant, _, _) = *kind {
527527
let discr_val = adt_def.variants[variant].disr_val.to_u64_unchecked();
528-
let discr_size = discr.size().bytes() as usize;
529-
let discr_offset = variants[variant].offsets[0].bytes() as isize;
528+
let discr_size = discr.size().bytes();
529+
let discr_offset = variants[variant].offsets[0].bytes();
530530

531531
// FIXME(solson)
532532
let dest = self.force_allocation(dest)?;
@@ -581,7 +581,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
581581
// FIXME(solson)
582582
let dest = self.force_allocation(dest)?.to_ptr();
583583

584-
let dest = dest.offset(offset.bytes() as isize);
584+
let dest = dest.offset(offset.bytes());
585585
let dest_size = self.type_size(ty)?.expect("bad StructWrappedNullablePointer discrfield");
586586
try!(self.memory.write_int(dest, 0, dest_size));
587587
}
@@ -594,7 +594,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
594594
assert_eq!(operands.len(), 0);
595595
if let mir::AggregateKind::Adt(adt_def, variant, _, _) = *kind {
596596
let n = adt_def.variants[variant].disr_val.to_u64_unchecked();
597-
let size = discr.size().bytes() as usize;
597+
let size = discr.size().bytes();
598598

599599
let val = if signed {
600600
PrimVal::from_int_with_size(n as i64, size)
@@ -621,18 +621,18 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
621621

622622
Repeat(ref operand, _) => {
623623
let (elem_ty, length) = match dest_ty.sty {
624-
ty::TyArray(elem_ty, n) => (elem_ty, n),
624+
ty::TyArray(elem_ty, n) => (elem_ty, n as u64),
625625
_ => bug!("tried to assign array-repeat to non-array type {:?}", dest_ty),
626626
};
627-
self.inc_step_counter_and_check_limit(length as u64)?;
627+
self.inc_step_counter_and_check_limit(length)?;
628628
let elem_size = self.type_size(elem_ty)?.expect("repeat element type must be sized");
629629
let value = self.eval_operand(operand)?;
630630

631631
// FIXME(solson)
632632
let dest = self.force_allocation(dest)?.to_ptr();
633633

634634
for i in 0..length {
635-
let elem_dest = dest.offset((i * elem_size) as isize);
635+
let elem_dest = dest.offset(i * elem_size);
636636
self.write_value_to_ptr(value, elem_dest, elem_ty)?;
637637
}
638638
}
@@ -741,15 +741,15 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
741741
}
742742

743743
fn nonnull_offset_and_ty(&self, ty: Ty<'tcx>, nndiscr: u64, discrfield: &[u32]) -> EvalResult<'tcx, (Size, Ty<'tcx>)> {
744-
// Skip the constant 0 at the start meant for LLVM GEP.
745-
let mut path = discrfield.iter().skip(1).map(|&i| i as usize);
744+
// Skip the constant 0 at the start meant for LLVM GEP and the outer non-null variant
745+
let path = discrfield.iter().skip(2).map(|&i| i as usize);
746746

747747
// Handle the field index for the outer non-null variant.
748748
let inner_ty = match ty.sty {
749749
ty::TyAdt(adt_def, substs) => {
750750
let variant = &adt_def.variants[nndiscr as usize];
751-
let index = path.next().unwrap();
752-
let field = &variant.fields[index];
751+
let index = discrfield[1];
752+
let field = &variant.fields[index as usize];
753753
field.ty(self.tcx, substs)
754754
}
755755
_ => bug!("non-enum for StructWrappedNullablePointer: {}", ty),
@@ -804,8 +804,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
804804
Ok(variant.offsets[field_index])
805805
}
806806
FatPointer { .. } => {
807-
let bytes = field_index * self.memory.pointer_size();
808-
Ok(Size::from_bytes(bytes as u64))
807+
let bytes = field_index as u64 * self.memory.pointer_size();
808+
Ok(Size::from_bytes(bytes))
809809
}
810810
_ => {
811811
let msg = format!("can't handle type: {:?}, with layout: {:?}", ty, layout);
@@ -980,7 +980,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
980980
_ => bug!("field access on non-product type: {:?}", base_layout),
981981
};
982982

983-
let ptr = base_ptr.offset(offset.bytes() as isize);
983+
let ptr = base_ptr.offset(offset.bytes());
984984
let extra = if self.type_is_sized(field_ty) {
985985
LvalueExtra::None
986986
} else {
@@ -1048,7 +1048,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
10481048
let n = self.value_to_primval(n_ptr, usize)?
10491049
.expect_uint("Projection::Index expected usize");
10501050
assert!(n < len);
1051-
let ptr = base_ptr.offset(n as isize * elem_size as isize);
1051+
let ptr = base_ptr.offset(n * elem_size);
10521052
(ptr, LvalueExtra::None)
10531053
}
10541054

@@ -1062,12 +1062,12 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
10621062
assert!(n >= min_length as u64);
10631063

10641064
let index = if from_end {
1065-
n as isize - offset as isize
1065+
n - u64::from(offset)
10661066
} else {
1067-
offset as isize
1067+
u64::from(offset)
10681068
};
10691069

1070-
let ptr = base_ptr.offset(index * elem_size as isize);
1070+
let ptr = base_ptr.offset(index * elem_size);
10711071
(ptr, LvalueExtra::None)
10721072
}
10731073

@@ -1078,9 +1078,9 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
10781078

10791079
let (elem_ty, n) = base.elem_ty_and_len(base_ty);
10801080
let elem_size = self.type_size(elem_ty)?.expect("slice element must be sized");
1081-
assert!((from as u64) <= n - (to as u64));
1082-
let ptr = base_ptr.offset(from as isize * elem_size as isize);
1083-
let extra = LvalueExtra::Length(n - to as u64 - from as u64);
1081+
assert!(u64::from(from) <= n - u64::from(to));
1082+
let ptr = base_ptr.offset(u64::from(from) * elem_size);
1083+
let extra = LvalueExtra::Length(n - u64::from(to) - u64::from(from));
10841084
(ptr, extra)
10851085
}
10861086
};
@@ -1318,8 +1318,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
13181318
ty: Ty<'tcx>
13191319
) -> EvalResult<'tcx, ()> {
13201320
assert_eq!(self.get_field_count(ty)?, 2);
1321-
let field_0 = self.get_field_offset(ty, 0)?.bytes() as isize;
1322-
let field_1 = self.get_field_offset(ty, 1)?.bytes() as isize;
1321+
let field_0 = self.get_field_offset(ty, 0)?.bytes();
1322+
let field_1 = self.get_field_offset(ty, 1)?.bytes();
13231323
self.memory.write_primval(ptr.offset(field_0), a)?;
13241324
self.memory.write_primval(ptr.offset(field_1), b)?;
13251325
Ok(())
@@ -1368,7 +1368,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
13681368
ty::TyAdt(..) => {
13691369
use rustc::ty::layout::Layout::*;
13701370
if let CEnum { discr, signed, .. } = *self.type_layout(ty)? {
1371-
let size = discr.size().bytes() as usize;
1371+
let size = discr.size().bytes();
13721372
if signed {
13731373
PrimValKind::from_int_size(size)
13741374
} else {
@@ -1450,7 +1450,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
14501450
PrimVal::from_ptr(p)
14511451
} else {
14521452
trace!("reading fat pointer extra of type {}", ty);
1453-
let extra = ptr.offset(self.memory.pointer_size() as isize);
1453+
let extra = ptr.offset(self.memory.pointer_size());
14541454
let extra = match self.tcx.struct_tail(ty).sty {
14551455
ty::TyTrait(..) => PrimVal::from_ptr(self.memory.read_ptr(extra)?),
14561456
ty::TySlice(..) |
@@ -1464,7 +1464,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
14641464
ty::TyAdt(..) => {
14651465
use rustc::ty::layout::Layout::*;
14661466
if let CEnum { discr, signed, .. } = *self.type_layout(ty)? {
1467-
let size = discr.size().bytes() as usize;
1467+
let size = discr.size().bytes();
14681468
if signed {
14691469
let n = self.memory.read_int(ptr, size)?;
14701470
PrimVal::from_int_with_size(n, size)
@@ -1566,8 +1566,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
15661566
if self.type_size(dst_fty)? == Some(0) {
15671567
continue;
15681568
}
1569-
let src_field_offset = self.get_field_offset(src_ty, i)?.bytes() as isize;
1570-
let dst_field_offset = self.get_field_offset(dest_ty, i)?.bytes() as isize;
1569+
let src_field_offset = self.get_field_offset(src_ty, i)?.bytes();
1570+
let dst_field_offset = self.get_field_offset(dest_ty, i)?.bytes();
15711571
let src_f_ptr = src_ptr.offset(src_field_offset);
15721572
let dst_f_ptr = dest.offset(dst_field_offset);
15731573
if src_fty == dst_fty {
@@ -1699,7 +1699,7 @@ impl<'tcx> Lvalue<'tcx> {
16991699
pub fn eval_main<'a, 'tcx: 'a>(
17001700
tcx: TyCtxt<'a, 'tcx, 'tcx>,
17011701
def_id: DefId,
1702-
memory_size: usize,
1702+
memory_size: u64,
17031703
step_limit: u64,
17041704
stack_limit: usize,
17051705
) {

src/interpreter/terminator/intrinsics.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
4646
let ptr = arg_vals[0].read_ptr(&self.memory)?;
4747
let offset = self.value_to_primval(arg_vals[1], isize)?
4848
.expect_int("arith_offset second arg not isize");
49-
let new_ptr = ptr.offset(offset as isize);
49+
let new_ptr = ptr.signed_offset(offset);
5050
self.write_primval(dest, PrimVal::from_ptr(new_ptr))?;
5151
}
5252

@@ -150,7 +150,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
150150
let dest = arg_vals[1].read_ptr(&self.memory)?;
151151
let count = self.value_to_primval(arg_vals[2], usize)?
152152
.expect_uint("arith_offset second arg not isize");
153-
self.memory.copy(src, dest, count as usize * elem_size, elem_align)?;
153+
self.memory.copy(src, dest, count * elem_size, elem_align)?;
154154
}
155155

156156
"ctpop" |
@@ -220,7 +220,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
220220
"forget" => {}
221221

222222
"init" => {
223-
let size = dest_layout.size(&self.tcx.data_layout).bytes() as usize;
223+
let size = dest_layout.size(&self.tcx.data_layout).bytes();
224224
let init = |this: &mut Self, val: Option<Value>| {
225225
match val {
226226
Some(Value::ByRef(ptr)) => {
@@ -280,12 +280,13 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
280280

281281
"offset" => {
282282
let pointee_ty = substs.type_at(0);
283-
let pointee_size = self.type_size(pointee_ty)?.expect("cannot offset a pointer to an unsized type") as isize;
283+
// FIXME: assuming here that type size is < i64::max_value()
284+
let pointee_size = self.type_size(pointee_ty)?.expect("cannot offset a pointer to an unsized type") as i64;
284285
let offset = self.value_to_primval(arg_vals[1], isize)?
285286
.expect_int("offset second arg not isize");
286287

287288
let ptr = arg_vals[0].read_ptr(&self.memory)?;
288-
let result_ptr = ptr.offset(offset as isize * pointee_size);
289+
let result_ptr = ptr.signed_offset(offset * pointee_size);
289290
self.write_primval(dest, PrimVal::from_ptr(result_ptr))?;
290291
}
291292

@@ -378,7 +379,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
378379
}
379380

380381
"uninit" => {
381-
let size = dest_layout.size(&self.tcx.data_layout).bytes() as usize;
382+
let size = dest_layout.size(&self.tcx.data_layout).bytes();
382383
let uninit = |this: &mut Self, val: Option<Value>| {
383384
match val {
384385
Some(Value::ByRef(ptr)) => {
@@ -482,8 +483,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
482483
ty::TyTrait(..) => {
483484
let (_, vtable) = value.expect_ptr_vtable_pair(&self.memory)?;
484485
// the second entry in the vtable is the dynamic size of the object.
485-
let size = self.memory.read_usize(vtable.offset(pointer_size as isize))?;
486-
let align = self.memory.read_usize(vtable.offset(pointer_size as isize * 2))?;
486+
let size = self.memory.read_usize(vtable.offset(pointer_size))?;
487+
let align = self.memory.read_usize(vtable.offset(pointer_size * 2))?;
487488
Ok((size, align))
488489
}
489490

0 commit comments

Comments
 (0)