Skip to content

Commit af1ac55

Browse files
authored
Rollup merge of #85408 - RalfJung:alloc-size, r=oli-obk
remove size field from Allocation This is a part of #85376 that can be easily split out. r? ``@oli-obk``
2 parents fbb0d70 + 5d71c67 commit af1ac55

23 files changed

+62
-63
lines changed

compiler/rustc_middle/src/mir/interpret/allocation.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ pub struct Allocation<Tag = (), Extra = ()> {
2727
relocations: Relocations<Tag>,
2828
/// Denotes which part of this allocation is initialized.
2929
init_mask: InitMask,
30-
/// The size of the allocation. Currently, must always equal `bytes.len()`.
31-
pub size: Size,
3230
/// The alignment of the allocation to detect unaligned reads.
3331
/// (`Align` guarantees that this is a power of two.)
3432
pub align: Align,
@@ -94,7 +92,6 @@ impl<Tag> Allocation<Tag> {
9492
bytes,
9593
relocations: Relocations::new(),
9694
init_mask: InitMask::new(size, true),
97-
size,
9895
align,
9996
mutability: Mutability::Not,
10097
extra: (),
@@ -110,7 +107,6 @@ impl<Tag> Allocation<Tag> {
110107
bytes: vec![0; size.bytes_usize()],
111108
relocations: Relocations::new(),
112109
init_mask: InitMask::new(size, false),
113-
size,
114110
align,
115111
mutability: Mutability::Mut,
116112
extra: (),
@@ -127,7 +123,6 @@ impl Allocation<(), ()> {
127123
) -> Allocation<T, E> {
128124
Allocation {
129125
bytes: self.bytes,
130-
size: self.size,
131126
relocations: Relocations::from_presorted(
132127
self.relocations
133128
.iter()
@@ -150,7 +145,11 @@ impl Allocation<(), ()> {
150145
/// Raw accessors. Provide access to otherwise private bytes.
151146
impl<Tag, Extra> Allocation<Tag, Extra> {
152147
pub fn len(&self) -> usize {
153-
self.size.bytes_usize()
148+
self.bytes.len()
149+
}
150+
151+
pub fn size(&self) -> Size {
152+
Size::from_bytes(self.len())
154153
}
155154

156155
/// Looks at a slice which may describe uninitialized bytes or describe a relocation. This differs

compiler/rustc_mir/src/interpret/memory.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
244244
let new_ptr = self.allocate(new_size, new_align, kind);
245245
let old_size = match old_size_and_align {
246246
Some((size, _align)) => size,
247-
None => self.get_raw(ptr.alloc_id)?.size,
247+
None => self.get_raw(ptr.alloc_id)?.size(),
248248
};
249249
self.copy(ptr, new_ptr, old_size.min(new_size), /*nonoverlapping*/ true)?;
250250
self.deallocate(ptr, old_size_and_align, kind)?;
@@ -306,11 +306,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
306306
);
307307
}
308308
if let Some((size, align)) = old_size_and_align {
309-
if size != alloc.size || align != alloc.align {
309+
if size != alloc.size() || align != alloc.align {
310310
throw_ub_format!(
311311
"incorrect layout on deallocation: {} has size {} and alignment {}, but gave size {} and alignment {}",
312312
ptr.alloc_id,
313-
alloc.size.bytes(),
313+
alloc.size().bytes(),
314314
alloc.align.bytes(),
315315
size.bytes(),
316316
align.bytes(),
@@ -319,11 +319,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
319319
}
320320

321321
// Let the machine take some extra action
322-
let size = alloc.size;
322+
let size = alloc.size();
323323
AllocationExtra::memory_deallocated(&mut alloc, ptr, size)?;
324324

325325
// Don't forget to remember size and align of this now-dead allocation
326-
let old = self.dead_alloc_map.insert(ptr.alloc_id, (alloc.size, alloc.align));
326+
let old = self.dead_alloc_map.insert(ptr.alloc_id, (alloc.size(), alloc.align));
327327
if old.is_some() {
328328
bug!("Nothing can be deallocated twice");
329329
}
@@ -586,7 +586,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
586586
// a) cause cycles in case `id` refers to a static
587587
// b) duplicate a global's allocation in miri
588588
if let Some((_, alloc)) = self.alloc_map.get(id) {
589-
return Ok((alloc.size, alloc.align));
589+
return Ok((alloc.size(), alloc.align));
590590
}
591591

592592
// # Function pointers
@@ -614,7 +614,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
614614
Some(GlobalAlloc::Memory(alloc)) => {
615615
// Need to duplicate the logic here, because the global allocations have
616616
// different associated types than the interpreter-local ones.
617-
Ok((alloc.size, alloc.align))
617+
Ok((alloc.size(), alloc.align))
618618
}
619619
Some(GlobalAlloc::Function(_)) => bug!("We already checked function pointers above"),
620620
// The rest must be dead.

compiler/rustc_mir/src/util/pretty.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -776,8 +776,8 @@ pub struct RenderAllocation<'a, 'tcx, Tag, Extra> {
776776
impl<Tag: Copy + Debug, Extra> std::fmt::Display for RenderAllocation<'a, 'tcx, Tag, Extra> {
777777
fn fmt(&self, w: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
778778
let RenderAllocation { tcx, alloc } = *self;
779-
write!(w, "size: {}, align: {})", alloc.size.bytes(), alloc.align.bytes())?;
780-
if alloc.size == Size::ZERO {
779+
write!(w, "size: {}, align: {})", alloc.size().bytes(), alloc.align.bytes())?;
780+
if alloc.size() == Size::ZERO {
781781
// We are done.
782782
return write!(w, " {{}}");
783783
}
@@ -822,9 +822,9 @@ fn write_allocation_bytes<Tag: Copy + Debug, Extra>(
822822
w: &mut dyn std::fmt::Write,
823823
prefix: &str,
824824
) -> std::fmt::Result {
825-
let num_lines = alloc.size.bytes_usize().saturating_sub(BYTES_PER_LINE);
825+
let num_lines = alloc.size().bytes_usize().saturating_sub(BYTES_PER_LINE);
826826
// Number of chars needed to represent all line numbers.
827-
let pos_width = format!("{:x}", alloc.size.bytes()).len();
827+
let pos_width = format!("{:x}", alloc.size().bytes()).len();
828828

829829
if num_lines > 0 {
830830
write!(w, "{}0x{:02$x} │ ", prefix, 0, pos_width)?;
@@ -845,7 +845,7 @@ fn write_allocation_bytes<Tag: Copy + Debug, Extra>(
845845
}
846846
};
847847

848-
while i < alloc.size {
848+
while i < alloc.size() {
849849
// The line start already has a space. While we could remove that space from the line start
850850
// printing and unconditionally print a space here, that would cause the single-line case
851851
// to have a single space before it, which looks weird.
@@ -929,7 +929,7 @@ fn write_allocation_bytes<Tag: Copy + Debug, Extra>(
929929
i += Size::from_bytes(1);
930930
}
931931
// Print a new line header if the next line still has some bytes to print.
932-
if i == line_start + Size::from_bytes(BYTES_PER_LINE) && i != alloc.size {
932+
if i == line_start + Size::from_bytes(BYTES_PER_LINE) && i != alloc.size() {
933933
line_start = write_allocation_newline(w, line_start, &ascii, pos_width, prefix)?;
934934
ascii.clear();
935935
}

src/test/mir-opt/const_debuginfo.main.ConstDebugInfo.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@
7777
_9 = const "hello, world!"; // scope 4 at $DIR/const_debuginfo.rs:14:13: 14:28
7878
// ty::Const
7979
// + ty: &str
80-
// + val: Value(Slice { data: Allocation { bytes: [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [8191], len: Size { raw: 13 } }, size: Size { raw: 13 }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 13 })
80+
// + val: Value(Slice { data: Allocation { bytes: [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [8191], len: Size { raw: 13 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 13 })
8181
// mir::Constant
8282
// + span: $DIR/const_debuginfo.rs:14:13: 14:28
83-
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [8191], len: Size { raw: 13 } }, size: Size { raw: 13 }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 13 }) }
83+
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [8191], len: Size { raw: 13 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 13 }) }
8484
StorageLive(_10); // scope 5 at $DIR/const_debuginfo.rs:16:9: 16:10
8585
(_10.0: bool) = const true; // scope 5 at $DIR/const_debuginfo.rs:16:13: 16:34
8686
(_10.1: bool) = const false; // scope 5 at $DIR/const_debuginfo.rs:16:13: 16:34

src/test/mir-opt/const_prop/checked_add.main.ConstProp.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
+ _2 = const (2_u32, false); // scope 0 at $DIR/checked_add.rs:5:18: 5:23
1717
+ // mir::Constant
1818
+ // + span: $DIR/checked_add.rs:5:18: 5:23
19-
+ // + literal: Const { ty: (u32, bool), val: Value(ByRef { alloc: Allocation { bytes: [2, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [31], len: Size { raw: 8 } }, size: Size { raw: 8 }, align: Align { pow2: 2 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
19+
+ // + literal: Const { ty: (u32, bool), val: Value(ByRef { alloc: Allocation { bytes: [2, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [31], len: Size { raw: 8 } }, align: Align { pow2: 2 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
2020
+ assert(!const false, "attempt to compute `{} + {}`, which would overflow", const 1_u32, const 1_u32) -> bb1; // scope 0 at $DIR/checked_add.rs:5:18: 5:23
2121
}
2222

src/test/mir-opt/const_prop/control_flow_simplification.hello.ConstProp.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
// + literal: Const { ty: fn(&str) -> ! {std::rt::begin_panic::<&str>}, val: Value(Scalar(<ZST>)) }
2323
// ty::Const
2424
// + ty: &str
25-
// + val: Value(Slice { data: Allocation { bytes: [101, 120, 112, 108, 105, 99, 105, 116, 32, 112, 97, 110, 105, 99], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [16383], len: Size { raw: 14 } }, size: Size { raw: 14 }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 14 })
25+
// + val: Value(Slice { data: Allocation { bytes: [101, 120, 112, 108, 105, 99, 105, 116, 32, 112, 97, 110, 105, 99], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [16383], len: Size { raw: 14 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 14 })
2626
// mir::Constant
2727
// + span: $SRC_DIR/std/src/panic.rs:LL:COL
28-
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [101, 120, 112, 108, 105, 99, 105, 116, 32, 112, 97, 110, 105, 99], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [16383], len: Size { raw: 14 } }, size: Size { raw: 14 }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 14 }) }
28+
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [101, 120, 112, 108, 105, 99, 105, 116, 32, 112, 97, 110, 105, 99], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [16383], len: Size { raw: 14 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 14 }) }
2929
}
3030

3131
bb2: {

src/test/mir-opt/const_prop/indirect.main.ConstProp.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
+ _3 = const (3_u8, false); // scope 0 at $DIR/indirect.rs:5:13: 5:29
2121
+ // mir::Constant
2222
+ // + span: $DIR/indirect.rs:5:13: 5:29
23-
+ // + literal: Const { ty: (u8, bool), val: Value(ByRef { alloc: Allocation { bytes: [3, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [3], len: Size { raw: 2 } }, size: Size { raw: 2 }, align: Align { pow2: 0 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
23+
+ // + literal: Const { ty: (u8, bool), val: Value(ByRef { alloc: Allocation { bytes: [3, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [3], len: Size { raw: 2 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
2424
+ assert(!const false, "attempt to compute `{} + {}`, which would overflow", const 2_u8, const 1_u8) -> bb1; // scope 0 at $DIR/indirect.rs:5:13: 5:29
2525
}
2626

src/test/mir-opt/const_prop/issue_67019.main.ConstProp.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
+ (_2.0: (u8, u8)) = const (1_u8, 2_u8); // scope 0 at $DIR/issue-67019.rs:11:10: 11:19
1818
+ // mir::Constant
1919
+ // + span: $DIR/issue-67019.rs:11:10: 11:19
20-
+ // + literal: Const { ty: (u8, u8), val: Value(ByRef { alloc: Allocation { bytes: [1, 2], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [3], len: Size { raw: 2 } }, size: Size { raw: 2 }, align: Align { pow2: 0 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
20+
+ // + literal: Const { ty: (u8, u8), val: Value(ByRef { alloc: Allocation { bytes: [1, 2], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [3], len: Size { raw: 2 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
2121
StorageDead(_3); // scope 0 at $DIR/issue-67019.rs:11:18: 11:19
2222
_1 = test(move _2) -> bb1; // scope 0 at $DIR/issue-67019.rs:11:5: 11:20
2323
// mir::Constant

src/test/mir-opt/const_prop/mutable_variable_aggregate.main.ConstProp.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
+ _2 = const (42_i32, 99_i32); // scope 1 at $DIR/mutable_variable_aggregate.rs:7:13: 7:14
2323
+ // mir::Constant
2424
+ // + span: $DIR/mutable_variable_aggregate.rs:7:13: 7:14
25-
+ // + literal: Const { ty: (i32, i32), val: Value(ByRef { alloc: Allocation { bytes: [42, 0, 0, 0, 99, 0, 0, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [255], len: Size { raw: 8 } }, size: Size { raw: 8 }, align: Align { pow2: 2 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
25+
+ // + literal: Const { ty: (i32, i32), val: Value(ByRef { alloc: Allocation { bytes: [42, 0, 0, 0, 99, 0, 0, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [255], len: Size { raw: 8 } }, align: Align { pow2: 2 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
2626
nop; // scope 0 at $DIR/mutable_variable_aggregate.rs:4:11: 8:2
2727
StorageDead(_2); // scope 1 at $DIR/mutable_variable_aggregate.rs:8:1: 8:2
2828
StorageDead(_1); // scope 0 at $DIR/mutable_variable_aggregate.rs:8:1: 8:2

src/test/mir-opt/const_prop/optimizes_into_variable.main.ConstProp.32bit.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
+ _2 = const (4_i32, false); // scope 0 at $DIR/optimizes_into_variable.rs:12:13: 12:18
3030
+ // mir::Constant
3131
+ // + span: $DIR/optimizes_into_variable.rs:12:13: 12:18
32-
+ // + literal: Const { ty: (i32, bool), val: Value(ByRef { alloc: Allocation { bytes: [4, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [31], len: Size { raw: 8 } }, size: Size { raw: 8 }, align: Align { pow2: 2 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
32+
+ // + literal: Const { ty: (i32, bool), val: Value(ByRef { alloc: Allocation { bytes: [4, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [31], len: Size { raw: 8 } }, align: Align { pow2: 2 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
3333
+ assert(!const false, "attempt to compute `{} + {}`, which would overflow", const 2_i32, const 2_i32) -> bb1; // scope 0 at $DIR/optimizes_into_variable.rs:12:13: 12:18
3434
}
3535

src/test/mir-opt/const_prop/optimizes_into_variable.main.ConstProp.64bit.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
+ _2 = const (4_i32, false); // scope 0 at $DIR/optimizes_into_variable.rs:12:13: 12:18
3030
+ // mir::Constant
3131
+ // + span: $DIR/optimizes_into_variable.rs:12:13: 12:18
32-
+ // + literal: Const { ty: (i32, bool), val: Value(ByRef { alloc: Allocation { bytes: [4, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [31], len: Size { raw: 8 } }, size: Size { raw: 8 }, align: Align { pow2: 2 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
32+
+ // + literal: Const { ty: (i32, bool), val: Value(ByRef { alloc: Allocation { bytes: [4, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [31], len: Size { raw: 8 } }, align: Align { pow2: 2 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
3333
+ assert(!const false, "attempt to compute `{} + {}`, which would overflow", const 2_i32, const 2_i32) -> bb1; // scope 0 at $DIR/optimizes_into_variable.rs:12:13: 12:18
3434
}
3535

src/test/mir-opt/const_prop/return_place.add.ConstProp.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
+ _1 = const (4_u32, false); // scope 0 at $DIR/return_place.rs:6:5: 6:10
1212
+ // mir::Constant
1313
+ // + span: $DIR/return_place.rs:6:5: 6:10
14-
+ // + literal: Const { ty: (u32, bool), val: Value(ByRef { alloc: Allocation { bytes: [4, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [31], len: Size { raw: 8 } }, size: Size { raw: 8 }, align: Align { pow2: 2 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
14+
+ // + literal: Const { ty: (u32, bool), val: Value(ByRef { alloc: Allocation { bytes: [4, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [31], len: Size { raw: 8 } }, align: Align { pow2: 2 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
1515
+ assert(!const false, "attempt to compute `{} + {}`, which would overflow", const 2_u32, const 2_u32) -> bb1; // scope 0 at $DIR/return_place.rs:6:5: 6:10
1616
}
1717

src/test/mir-opt/const_prop/tuple_literal_propagation.main.ConstProp.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
+ _3 = const (1_u32, 2_u32); // scope 1 at $DIR/tuple_literal_propagation.rs:5:13: 5:14
2121
+ // mir::Constant
2222
+ // + span: $DIR/tuple_literal_propagation.rs:5:13: 5:14
23-
+ // + literal: Const { ty: (u32, u32), val: Value(ByRef { alloc: Allocation { bytes: [1, 0, 0, 0, 2, 0, 0, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [255], len: Size { raw: 8 } }, size: Size { raw: 8 }, align: Align { pow2: 2 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
23+
+ // + literal: Const { ty: (u32, u32), val: Value(ByRef { alloc: Allocation { bytes: [1, 0, 0, 0, 2, 0, 0, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [255], len: Size { raw: 8 } }, align: Align { pow2: 2 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
2424
_2 = consume(move _3) -> bb1; // scope 1 at $DIR/tuple_literal_propagation.rs:5:5: 5:15
2525
// mir::Constant
2626
// + span: $DIR/tuple_literal_propagation.rs:5:5: 5:12

0 commit comments

Comments
 (0)