Skip to content

Commit 1dbd972

Browse files
authored
Unrolled build for rust-lang#125701
Rollup merge of rust-lang#125701 - scottmcm:generic-from-raw-parts, r=WaffleLapkin [ACP 362] genericize `ptr::from_raw_parts` This implements rust-lang/libs-team#362 As such, it can partially undo rust-lang#124795 , letting `slice_from_raw_parts` just call `from_raw_parts` again without re-introducing the unnecessary cast to MIR. By doing this it also removes a spurious cast from `str::from_raw_parts`. And I think it does a good job of showing the value of the ACP, since the only thing that needed new turbofishing because of this is inside `ptr::null(_mut)`, but only because `ptr::without_provenance(_mut)` doesn't support pointers to extern types, which it absolutely could (without even changing the implementation).
2 parents 23ea77b + 0d63e6b commit 1dbd972

13 files changed

+28
-18
lines changed

library/core/src/ptr/metadata.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub const fn metadata<T: ?Sized>(ptr: *const T) -> <T as Pointee>::Metadata {
120120
#[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")]
121121
#[inline]
122122
pub const fn from_raw_parts<T: ?Sized>(
123-
data_pointer: *const (),
123+
data_pointer: *const impl Thin,
124124
metadata: <T as Pointee>::Metadata,
125125
) -> *const T {
126126
aggregate_raw_ptr(data_pointer, metadata)
@@ -134,7 +134,7 @@ pub const fn from_raw_parts<T: ?Sized>(
134134
#[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")]
135135
#[inline]
136136
pub const fn from_raw_parts_mut<T: ?Sized>(
137-
data_pointer: *mut (),
137+
data_pointer: *mut impl Thin,
138138
metadata: <T as Pointee>::Metadata,
139139
) -> *mut T {
140140
aggregate_raw_ptr(data_pointer, metadata)

library/core/src/ptr/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
565565
#[rustc_allow_const_fn_unstable(ptr_metadata)]
566566
#[rustc_diagnostic_item = "ptr_null"]
567567
pub const fn null<T: ?Sized + Thin>() -> *const T {
568-
from_raw_parts(without_provenance(0), ())
568+
from_raw_parts(without_provenance::<()>(0), ())
569569
}
570570

571571
/// Creates a null mutable raw pointer.
@@ -591,7 +591,7 @@ pub const fn null<T: ?Sized + Thin>() -> *const T {
591591
#[rustc_allow_const_fn_unstable(ptr_metadata)]
592592
#[rustc_diagnostic_item = "ptr_null_mut"]
593593
pub const fn null_mut<T: ?Sized + Thin>() -> *mut T {
594-
from_raw_parts_mut(without_provenance_mut(0), ())
594+
from_raw_parts_mut(without_provenance_mut::<()>(0), ())
595595
}
596596

597597
/// Creates a pointer with the given address and no provenance.
@@ -835,7 +835,7 @@ pub const fn from_mut<T: ?Sized>(r: &mut T) -> *mut T {
835835
#[rustc_allow_const_fn_unstable(ptr_metadata)]
836836
#[rustc_diagnostic_item = "ptr_slice_from_raw_parts"]
837837
pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
838-
intrinsics::aggregate_raw_ptr(data, len)
838+
from_raw_parts(data, len)
839839
}
840840

841841
/// Forms a raw mutable slice from a pointer and a length.
@@ -881,7 +881,7 @@ pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
881881
#[rustc_const_unstable(feature = "const_slice_from_raw_parts_mut", issue = "67456")]
882882
#[rustc_diagnostic_item = "ptr_slice_from_raw_parts_mut"]
883883
pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
884-
intrinsics::aggregate_raw_ptr(data, len)
884+
from_raw_parts_mut(data, len)
885885
}
886886

887887
/// Swaps the values at two mutable locations of the same type, without

library/core/src/str/converts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ pub const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
222222
#[rustc_const_unstable(feature = "str_from_raw_parts", issue = "119206")]
223223
pub const unsafe fn from_raw_parts<'a>(ptr: *const u8, len: usize) -> &'a str {
224224
// SAFETY: the caller must uphold the safety contract for `from_raw_parts`.
225-
unsafe { &*ptr::from_raw_parts(ptr.cast(), len) }
225+
unsafe { &*ptr::from_raw_parts(ptr, len) }
226226
}
227227

228228
/// Creates an `&mut str` from a pointer and a length.
@@ -241,5 +241,5 @@ pub const unsafe fn from_raw_parts<'a>(ptr: *const u8, len: usize) -> &'a str {
241241
#[rustc_const_unstable(feature = "const_str_from_raw_parts_mut", issue = "119206")]
242242
pub const unsafe fn from_raw_parts_mut<'a>(ptr: *mut u8, len: usize) -> &'a mut str {
243243
// SAFETY: the caller must uphold the safety contract for `from_raw_parts_mut`.
244-
unsafe { &mut *ptr::from_raw_parts_mut(ptr.cast(), len) }
244+
unsafe { &mut *ptr::from_raw_parts_mut(ptr, len) }
245245
}

library/core/tests/mem.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ fn align_of_val_raw_packed() {
8383
f: [u32],
8484
}
8585
let storage = [0u8; 4];
86-
let b: *const B = ptr::from_raw_parts(storage.as_ptr().cast(), 1);
86+
let b: *const B = ptr::from_raw_parts(storage.as_ptr(), 1);
8787
assert_eq!(unsafe { align_of_val_raw(b) }, 1);
8888

8989
const ALIGN_OF_VAL_RAW: usize = {
9090
let storage = [0u8; 4];
91-
let b: *const B = ptr::from_raw_parts(storage.as_ptr().cast(), 1);
91+
let b: *const B = ptr::from_raw_parts(storage.as_ptr(), 1);
9292
unsafe { align_of_val_raw(b) }
9393
};
9494
assert_eq!(ALIGN_OF_VAL_RAW, 1);

library/core/tests/ptr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -965,15 +965,15 @@ fn thin_box() {
965965
fn value_ptr(&self) -> *const T {
966966
let (_, offset) = self.layout();
967967
let data_ptr = unsafe { self.ptr.cast::<u8>().as_ptr().add(offset) };
968-
ptr::from_raw_parts(data_ptr.cast(), self.meta())
968+
ptr::from_raw_parts(data_ptr, self.meta())
969969
}
970970

971971
fn value_mut_ptr(&mut self) -> *mut T {
972972
let (_, offset) = self.layout();
973973
// FIXME: can this line be shared with the same in `value_ptr()`
974974
// without upsetting Stacked Borrows?
975975
let data_ptr = unsafe { self.ptr.cast::<u8>().as_ptr().add(offset) };
976-
from_raw_parts_mut(data_ptr.cast(), self.meta())
976+
from_raw_parts_mut(data_ptr, self.meta())
977977
}
978978
}
979979

tests/mir-opt/inline/inline_shims.drop.Inline.panic-abort.diff

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
+ }
2828
+ }
2929
+ scope 7 (inlined slice_from_raw_parts_mut::<A>) {
30+
+ scope 8 (inlined std::ptr::from_raw_parts_mut::<[A], A>) {
31+
+ }
3032
+ }
3133
+ }
3234
+ }
33-
+ scope 8 (inlined std::ptr::drop_in_place::<Option<B>> - shim(Some(Option<B>))) {
35+
+ scope 9 (inlined std::ptr::drop_in_place::<Option<B>> - shim(Some(Option<B>))) {
3436
+ let mut _14: isize;
3537
+ let mut _15: isize;
3638
+ }

tests/mir-opt/pre-codegen/ptr_offset.demo_byte_add_fat.PreCodegen.after.panic-abort.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn demo_byte_add_fat(_1: *const [u32], _2: usize) -> *const [u32] {
1616
let mut _6: usize;
1717
scope 5 (inlined std::ptr::metadata::<[u32]>) {
1818
}
19-
scope 6 (inlined std::ptr::from_raw_parts::<[u32]>) {
19+
scope 6 (inlined std::ptr::from_raw_parts::<[u32], ()>) {
2020
}
2121
}
2222
}

tests/mir-opt/pre-codegen/ptr_offset.demo_byte_add_fat.PreCodegen.after.panic-unwind.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn demo_byte_add_fat(_1: *const [u32], _2: usize) -> *const [u32] {
1616
let mut _6: usize;
1717
scope 5 (inlined std::ptr::metadata::<[u32]>) {
1818
}
19-
scope 6 (inlined std::ptr::from_raw_parts::<[u32]>) {
19+
scope 6 (inlined std::ptr::from_raw_parts::<[u32], ()>) {
2020
}
2121
}
2222
}

tests/mir-opt/pre-codegen/ptr_offset.demo_byte_add_thin.PreCodegen.after.panic-abort.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn demo_byte_add_thin(_1: *const u32, _2: usize) -> *const u32 {
1414
scope 4 (inlined std::ptr::const_ptr::<impl *const u8>::with_metadata_of::<u32>) {
1515
scope 5 (inlined std::ptr::metadata::<u32>) {
1616
}
17-
scope 6 (inlined std::ptr::from_raw_parts::<u32>) {
17+
scope 6 (inlined std::ptr::from_raw_parts::<u32, ()>) {
1818
}
1919
}
2020
}

tests/mir-opt/pre-codegen/ptr_offset.demo_byte_add_thin.PreCodegen.after.panic-unwind.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn demo_byte_add_thin(_1: *const u32, _2: usize) -> *const u32 {
1414
scope 4 (inlined std::ptr::const_ptr::<impl *const u8>::with_metadata_of::<u32>) {
1515
scope 5 (inlined std::ptr::metadata::<u32>) {
1616
}
17-
scope 6 (inlined std::ptr::from_raw_parts::<u32>) {
17+
scope 6 (inlined std::ptr::from_raw_parts::<u32, ()>) {
1818
}
1919
}
2020
}

tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-abort.mir

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
3737
scope 11 (inlined slice_from_raw_parts::<u8>) {
3838
debug data => _4;
3939
debug len => _5;
40+
scope 12 (inlined std::ptr::from_raw_parts::<[u8], u8>) {
41+
debug data_pointer => _4;
42+
debug metadata => _5;
43+
}
4044
}
4145
}
4246
}

tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-unwind.mir

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
3737
scope 11 (inlined slice_from_raw_parts::<u8>) {
3838
debug data => _4;
3939
debug len => _5;
40+
scope 12 (inlined std::ptr::from_raw_parts::<[u8], u8>) {
41+
debug data_pointer => _4;
42+
debug metadata => _5;
43+
}
4044
}
4145
}
4246
}

tests/ui/unsized/unsized3-rpass.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn main() {
8787
let obj: Box<St> = Box::new(St { f: 42 });
8888
let obj: &Tr = &*obj;
8989
let data: Box<_> = Box::new(Qux_ { f: St { f: 234 } });
90-
let x: &Qux = &*ptr::from_raw_parts::<Qux>((&*data as *const _).cast(), ptr::metadata(obj));
90+
let x: &Qux = &*ptr::from_raw_parts::<Qux>(&*data as *const _, ptr::metadata(obj));
9191
assert_eq!(x.f.foo(), 234);
9292
}
9393
}

0 commit comments

Comments
 (0)