Skip to content

Commit 33d62dd

Browse files
committed
Dedup call to layout query
1 parent 89d9dd6 commit 33d62dd

File tree

1 file changed

+11
-25
lines changed
  • compiler/rustc_smir/src/rustc_smir

1 file changed

+11
-25
lines changed

Diff for: compiler/rustc_smir/src/rustc_smir/alloc.rs

+11-25
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,30 @@ pub(crate) fn try_new_allocation<'tcx>(
3636
const_value: ConstValue<'tcx>,
3737
tables: &mut Tables<'tcx>,
3838
) -> Result<Allocation, Error> {
39+
let layout = tables
40+
.tcx
41+
.layout_of(rustc_middle::ty::TypingEnv::fully_monomorphized().as_query_input(ty))
42+
.map_err(|e| e.stable(tables))?;
3943
Ok(match const_value {
4044
ConstValue::Scalar(scalar) => {
4145
let size = scalar.size();
42-
let align = tables
43-
.tcx
44-
.layout_of(rustc_middle::ty::TypingEnv::fully_monomorphized().as_query_input(ty))
45-
.map_err(|e| e.stable(tables))?
46-
.align;
47-
let mut allocation =
48-
rustc_middle::mir::interpret::Allocation::new(size, align.abi, AllocInit::Uninit);
46+
let mut allocation = rustc_middle::mir::interpret::Allocation::new(
47+
size,
48+
layout.align.abi,
49+
AllocInit::Uninit,
50+
);
4951
allocation
5052
.write_scalar(&tables.tcx, alloc_range(Size::ZERO, size), scalar)
5153
.map_err(|e| e.stable(tables))?;
5254
allocation.stable(tables)
5355
}
54-
ConstValue::ZeroSized => {
55-
let align = tables
56-
.tcx
57-
.layout_of(rustc_middle::ty::TypingEnv::fully_monomorphized().as_query_input(ty))
58-
.map_err(|e| e.stable(tables))?
59-
.align;
60-
new_empty_allocation(align.abi)
61-
}
56+
ConstValue::ZeroSized => new_empty_allocation(layout.align.abi),
6257
ConstValue::Slice { data, meta } => {
6358
let alloc_id = tables.tcx.reserve_and_set_memory_alloc(data);
6459
let ptr = Pointer::new(alloc_id.into(), Size::ZERO);
6560
let scalar_ptr = rustc_middle::mir::interpret::Scalar::from_pointer(ptr, &tables.tcx);
6661
let scalar_meta =
6762
rustc_middle::mir::interpret::Scalar::from_target_usize(meta, &tables.tcx);
68-
let layout = tables
69-
.tcx
70-
.layout_of(rustc_middle::ty::TypingEnv::fully_monomorphized().as_query_input(ty))
71-
.map_err(|e| e.stable(tables))?;
7263
let mut allocation = rustc_middle::mir::interpret::Allocation::new(
7364
layout.size,
7465
layout.align.abi,
@@ -92,12 +83,7 @@ pub(crate) fn try_new_allocation<'tcx>(
9283
}
9384
ConstValue::Indirect { alloc_id, offset } => {
9485
let alloc = tables.tcx.global_alloc(alloc_id).unwrap_memory();
95-
let ty_size = tables
96-
.tcx
97-
.layout_of(rustc_middle::ty::TypingEnv::fully_monomorphized().as_query_input(ty))
98-
.map_err(|e| e.stable(tables))?
99-
.size;
100-
allocation_filter(&alloc.0, alloc_range(offset, ty_size), tables)
86+
allocation_filter(&alloc.0, alloc_range(offset, layout.size), tables)
10187
}
10288
})
10389
}

0 commit comments

Comments
 (0)