Skip to content

Commit d8b2222

Browse files
committed
Auto merge of rust-lang#152737 - scottmcm:box_new_uninit_layout, r=RalfJung
Just pass `Layout` directly to `box_new_uninit` *[View all comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust/pull/152737)* We have a constant for it already (used in `RawVec` for basically the same polymorphization) so let's use it. This is a simple follow-up to rust-lang#148190 from one of the comments.
2 parents 5fb2ff8 + 4b03b97 commit d8b2222

File tree

7 files changed

+38
-248
lines changed

7 files changed

+38
-248
lines changed

library/alloc/src/boxed.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,14 @@ pub struct Box<
237237
>(Unique<T>, A);
238238

239239
/// Monomorphic function for allocating an uninit `Box`.
240-
///
241-
/// # Safety
242-
///
243-
/// size and align need to be safe for `Layout::from_size_align_unchecked`.
244240
#[inline]
241+
// The is a separate function to avoid doing it in every generic version, but it
242+
// looks small to the mir inliner (particularly in panic=abort) so leave it to
243+
// the backend to decide whether pulling it in everywhere is worth doing.
244+
#[rustc_no_mir_inline]
245245
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
246246
#[cfg(not(no_global_oom_handling))]
247-
unsafe fn box_new_uninit(size: usize, align: usize) -> *mut u8 {
248-
let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
247+
fn box_new_uninit(layout: Layout) -> *mut u8 {
249248
match Global.allocate(layout) {
250249
Ok(ptr) => ptr.as_mut_ptr(),
251250
Err(_) => handle_alloc_error(layout),
@@ -284,10 +283,7 @@ impl<T> Box<T> {
284283
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
285284
pub fn new(x: T) -> Self {
286285
// This is `Box::new_uninit` but inlined to avoid build time regressions.
287-
// SAFETY: The size and align of a valid type `T` are always valid for `Layout`.
288-
let ptr = unsafe {
289-
box_new_uninit(<T as SizedTypeProperties>::SIZE, <T as SizedTypeProperties>::ALIGN)
290-
} as *mut T;
286+
let ptr = box_new_uninit(<T as SizedTypeProperties>::LAYOUT) as *mut T;
291287
// Nothing below can panic so we do not have to worry about deallocating `ptr`.
292288
// SAFETY: we just allocated the box to store `x`.
293289
unsafe { core::intrinsics::write_via_move(ptr, x) };
@@ -317,14 +313,8 @@ impl<T> Box<T> {
317313
// `Box::new`).
318314

319315
// SAFETY:
320-
// - The size and align of a valid type `T` are always valid for `Layout`.
321316
// - If `allocate` succeeds, the returned pointer exactly matches what `Box` needs.
322-
unsafe {
323-
mem::transmute(box_new_uninit(
324-
<T as SizedTypeProperties>::SIZE,
325-
<T as SizedTypeProperties>::ALIGN,
326-
))
327-
}
317+
unsafe { mem::transmute(box_new_uninit(<T as SizedTypeProperties>::LAYOUT)) }
328318
}
329319

330320
/// Constructs a new `Box` with uninitialized contents, with the memory

tests/codegen-units/item-collection/opaque-return-impls.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ pub fn foo2() -> Box<dyn TestTrait2> {
4545
//~ MONO_ITEM fn foo2
4646
//~ MONO_ITEM fn std::alloc::Global::alloc_impl_runtime
4747
//~ MONO_ITEM fn std::boxed::Box::<TestStruct2>::new
48-
//~ MONO_ITEM fn std::alloc::Layout::from_size_align_unchecked::precondition_check
49-
//~ MONO_ITEM fn std::ptr::Alignment::new_unchecked::precondition_check
48+
//~ MONO_ITEM fn std::boxed::box_new_uninit
5049
//~ MONO_ITEM fn std::ptr::NonNull::<T>::new_unchecked::precondition_check
5150

5251
struct Counter {

tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.32bit.panic-abort.diff

Lines changed: 14 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
let mut _10: std::boxed::Box<()>;
1313
let mut _11: *const ();
1414
let mut _14: usize;
15-
let mut _15: usize;
16-
let mut _24: usize;
1715
scope 1 {
1816
debug vp_ctx => _1;
1917
let _5: *const ();
@@ -26,12 +24,12 @@
2624
scope 4 {
2725
debug _x => _8;
2826
}
29-
scope 20 (inlined foo) {
30-
let mut _25: *const [()];
27+
scope 9 (inlined foo) {
28+
let mut _15: *const [()];
3129
}
3230
}
33-
scope 18 (inlined slice_from_raw_parts::<()>) {
34-
scope 19 (inlined std::ptr::from_raw_parts::<[()], ()>) {
31+
scope 7 (inlined slice_from_raw_parts::<()>) {
32+
scope 8 (inlined std::ptr::from_raw_parts::<[()], ()>) {
3533
}
3634
}
3735
}
@@ -41,36 +39,6 @@
4139
let mut _13: *mut u8;
4240
scope 6 {
4341
}
44-
scope 7 (inlined boxed::box_new_uninit) {
45-
let _16: std::alloc::Layout;
46-
let mut _17: std::result::Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError>;
47-
let mut _18: isize;
48-
let mut _20: !;
49-
scope 8 {
50-
let _19: std::ptr::NonNull<[u8]>;
51-
scope 9 {
52-
scope 13 (inlined NonNull::<[u8]>::as_mut_ptr) {
53-
scope 14 (inlined NonNull::<[u8]>::as_non_null_ptr) {
54-
scope 15 (inlined NonNull::<[u8]>::cast::<u8>) {
55-
let mut _23: *mut [u8];
56-
scope 16 (inlined NonNull::<[u8]>::as_ptr) {
57-
}
58-
}
59-
}
60-
scope 17 (inlined NonNull::<u8>::as_ptr) {
61-
}
62-
}
63-
}
64-
scope 11 (inlined <std::alloc::Global as Allocator>::allocate) {
65-
scope 12 (inlined std::alloc::Global::alloc_impl) {
66-
}
67-
}
68-
}
69-
scope 10 (inlined #[track_caller] Layout::from_size_align_unchecked) {
70-
let _21: ();
71-
let mut _22: std::ptr::Alignment;
72-
}
73-
}
7442
}
7543

7644
bb0: {
@@ -82,18 +50,7 @@
8250
+ _4 = const ();
8351
StorageLive(_12);
8452
StorageLive(_13);
85-
StorageLive(_14);
86-
- _14 = const <() as std::mem::SizedTypeProperties>::SIZE;
87-
+ _14 = const 0_usize;
88-
StorageLive(_15);
89-
- _15 = const <() as std::mem::SizedTypeProperties>::ALIGN;
90-
+ _15 = const 1_usize;
91-
StorageLive(_16);
92-
StorageLive(_18);
93-
StorageLive(_19);
94-
StorageLive(_20);
95-
StorageLive(_21);
96-
switchInt(UbChecks) -> [0: bb6, otherwise: bb5];
53+
_13 = boxed::box_new_uninit(const <() as std::mem::SizedTypeProperties>::LAYOUT) -> [return: bb2, unwind unreachable];
9754
}
9855

9956
bb1: {
@@ -103,33 +60,8 @@
10360
}
10461

10562
bb2: {
106-
unreachable;
107-
}
108-
109-
bb3: {
110-
- _20 = handle_alloc_error(move _16) -> unwind unreachable;
111-
+ _20 = handle_alloc_error(const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }}) -> unwind unreachable;
112-
}
113-
114-
bb4: {
115-
_19 = copy ((_17 as Ok).0: std::ptr::NonNull<[u8]>);
116-
- StorageLive(_23);
117-
+ nop;
118-
_23 = copy _19 as *mut [u8] (Transmute);
119-
_13 = copy _23 as *mut u8 (PtrToPtr);
120-
- StorageDead(_23);
121-
+ nop;
122-
StorageDead(_17);
123-
StorageDead(_21);
124-
StorageDead(_20);
125-
StorageDead(_19);
126-
StorageDead(_18);
127-
StorageDead(_16);
128-
StorageDead(_15);
129-
StorageDead(_14);
130-
- _12 = copy _13 as *mut () (PtrToPtr);
63+
_12 = copy _13 as *mut () (PtrToPtr);
13164
- (*_12) = move _4;
132-
+ _12 = copy _23 as *mut () (PtrToPtr);
13365
+ (*_12) = const ();
13466
_3 = copy _13 as std::boxed::Box<()> (Transmute);
13567
StorageDead(_13);
@@ -147,21 +79,21 @@
14779
+ nop;
14880
StorageLive(_7);
14981
_7 = copy _5;
150-
StorageLive(_24);
151-
_24 = const 1_usize;
152-
- _6 = *const [()] from (copy _7, copy _24);
82+
StorageLive(_14);
83+
_14 = const 1_usize;
84+
- _6 = *const [()] from (copy _7, copy _14);
15385
+ _6 = *const [()] from (copy _5, const 1_usize);
154-
StorageDead(_24);
86+
StorageDead(_14);
15587
StorageDead(_7);
15688
StorageLive(_8);
15789
StorageLive(_9);
15890
_9 = copy _6;
159-
StorageLive(_25);
160-
- _25 = copy _9;
91+
StorageLive(_15);
92+
- _15 = copy _9;
16193
- _8 = copy _9 as *mut () (PtrToPtr);
162-
+ _25 = copy _6;
94+
+ _15 = copy _6;
16395
+ _8 = copy _5 as *mut () (PtrToPtr);
164-
StorageDead(_25);
96+
StorageDead(_15);
16597
StorageDead(_9);
16698
_0 = const ();
16799
StorageDead(_8);
@@ -171,31 +103,5 @@
171103
+ nop;
172104
drop(_3) -> [return: bb1, unwind unreachable];
173105
}
174-
175-
bb5: {
176-
- _21 = Layout::from_size_align_unchecked::precondition_check(copy _14, copy _15) -> [return: bb6, unwind unreachable];
177-
+ _21 = Layout::from_size_align_unchecked::precondition_check(const 0_usize, const 1_usize) -> [return: bb6, unwind unreachable];
178-
}
179-
180-
bb6: {
181-
StorageLive(_22);
182-
- _22 = copy _15 as std::ptr::Alignment (Transmute);
183-
- _16 = Layout { size: copy _14, align: move _22 };
184-
+ _22 = const std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }};
185-
+ _16 = const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }};
186-
StorageDead(_22);
187-
StorageLive(_17);
188-
- _17 = std::alloc::Global::alloc_impl_runtime(copy _16, const false) -> [return: bb7, unwind unreachable];
189-
+ _17 = std::alloc::Global::alloc_impl_runtime(const Layout {{ size: 0_usize, align: std::ptr::Alignment {{ _inner_repr_trick: std::ptr::alignment::AlignmentEnum::_Align1Shl0 }} }}, const false) -> [return: bb7, unwind unreachable];
190-
}
191-
192-
bb7: {
193-
_18 = discriminant(_17);
194-
switchInt(move _18) -> [0: bb4, 1: bb3, otherwise: bb2];
195-
}
196-
+ }
197-
+
198-
+ ALLOC0 (size: 8, align: 4) {
199-
+ 01 00 00 00 00 00 00 00 │ ........
200106
}
201107

0 commit comments

Comments
 (0)