Skip to content

Commit 9dc6454

Browse files
committed
Use it in the library, and InstSimplify it away in the easy places
1 parent 4718ee9 commit 9dc6454

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

core/src/intrinsics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2786,6 +2786,7 @@ pub unsafe fn vtable_align(_ptr: *const ()) -> usize {
27862786
/// change the possible layouts of pointers.
27872787
#[rustc_nounwind]
27882788
#[unstable(feature = "core_intrinsics", issue = "none")]
2789+
#[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")]
27892790
#[rustc_intrinsic]
27902791
#[rustc_intrinsic_must_be_overridden]
27912792
#[cfg(not(bootstrap))]

core/src/ptr/metadata.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
use crate::fmt;
44
use crate::hash::{Hash, Hasher};
5+
#[cfg(not(bootstrap))]
6+
use crate::intrinsics::aggregate_raw_ptr;
57
use crate::marker::Freeze;
68

79
/// Provides the pointer metadata type of any pointed-to type.
@@ -113,10 +115,17 @@ pub const fn from_raw_parts<T: ?Sized>(
113115
data_pointer: *const (),
114116
metadata: <T as Pointee>::Metadata,
115117
) -> *const T {
116-
// SAFETY: Accessing the value from the `PtrRepr` union is safe since *const T
117-
// and PtrComponents<T> have the same memory layouts. Only std can make this
118-
// guarantee.
119-
unsafe { PtrRepr { components: PtrComponents { data_pointer, metadata } }.const_ptr }
118+
#[cfg(bootstrap)]
119+
{
120+
// SAFETY: Accessing the value from the `PtrRepr` union is safe since *const T
121+
// and PtrComponents<T> have the same memory layouts. Only std can make this
122+
// guarantee.
123+
unsafe { PtrRepr { components: PtrComponents { data_pointer, metadata } }.const_ptr }
124+
}
125+
#[cfg(not(bootstrap))]
126+
{
127+
aggregate_raw_ptr(data_pointer, metadata)
128+
}
120129
}
121130

122131
/// Performs the same functionality as [`from_raw_parts`], except that a
@@ -130,10 +139,17 @@ pub const fn from_raw_parts_mut<T: ?Sized>(
130139
data_pointer: *mut (),
131140
metadata: <T as Pointee>::Metadata,
132141
) -> *mut T {
133-
// SAFETY: Accessing the value from the `PtrRepr` union is safe since *const T
134-
// and PtrComponents<T> have the same memory layouts. Only std can make this
135-
// guarantee.
136-
unsafe { PtrRepr { components: PtrComponents { data_pointer, metadata } }.mut_ptr }
142+
#[cfg(bootstrap)]
143+
{
144+
// SAFETY: Accessing the value from the `PtrRepr` union is safe since *const T
145+
// and PtrComponents<T> have the same memory layouts. Only std can make this
146+
// guarantee.
147+
unsafe { PtrRepr { components: PtrComponents { data_pointer, metadata } }.mut_ptr }
148+
}
149+
#[cfg(not(bootstrap))]
150+
{
151+
aggregate_raw_ptr(data_pointer, metadata)
152+
}
137153
}
138154

139155
#[repr(C)]

core/tests/ptr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,3 +1163,11 @@ fn test_null_array_as_slice() {
11631163
assert!(ptr.is_null());
11641164
assert_eq!(ptr.len(), 4);
11651165
}
1166+
1167+
#[test]
1168+
fn test_ptr_from_raw_parts_in_const() {
1169+
const EMPTY_SLICE_PTR: *const [i32] =
1170+
std::ptr::slice_from_raw_parts(std::ptr::without_provenance(123), 456);
1171+
assert_eq!(EMPTY_SLICE_PTR.addr(), 123);
1172+
assert_eq!(EMPTY_SLICE_PTR.len(), 456);
1173+
}

core/tests/slice.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,3 +2678,16 @@ fn test_get_many_mut_duplicate() {
26782678
let mut v = vec![1, 2, 3, 4, 5];
26792679
assert!(v.get_many_mut([1, 3, 3, 4]).is_err());
26802680
}
2681+
2682+
#[test]
2683+
fn test_slice_from_raw_parts_in_const() {
2684+
static FANCY: i32 = 4;
2685+
static FANCY_SLICE: &[i32] = unsafe { std::slice::from_raw_parts(&FANCY, 1) };
2686+
assert_eq!(FANCY_SLICE.as_ptr(), std::ptr::addr_of!(FANCY));
2687+
assert_eq!(FANCY_SLICE.len(), 1);
2688+
2689+
const EMPTY_SLICE: &[i32] =
2690+
unsafe { std::slice::from_raw_parts(std::ptr::without_provenance(123456), 0) };
2691+
assert_eq!(EMPTY_SLICE.as_ptr().addr(), 123456);
2692+
assert_eq!(EMPTY_SLICE.len(), 0);
2693+
}

0 commit comments

Comments
 (0)