Skip to content

Commit aded02c

Browse files
committed
address review comments; fix CI
1 parent 57e6fd9 commit aded02c

9 files changed

+77
-74
lines changed

library/alloc/src/alloc.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,8 @@ impl Global {
374374
let raw_ptr = core::intrinsics::const_allocate(layout.size(), layout.align());
375375
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
376376
if zeroed {
377-
let mut offset = 0;
378-
while offset < size {
379-
offset += 1;
380-
// SAFETY: the pointer returned by `const_allocate` is valid to write to.
381-
ptr.add(offset).write(0)
382-
}
377+
// SAFETY: the pointer returned by `const_allocate` is valid to write to.
378+
ptr.write_bytes(0, size);
383379
}
384380
Ok(NonNull::slice_from_raw_parts(ptr, size))
385381
},

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
#![feature(fmt_internals)]
123123
#![feature(fn_traits)]
124124
#![feature(formatting_options)]
125+
#![feature(freeze)]
125126
#![feature(generic_atomic)]
126127
#![feature(hasher_prefixfree_extras)]
127128
#![feature(inplace_iteration)]

library/alloc/src/vec/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ use core::hash::{Hash, Hasher};
8181
use core::iter;
8282
#[cfg(not(no_global_oom_handling))]
8383
use core::marker::Destruct;
84-
use core::marker::PhantomData;
84+
use core::marker::{Freeze, PhantomData};
8585
use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
8686
use core::ops::{self, Index, IndexMut, Range, RangeBounds};
8787
use core::ptr::{self, NonNull};
@@ -848,11 +848,17 @@ impl<T> Vec<T> {
848848
(unsafe { NonNull::new_unchecked(ptr) }, len, capacity)
849849
}
850850

851-
/// Leaks the `Vec<T>` to be interned statically. This mut be done for all
852-
/// `Vec<T>` created during compile time.
851+
/// Interns the `Vec<T>`, making the underlying memory read-only. This method should be
852+
/// called during compile time. (This is a no-op if called during runtime)
853+
///
854+
/// This method must be called if the memory used by `Vec` needs to appear in the final
855+
/// values of constants.
853856
#[unstable(feature = "const_heap", issue = "79597")]
854857
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
855-
pub const fn const_leak(mut self) -> &'static [T] {
858+
pub const fn const_make_global(mut self) -> &'static [T]
859+
where
860+
T: Freeze,
861+
{
856862
unsafe { core::intrinsics::const_make_global(self.as_mut_ptr().cast()) };
857863
let me = ManuallyDrop::new(self);
858864
unsafe { slice::from_raw_parts(me.as_ptr(), me.len) }

library/alloctests/tests/vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2731,7 +2731,7 @@ fn const_heap() {
27312731
x *= 2;
27322732
}
27332733
assert!(v.len() == 6);
2734-
v.const_leak()
2734+
v.const_make_global()
27352735
};
27362736

27372737
assert_eq!([1, 2, 4, 8, 16, 32], X);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub fn foo2() -> Box<dyn TestTrait2> {
4444
//~ MONO_ITEM fn <TestStruct2 as TestTrait2>::test_func2
4545
//~ MONO_ITEM fn alloc::alloc::exchange_malloc
4646
//~ MONO_ITEM fn foo2
47-
//~ MONO_ITEM fn std::alloc::Global::alloc_impl
47+
//~ MONO_ITEM fn std::alloc::Global::alloc_impl_runtime
4848
//~ MONO_ITEM fn std::boxed::Box::<TestStruct2>::new
4949
//~ MONO_ITEM fn std::alloc::Layout::from_size_align_unchecked::precondition_check
5050
//~ MONO_ITEM fn std::ptr::NonNull::<T>::new_unchecked::precondition_check

tests/mir-opt/pre-codegen/drop_boxed_slice.generic_in_place.PreCodegen.after.32bit.panic-unwind.mir

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@ fn generic_in_place(_1: *mut Box<[T]>) -> () {
2525
}
2626
}
2727
scope 18 (inlined <std::alloc::Global as Allocator>::deallocate) {
28-
let mut _9: *mut u8;
29-
scope 19 (inlined Layout::size) {
30-
}
31-
scope 20 (inlined NonNull::<u8>::as_ptr) {
32-
}
33-
scope 21 (inlined std::alloc::dealloc) {
34-
let mut _10: usize;
35-
scope 22 (inlined Layout::size) {
36-
}
37-
scope 23 (inlined Layout::align) {
38-
scope 24 (inlined std::ptr::Alignment::as_usize) {
28+
scope 19 (inlined std::alloc::Global::deallocate_impl) {
29+
scope 20 (inlined std::alloc::Global::deallocate_impl_runtime) {
30+
let mut _9: *mut u8;
31+
scope 21 (inlined Layout::size) {
32+
}
33+
scope 22 (inlined NonNull::<u8>::as_ptr) {
34+
}
35+
scope 23 (inlined std::alloc::dealloc) {
36+
let mut _10: usize;
37+
scope 24 (inlined Layout::size) {
38+
}
39+
scope 25 (inlined Layout::align) {
40+
scope 26 (inlined std::ptr::Alignment::as_usize) {
41+
}
42+
}
3943
}
4044
}
4145
}

tests/mir-opt/pre-codegen/drop_boxed_slice.generic_in_place.PreCodegen.after.64bit.panic-unwind.mir

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@ fn generic_in_place(_1: *mut Box<[T]>) -> () {
2525
}
2626
}
2727
scope 18 (inlined <std::alloc::Global as Allocator>::deallocate) {
28-
let mut _9: *mut u8;
29-
scope 19 (inlined Layout::size) {
30-
}
31-
scope 20 (inlined NonNull::<u8>::as_ptr) {
32-
}
33-
scope 21 (inlined std::alloc::dealloc) {
34-
let mut _10: usize;
35-
scope 22 (inlined Layout::size) {
36-
}
37-
scope 23 (inlined Layout::align) {
38-
scope 24 (inlined std::ptr::Alignment::as_usize) {
28+
scope 19 (inlined std::alloc::Global::deallocate_impl) {
29+
scope 20 (inlined std::alloc::Global::deallocate_impl_runtime) {
30+
let mut _9: *mut u8;
31+
scope 21 (inlined Layout::size) {
32+
}
33+
scope 22 (inlined NonNull::<u8>::as_ptr) {
34+
}
35+
scope 23 (inlined std::alloc::dealloc) {
36+
let mut _10: usize;
37+
scope 24 (inlined Layout::size) {
38+
}
39+
scope 25 (inlined Layout::align) {
40+
scope 26 (inlined std::ptr::Alignment::as_usize) {
41+
}
42+
}
3943
}
4044
}
4145
}

tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-unwind.diff

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
let mut _4: *mut [u8];
1010
let mut _5: std::ptr::NonNull<[u8]>;
1111
let mut _6: std::result::Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError>;
12-
let mut _7: &std::alloc::Global;
13-
let mut _8: std::alloc::Layout;
12+
let mut _7: std::alloc::Layout;
1413
scope 1 {
1514
debug layout => _1;
16-
let mut _9: &std::alloc::Global;
1715
scope 2 {
1816
debug ptr => _3;
1917
}
2018
scope 5 (inlined <std::alloc::Global as Allocator>::allocate) {
19+
scope 6 (inlined std::alloc::Global::alloc_impl) {
20+
}
2121
}
22-
scope 6 (inlined NonNull::<[u8]>::as_ptr) {
22+
scope 7 (inlined NonNull::<[u8]>::as_ptr) {
2323
}
2424
}
2525
scope 3 (inlined #[track_caller] Option::<Layout>::unwrap) {
26-
let mut _10: isize;
27-
let mut _11: !;
26+
let mut _8: isize;
27+
let mut _9: !;
2828
scope 4 {
2929
}
3030
}
@@ -35,10 +35,10 @@
3535
StorageLive(_2);
3636
- _2 = Option::<Layout>::None;
3737
+ _2 = const Option::<Layout>::None;
38-
StorageLive(_10);
39-
- _10 = discriminant(_2);
40-
- switchInt(move _10) -> [0: bb3, 1: bb4, otherwise: bb2];
41-
+ _10 = const 0_isize;
38+
StorageLive(_8);
39+
- _8 = discriminant(_2);
40+
- switchInt(move _8) -> [0: bb3, 1: bb4, otherwise: bb2];
41+
+ _8 = const 0_isize;
4242
+ switchInt(const 0_isize) -> [0: bb3, 1: bb4, otherwise: bb2];
4343
}
4444

@@ -59,30 +59,26 @@
5959
}
6060

6161
bb3: {
62-
_11 = option::unwrap_failed() -> unwind continue;
62+
_9 = option::unwrap_failed() -> unwind continue;
6363
}
6464

6565
bb4: {
6666
- _1 = move ((_2 as Some).0: std::alloc::Layout);
6767
+ _1 = const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(4 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x00000000): std::ptr::alignment::AlignmentEnum) }};
68-
StorageDead(_10);
68+
StorageDead(_8);
6969
StorageDead(_2);
7070
StorageLive(_3);
7171
StorageLive(_4);
7272
StorageLive(_5);
7373
StorageLive(_6);
7474
StorageLive(_7);
75-
_9 = const main::promoted[0];
76-
_7 = copy _9;
77-
StorageLive(_8);
78-
- _8 = copy _1;
79-
- _6 = std::alloc::Global::alloc_impl(move _7, move _8, const false) -> [return: bb5, unwind continue];
80-
+ _8 = const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(4 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x00000000): std::ptr::alignment::AlignmentEnum) }};
81-
+ _6 = std::alloc::Global::alloc_impl(copy _9, const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(4 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x00000000): std::ptr::alignment::AlignmentEnum) }}, const false) -> [return: bb5, unwind continue];
75+
- _7 = copy _1;
76+
- _6 = std::alloc::Global::alloc_impl_runtime(move _7, const false) -> [return: bb5, unwind continue];
77+
+ _7 = const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(4 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x00000000): std::ptr::alignment::AlignmentEnum) }};
78+
+ _6 = std::alloc::Global::alloc_impl_runtime(const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(4 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x00000000): std::ptr::alignment::AlignmentEnum) }}, const false) -> [return: bb5, unwind continue];
8279
}
8380

8481
bb5: {
85-
StorageDead(_8);
8682
StorageDead(_7);
8783
_5 = Result::<NonNull<[u8]>, std::alloc::AllocError>::unwrap(move _6) -> [return: bb1, unwind continue];
8884
}

tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-unwind.diff

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
let mut _4: *mut [u8];
1010
let mut _5: std::ptr::NonNull<[u8]>;
1111
let mut _6: std::result::Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError>;
12-
let mut _7: &std::alloc::Global;
13-
let mut _8: std::alloc::Layout;
12+
let mut _7: std::alloc::Layout;
1413
scope 1 {
1514
debug layout => _1;
16-
let mut _9: &std::alloc::Global;
1715
scope 2 {
1816
debug ptr => _3;
1917
}
2018
scope 5 (inlined <std::alloc::Global as Allocator>::allocate) {
19+
scope 6 (inlined std::alloc::Global::alloc_impl) {
20+
}
2121
}
22-
scope 6 (inlined NonNull::<[u8]>::as_ptr) {
22+
scope 7 (inlined NonNull::<[u8]>::as_ptr) {
2323
}
2424
}
2525
scope 3 (inlined #[track_caller] Option::<Layout>::unwrap) {
26-
let mut _10: isize;
27-
let mut _11: !;
26+
let mut _8: isize;
27+
let mut _9: !;
2828
scope 4 {
2929
}
3030
}
@@ -35,10 +35,10 @@
3535
StorageLive(_2);
3636
- _2 = Option::<Layout>::None;
3737
+ _2 = const Option::<Layout>::None;
38-
StorageLive(_10);
39-
- _10 = discriminant(_2);
40-
- switchInt(move _10) -> [0: bb3, 1: bb4, otherwise: bb2];
41-
+ _10 = const 0_isize;
38+
StorageLive(_8);
39+
- _8 = discriminant(_2);
40+
- switchInt(move _8) -> [0: bb3, 1: bb4, otherwise: bb2];
41+
+ _8 = const 0_isize;
4242
+ switchInt(const 0_isize) -> [0: bb3, 1: bb4, otherwise: bb2];
4343
}
4444

@@ -59,30 +59,26 @@
5959
}
6060

6161
bb3: {
62-
_11 = option::unwrap_failed() -> unwind continue;
62+
_9 = option::unwrap_failed() -> unwind continue;
6363
}
6464

6565
bb4: {
6666
- _1 = move ((_2 as Some).0: std::alloc::Layout);
6767
+ _1 = const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(8 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x0000000000000000): std::ptr::alignment::AlignmentEnum) }};
68-
StorageDead(_10);
68+
StorageDead(_8);
6969
StorageDead(_2);
7070
StorageLive(_3);
7171
StorageLive(_4);
7272
StorageLive(_5);
7373
StorageLive(_6);
7474
StorageLive(_7);
75-
_9 = const main::promoted[0];
76-
_7 = copy _9;
77-
StorageLive(_8);
78-
- _8 = copy _1;
79-
- _6 = std::alloc::Global::alloc_impl(move _7, move _8, const false) -> [return: bb5, unwind continue];
80-
+ _8 = const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(8 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x0000000000000000): std::ptr::alignment::AlignmentEnum) }};
81-
+ _6 = std::alloc::Global::alloc_impl(copy _9, const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(8 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x0000000000000000): std::ptr::alignment::AlignmentEnum) }}, const false) -> [return: bb5, unwind continue];
75+
- _7 = copy _1;
76+
- _6 = std::alloc::Global::alloc_impl_runtime(move _7, const false) -> [return: bb5, unwind continue];
77+
+ _7 = const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(8 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x0000000000000000): std::ptr::alignment::AlignmentEnum) }};
78+
+ _6 = std::alloc::Global::alloc_impl_runtime(const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(8 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x0000000000000000): std::ptr::alignment::AlignmentEnum) }}, const false) -> [return: bb5, unwind continue];
8279
}
8380

8481
bb5: {
85-
StorageDead(_8);
8682
StorageDead(_7);
8783
_5 = Result::<NonNull<[u8]>, std::alloc::AllocError>::unwrap(move _6) -> [return: bb1, unwind continue];
8884
}

0 commit comments

Comments
 (0)