Skip to content

Commit 0bc108a

Browse files
committed
make Size::from* methods generic in the integer type they accept
1 parent f16b491 commit 0bc108a

File tree

9 files changed

+22
-26
lines changed

9 files changed

+22
-26
lines changed

src/librustc/mir/interpret/allocation.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl<Tag> Allocation<Tag> {
9292
/// Creates a read-only allocation initialized by the given bytes
9393
pub fn from_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, align: Align) -> Self {
9494
let bytes = slice.into().into_owned();
95-
let size = Size::from_bytes(u64::try_from(bytes.len()).unwrap());
95+
let size = Size::from_bytes(bytes.len());
9696
Self {
9797
bytes,
9898
relocations: Relocations::new(),
@@ -293,8 +293,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
293293
let offset = usize::try_from(ptr.offset.bytes()).unwrap();
294294
Ok(match self.bytes[offset..].iter().position(|&c| c == 0) {
295295
Some(size) => {
296-
let size_with_null =
297-
Size::from_bytes(u64::try_from(size.checked_add(1).unwrap()).unwrap());
296+
let size_with_null = Size::from_bytes(size.checked_add(1).unwrap());
298297
// Go through `get_bytes` for checks and AllocationExtra hooks.
299298
// We read the null, so we include it in the request, but we want it removed
300299
// from the result, so we do subslicing.
@@ -339,7 +338,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
339338
let (lower, upper) = src.size_hint();
340339
let len = upper.expect("can only write bounded iterators");
341340
assert_eq!(lower, len, "can only write iterators with a precise length");
342-
let bytes = self.get_bytes_mut(cx, ptr, Size::from_bytes(u64::try_from(len).unwrap()))?;
341+
let bytes = self.get_bytes_mut(cx, ptr, Size::from_bytes(len))?;
343342
// `zip` would stop when the first iterator ends; we want to definitely
344343
// cover all of `bytes`.
345344
for dest in bytes {
@@ -382,11 +381,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
382381
} else {
383382
match self.relocations.get(&ptr.offset) {
384383
Some(&(tag, alloc_id)) => {
385-
let ptr = Pointer::new_with_tag(
386-
alloc_id,
387-
Size::from_bytes(u64::try_from(bits).unwrap()),
388-
tag,
389-
);
384+
let ptr = Pointer::new_with_tag(alloc_id, Size::from_bytes(bits), tag);
390385
return Ok(ScalarMaybeUndef::Scalar(ptr.into()));
391386
}
392387
None => {}

src/librustc/mir/interpret/value.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,8 +686,8 @@ pub fn get_slice_bytes<'tcx>(cx: &impl HasDataLayout, val: ConstValue<'tcx>) ->
686686
data.get_bytes(
687687
cx,
688688
// invent a pointer, only the offset is relevant anyway
689-
Pointer::new(AllocId(0), Size::from_bytes(u64::try_from(start).unwrap())),
690-
Size::from_bytes(u64::try_from(len).unwrap()),
689+
Pointer::new(AllocId(0), Size::from_bytes(start)),
690+
Size::from_bytes(len),
691691
)
692692
.unwrap_or_else(|err| bug!("const slice is invalid: {:?}", err))
693693
} else {

src/librustc/ty/print/pretty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ pub trait PrettyPrinter<'tcx>:
981981
.alloc_map
982982
.lock()
983983
.unwrap_memory(ptr.alloc_id)
984-
.get_bytes(&self.tcx(), ptr, Size::from_bytes(*data as u64))
984+
.get_bytes(&self.tcx(), ptr, Size::from_bytes(*data))
985985
.unwrap();
986986
p!(pretty_print_byte_str(byte_str));
987987
}
@@ -1169,7 +1169,7 @@ pub trait PrettyPrinter<'tcx>:
11691169
(ConstValue::ByRef { alloc, offset }, ty::Array(t, n)) if *t == u8_type => {
11701170
let n = n.val.try_to_bits(self.tcx().data_layout.pointer_size).unwrap();
11711171
// cast is ok because we already checked for pointer size (32 or 64 bit) above
1172-
let n = Size::from_bytes(n as u64);
1172+
let n = Size::from_bytes(n);
11731173
let ptr = Pointer::new(AllocId(0), offset);
11741174

11751175
let byte_str = alloc.get_bytes(&self.tcx(), ptr, n).unwrap();

src/librustc_codegen_ssa/mir/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
9191
};
9292
let a = Scalar::from(Pointer::new(
9393
bx.tcx().alloc_map.lock().create_memory_alloc(data),
94-
Size::from_bytes(start as u64),
94+
Size::from_bytes(start),
9595
));
9696
let a_llval = bx.scalar_to_backend(
9797
a,

src/librustc_mir/interpret/memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
836836
src: impl IntoIterator<Item = u8>,
837837
) -> InterpResult<'tcx> {
838838
let src = src.into_iter();
839-
let size = Size::from_bytes(src.size_hint().0 as u64);
839+
let size = Size::from_bytes(src.size_hint().0);
840840
// `write_bytes` checks that this lower bound `size` matches the upper bound and reality.
841841
let ptr = match self.check_ptr_access(ptr, size, Align::from_bytes(1).unwrap())? {
842842
Some(ptr) => ptr,

src/librustc_mir/interpret/operand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Functions concerning immediate values and operands, and reading from operands.
22
//! All high-level functions to read from memory work on operands as sources.
33
4-
use std::convert::{TryFrom, TryInto};
4+
use std::convert::TryFrom;
55

66
use super::{InterpCx, MPlaceTy, Machine, MemPlace, Place, PlaceTy};
77
pub use rustc::mir::interpret::ScalarMaybeUndef;
@@ -570,7 +570,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
570570
// where none should happen.
571571
let ptr = Pointer::new(
572572
self.tcx.alloc_map.lock().create_memory_alloc(data),
573-
Size::from_bytes(start.try_into().unwrap()), // offset: `start`
573+
Size::from_bytes(start), // offset: `start`
574574
);
575575
Operand::Immediate(Immediate::new_slice(
576576
self.tag_global_base_pointer(ptr).into(),

src/librustc_mir/interpret/place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ where
739739
),
740740
Immediate::Scalar(ScalarMaybeUndef::Scalar(Scalar::Raw { size, .. })) => {
741741
assert_eq!(
742-
Size::from_bytes(size.into()),
742+
Size::from_bytes(size),
743743
dest.layout.size,
744744
"Size mismatch when writing bits"
745745
)

src/librustc_mir_build/hair/pattern/_match.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,8 +1920,8 @@ fn slice_pat_covered_by_const<'tcx>(
19201920
}
19211921
(ConstValue::Slice { data, start, end }, ty::Slice(t)) => {
19221922
assert_eq!(*t, tcx.types.u8);
1923-
let ptr = Pointer::new(AllocId(0), Size::from_bytes(start as u64));
1924-
data.get_bytes(&tcx, ptr, Size::from_bytes((end - start) as u64)).unwrap()
1923+
let ptr = Pointer::new(AllocId(0), Size::from_bytes(start));
1924+
data.get_bytes(&tcx, ptr, Size::from_bytes(end - start)).unwrap()
19251925
}
19261926
// FIXME(oli-obk): create a way to extract fat pointers from ByRef
19271927
(_, ty::Slice(_)) => return Ok(false),
@@ -2375,7 +2375,7 @@ fn specialize_one_pattern<'p, 'tcx>(
23752375
ty::Slice(t) => {
23762376
match value.val {
23772377
ty::ConstKind::Value(ConstValue::Slice { data, start, end }) => {
2378-
let offset = Size::from_bytes(start as u64);
2378+
let offset = Size::from_bytes(start);
23792379
let n = (end - start) as u64;
23802380
(Cow::Borrowed(data), offset, n, t)
23812381
}

src/librustc_target/abi/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub use Primitive::*;
33

44
use crate::spec::Target;
55

6-
use std::convert::TryFrom;
6+
use std::convert::{TryFrom, TryInto};
77
use std::ops::{Add, AddAssign, Deref, Mul, Range, RangeInclusive, Sub};
88

99
use rustc_index::vec::{Idx, IndexVec};
@@ -241,17 +241,18 @@ pub struct Size {
241241
}
242242

243243
impl Size {
244-
pub const ZERO: Size = Self::from_bytes(0);
244+
pub const ZERO: Size = Size { raw: 0 };
245245

246246
#[inline]
247-
pub fn from_bits(bits: u64) -> Size {
247+
pub fn from_bits(bits: impl TryInto<u64>) -> Size {
248+
let bits = bits.try_into().ok().unwrap();
248249
// Avoid potential overflow from `bits + 7`.
249250
Size::from_bytes(bits / 8 + ((bits % 8) + 7) / 8)
250251
}
251252

252253
#[inline]
253-
pub const fn from_bytes(bytes: u64) -> Size {
254-
Size { raw: bytes }
254+
pub fn from_bytes(bytes: impl TryInto<u64>) -> Size {
255+
Size { raw: bytes.try_into().ok().unwrap() }
255256
}
256257

257258
#[inline]

0 commit comments

Comments
 (0)