Skip to content

Commit 91377bd

Browse files
authored
Rollup merge of #139404 - yotamofek:pr/smir/cleanup, r=compiler-errors
Small smir cleanup First commit might have small positive perf effect, second one is just to make code a bit shorter
2 parents 2769522 + 33d62dd commit 91377bd

File tree

1 file changed

+12
-26
lines changed
  • compiler/rustc_smir/src/rustc_smir

1 file changed

+12
-26
lines changed

compiler/rustc_smir/src/rustc_smir/alloc.rs

+12-26
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(crate) fn new_allocation<'tcx>(
2828
tables: &mut Tables<'tcx>,
2929
) -> Allocation {
3030
try_new_allocation(ty, const_value, tables)
31-
.expect(&format!("Failed to convert: {const_value:?} to {ty:?}"))
31+
.unwrap_or_else(|_| panic!("Failed to convert: {const_value:?} to {ty:?}"))
3232
}
3333

3434
#[allow(rustc::usage_of_qualified_ty)]
@@ -37,39 +37,30 @@ pub(crate) fn try_new_allocation<'tcx>(
3737
const_value: ConstValue<'tcx>,
3838
tables: &mut Tables<'tcx>,
3939
) -> Result<Allocation, Error> {
40+
let layout = tables
41+
.tcx
42+
.layout_of(rustc_middle::ty::TypingEnv::fully_monomorphized().as_query_input(ty))
43+
.map_err(|e| e.stable(tables))?;
4044
Ok(match const_value {
4145
ConstValue::Scalar(scalar) => {
4246
let size = scalar.size();
43-
let align = tables
44-
.tcx
45-
.layout_of(rustc_middle::ty::TypingEnv::fully_monomorphized().as_query_input(ty))
46-
.map_err(|e| e.stable(tables))?
47-
.align;
48-
let mut allocation =
49-
rustc_middle::mir::interpret::Allocation::new(size, align.abi, AllocInit::Uninit);
47+
let mut allocation = rustc_middle::mir::interpret::Allocation::new(
48+
size,
49+
layout.align.abi,
50+
AllocInit::Uninit,
51+
);
5052
allocation
5153
.write_scalar(&tables.tcx, alloc_range(Size::ZERO, size), scalar)
5254
.map_err(|e| e.stable(tables))?;
5355
allocation.stable(tables)
5456
}
55-
ConstValue::ZeroSized => {
56-
let align = tables
57-
.tcx
58-
.layout_of(rustc_middle::ty::TypingEnv::fully_monomorphized().as_query_input(ty))
59-
.map_err(|e| e.stable(tables))?
60-
.align;
61-
new_empty_allocation(align.abi)
62-
}
57+
ConstValue::ZeroSized => new_empty_allocation(layout.align.abi),
6358
ConstValue::Slice { data, meta } => {
6459
let alloc_id = tables.tcx.reserve_and_set_memory_alloc(data);
6560
let ptr = Pointer::new(alloc_id.into(), Size::ZERO);
6661
let scalar_ptr = rustc_middle::mir::interpret::Scalar::from_pointer(ptr, &tables.tcx);
6762
let scalar_meta =
6863
rustc_middle::mir::interpret::Scalar::from_target_usize(meta, &tables.tcx);
69-
let layout = tables
70-
.tcx
71-
.layout_of(rustc_middle::ty::TypingEnv::fully_monomorphized().as_query_input(ty))
72-
.map_err(|e| e.stable(tables))?;
7364
let mut allocation = rustc_middle::mir::interpret::Allocation::new(
7465
layout.size,
7566
layout.align.abi,
@@ -93,12 +84,7 @@ pub(crate) fn try_new_allocation<'tcx>(
9384
}
9485
ConstValue::Indirect { alloc_id, offset } => {
9586
let alloc = tables.tcx.global_alloc(alloc_id).unwrap_memory();
96-
let ty_size = tables
97-
.tcx
98-
.layout_of(rustc_middle::ty::TypingEnv::fully_monomorphized().as_query_input(ty))
99-
.map_err(|e| e.stable(tables))?
100-
.size;
101-
allocation_filter(&alloc.0, alloc_range(offset, ty_size), tables)
87+
allocation_filter(&alloc.0, alloc_range(offset, layout.size), tables)
10288
}
10389
})
10490
}

0 commit comments

Comments
 (0)