Skip to content

Commit 95e7764

Browse files
committed
Auto merge of rust-lang#97522 - xfix:stabilize-slice-from-raw-parts, r=dtolnay
Partially stabilize const_slice_from_raw_parts This doesn't stabilize methods working on mutable pointers. This pull request continues from rust-lang#94946. Pinging `@rust-lang/wg-const-eval` this because I use `rustc_allow_const_fn_unstable`. I believe this is justifiable as it's already possible to use `slice::from_raw_parts` in stable by abusing `transmute`. The stable alternative to this would be to provide a stable const implementation of `std::ptr::from_raw_parts` (as it can already be implemented in stable). ```rust use std::mem; #[repr(C)] struct Slice<T> { data: *const T, len: usize, } fn main() { let data: *const i32 = [1, 2, 3, 4].as_ptr(); let len = 4; println!("{:?}", unsafe { mem::transmute::<Slice<i32>, &[i32]>(Slice { data, len }) }); } ``` `@rustbot` modify labels: +T-libs-api
2 parents 100142b + 0753fd1 commit 95e7764

File tree

11 files changed

+62
-67
lines changed

11 files changed

+62
-67
lines changed

compiler/rustc_codegen_cranelift/example/issue-91827-extern-types.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// Regression test for issue #91827.
77

88
#![feature(const_ptr_offset_from)]
9-
#![feature(const_slice_from_raw_parts)]
109
#![feature(extern_types)]
1110

1211
use std::ptr::addr_of;

library/core/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
#![feature(const_ptr_write)]
135135
#![feature(const_raw_ptr_comparison)]
136136
#![feature(const_size_of_val)]
137-
#![feature(const_slice_from_raw_parts)]
137+
#![feature(const_slice_from_raw_parts_mut)]
138138
#![feature(const_slice_ptr_len)]
139139
#![feature(const_str_from_utf8_unchecked_mut)]
140140
#![feature(const_swap)]

library/core/src/ptr/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,8 @@ pub const fn null_mut<T: ?Sized + Thin>() -> *mut T {
690690
/// ```
691691
#[inline]
692692
#[stable(feature = "slice_from_raw_parts", since = "1.42.0")]
693-
#[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")]
693+
#[rustc_const_stable(feature = "const_slice_from_raw_parts", since = "1.64.0")]
694+
#[rustc_allow_const_fn_unstable(ptr_metadata)]
694695
pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
695696
from_raw_parts(data.cast(), len)
696697
}
@@ -722,7 +723,7 @@ pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
722723
/// ```
723724
#[inline]
724725
#[stable(feature = "slice_from_raw_parts", since = "1.42.0")]
725-
#[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")]
726+
#[rustc_const_unstable(feature = "const_slice_from_raw_parts_mut", issue = "67456")]
726727
pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
727728
from_raw_parts_mut(data.cast(), len)
728729
}

library/core/src/slice/raw.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ use crate::ptr;
8585
/// [`NonNull::dangling()`]: ptr::NonNull::dangling
8686
#[inline]
8787
#[stable(feature = "rust1", since = "1.0.0")]
88-
#[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")]
88+
#[rustc_const_stable(feature = "const_slice_from_raw_parts", since = "1.64.0")]
8989
#[must_use]
9090
pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
9191
// SAFETY: the caller must uphold the safety contract for `from_raw_parts`.
@@ -129,7 +129,7 @@ pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T]
129129
/// [`NonNull::dangling()`]: ptr::NonNull::dangling
130130
#[inline]
131131
#[stable(feature = "rust1", since = "1.0.0")]
132-
#[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")]
132+
#[rustc_const_unstable(feature = "const_slice_from_raw_parts_mut", issue = "67456")]
133133
#[must_use]
134134
pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] {
135135
// SAFETY: the caller must uphold the safety contract for `from_raw_parts_mut`.

library/core/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
#![feature(iterator_try_reduce)]
7474
#![feature(const_mut_refs)]
7575
#![feature(const_pin)]
76-
#![feature(const_slice_from_raw_parts)]
7776
#![feature(never_type)]
7877
#![feature(unwrap_infallible)]
7978
#![feature(result_into_ok_or_err)]

src/test/ui/const-ptr/allowed_slices.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// run-pass
22
#![feature(
3-
const_slice_from_raw_parts,
43
slice_from_ptr_range,
54
const_slice_from_ptr_range,
65
pointer_byte_offsets,

src/test/ui/const-ptr/forbidden_slices.32bit.stderr

+28-28
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ LL | &*ptr::slice_from_raw_parts(data, len)
77
| dereferencing pointer failed: null pointer is a dangling pointer (it has no provenance)
88
| inside `std::slice::from_raw_parts::<u32>` at $SRC_DIR/core/src/slice/raw.rs:LL:COL
99
|
10-
::: $DIR/forbidden_slices.rs:19:34
10+
::: $DIR/forbidden_slices.rs:18:34
1111
|
1212
LL | pub static S0: &[u32] = unsafe { from_raw_parts(ptr::null(), 0) };
13-
| ------------------------------ inside `S0` at $DIR/forbidden_slices.rs:19:34
13+
| ------------------------------ inside `S0` at $DIR/forbidden_slices.rs:18:34
1414

1515
error[E0080]: could not evaluate static initializer
1616
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
@@ -21,10 +21,10 @@ LL | &*ptr::slice_from_raw_parts(data, len)
2121
| dereferencing pointer failed: null pointer is a dangling pointer (it has no provenance)
2222
| inside `std::slice::from_raw_parts::<()>` at $SRC_DIR/core/src/slice/raw.rs:LL:COL
2323
|
24-
::: $DIR/forbidden_slices.rs:20:33
24+
::: $DIR/forbidden_slices.rs:19:33
2525
|
2626
LL | pub static S1: &[()] = unsafe { from_raw_parts(ptr::null(), 0) };
27-
| ------------------------------ inside `S1` at $DIR/forbidden_slices.rs:20:33
27+
| ------------------------------ inside `S1` at $DIR/forbidden_slices.rs:19:33
2828

2929
error[E0080]: could not evaluate static initializer
3030
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
@@ -35,13 +35,13 @@ LL | &*ptr::slice_from_raw_parts(data, len)
3535
| dereferencing pointer failed: ALLOC_ID has size 4, so pointer to 8 bytes starting at offset 0 is out-of-bounds
3636
| inside `std::slice::from_raw_parts::<u32>` at $SRC_DIR/core/src/slice/raw.rs:LL:COL
3737
|
38-
::: $DIR/forbidden_slices.rs:23:34
38+
::: $DIR/forbidden_slices.rs:22:34
3939
|
4040
LL | pub static S2: &[u32] = unsafe { from_raw_parts(&D0, 2) };
41-
| ---------------------- inside `S2` at $DIR/forbidden_slices.rs:23:34
41+
| ---------------------- inside `S2` at $DIR/forbidden_slices.rs:22:34
4242

4343
error[E0080]: it is undefined behavior to use this value
44-
--> $DIR/forbidden_slices.rs:26:1
44+
--> $DIR/forbidden_slices.rs:25:1
4545
|
4646
LL | pub static S4: &[u8] = unsafe { from_raw_parts((&D1) as *const _ as _, 1) };
4747
| ^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered uninitialized bytes
@@ -52,7 +52,7 @@ LL | pub static S4: &[u8] = unsafe { from_raw_parts((&D1) as *const _ as _, 1) }
5252
}
5353

5454
error[E0080]: it is undefined behavior to use this value
55-
--> $DIR/forbidden_slices.rs:28:1
55+
--> $DIR/forbidden_slices.rs:27:1
5656
|
5757
LL | pub static S5: &[u8] = unsafe { from_raw_parts((&D3) as *const _ as _, size_of::<&u32>()) };
5858
| ^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>: encountered a pointer, but expected plain (non-pointer) bytes
@@ -63,7 +63,7 @@ LL | pub static S5: &[u8] = unsafe { from_raw_parts((&D3) as *const _ as _, size
6363
}
6464

6565
error[E0080]: it is undefined behavior to use this value
66-
--> $DIR/forbidden_slices.rs:30:1
66+
--> $DIR/forbidden_slices.rs:29:1
6767
|
6868
LL | pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4) };
6969
| ^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered 0x11, but expected a boolean
@@ -74,7 +74,7 @@ LL | pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4)
7474
}
7575

7676
error[E0080]: it is undefined behavior to use this value
77-
--> $DIR/forbidden_slices.rs:33:1
77+
--> $DIR/forbidden_slices.rs:32:1
7878
|
7979
LL | pub static S7: &[u16] = unsafe {
8080
| ^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1)
@@ -93,10 +93,10 @@ LL | &*ptr::slice_from_raw_parts(data, len)
9393
| dereferencing pointer failed: ALLOC_ID has size 8, so pointer to 8 bytes starting at offset 1 is out-of-bounds
9494
| inside `std::slice::from_raw_parts::<u64>` at $SRC_DIR/core/src/slice/raw.rs:LL:COL
9595
|
96-
::: $DIR/forbidden_slices.rs:44:5
96+
::: $DIR/forbidden_slices.rs:43:5
9797
|
9898
LL | from_raw_parts(ptr, 1)
99-
| ---------------------- inside `S8` at $DIR/forbidden_slices.rs:44:5
99+
| ---------------------- inside `S8` at $DIR/forbidden_slices.rs:43:5
100100

101101
error[E0080]: could not evaluate static initializer
102102
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
@@ -112,10 +112,10 @@ LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) }
112112
LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) }
113113
| ------------------------------ inside `from_ptr_range::<u32>` at $SRC_DIR/core/src/slice/raw.rs:LL:COL
114114
|
115-
::: $DIR/forbidden_slices.rs:47:34
115+
::: $DIR/forbidden_slices.rs:46:34
116116
|
117117
LL | pub static R0: &[u32] = unsafe { from_ptr_range(ptr::null()..ptr::null()) };
118-
| ---------------------------------------- inside `R0` at $DIR/forbidden_slices.rs:47:34
118+
| ---------------------------------------- inside `R0` at $DIR/forbidden_slices.rs:46:34
119119

120120
error[E0080]: could not evaluate static initializer
121121
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
@@ -131,10 +131,10 @@ LL | assert!(0 < pointee_size && pointee_size <= isize::MAX as usize);
131131
LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) }
132132
| ------------------------------ inside `from_ptr_range::<()>` at $SRC_DIR/core/src/slice/raw.rs:LL:COL
133133
|
134-
::: $DIR/forbidden_slices.rs:48:33
134+
::: $DIR/forbidden_slices.rs:47:33
135135
|
136136
LL | pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) };
137-
| ---------------------------------------- inside `R1` at $DIR/forbidden_slices.rs:48:33
137+
| ---------------------------------------- inside `R1` at $DIR/forbidden_slices.rs:47:33
138138
|
139139
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
140140

@@ -150,13 +150,13 @@ LL | unsafe { intrinsics::offset(self, count) }
150150
LL | unsafe { self.offset(count as isize) }
151151
| --------------------------- inside `ptr::const_ptr::<impl *const u32>::add` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
152152
|
153-
::: $DIR/forbidden_slices.rs:51:25
153+
::: $DIR/forbidden_slices.rs:50:25
154154
|
155155
LL | from_ptr_range(ptr..ptr.add(2))
156-
| ---------- inside `R2` at $DIR/forbidden_slices.rs:51:25
156+
| ---------- inside `R2` at $DIR/forbidden_slices.rs:50:25
157157

158158
error[E0080]: it is undefined behavior to use this value
159-
--> $DIR/forbidden_slices.rs:53:1
159+
--> $DIR/forbidden_slices.rs:52:1
160160
|
161161
LL | pub static R4: &[u8] = unsafe {
162162
| ^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered uninitialized bytes
@@ -167,7 +167,7 @@ LL | pub static R4: &[u8] = unsafe {
167167
}
168168

169169
error[E0080]: it is undefined behavior to use this value
170-
--> $DIR/forbidden_slices.rs:58:1
170+
--> $DIR/forbidden_slices.rs:57:1
171171
|
172172
LL | pub static R5: &[u8] = unsafe {
173173
| ^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>: encountered a pointer, but expected plain (non-pointer) bytes
@@ -178,7 +178,7 @@ LL | pub static R5: &[u8] = unsafe {
178178
}
179179

180180
error[E0080]: it is undefined behavior to use this value
181-
--> $DIR/forbidden_slices.rs:63:1
181+
--> $DIR/forbidden_slices.rs:62:1
182182
|
183183
LL | pub static R6: &[bool] = unsafe {
184184
| ^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered 0x11, but expected a boolean
@@ -189,7 +189,7 @@ LL | pub static R6: &[bool] = unsafe {
189189
}
190190

191191
error[E0080]: it is undefined behavior to use this value
192-
--> $DIR/forbidden_slices.rs:68:1
192+
--> $DIR/forbidden_slices.rs:67:1
193193
|
194194
LL | pub static R7: &[u16] = unsafe {
195195
| ^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1)
@@ -211,10 +211,10 @@ LL | unsafe { intrinsics::offset(self, count) }
211211
LL | unsafe { self.offset(count as isize) }
212212
| --------------------------- inside `ptr::const_ptr::<impl *const u64>::add` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
213213
|
214-
::: $DIR/forbidden_slices.rs:75:25
214+
::: $DIR/forbidden_slices.rs:74:25
215215
|
216216
LL | from_ptr_range(ptr..ptr.add(1))
217-
| ---------- inside `R8` at $DIR/forbidden_slices.rs:75:25
217+
| ---------- inside `R8` at $DIR/forbidden_slices.rs:74:25
218218

219219
error[E0080]: could not evaluate static initializer
220220
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
@@ -230,10 +230,10 @@ LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) }
230230
LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) }
231231
| ------------------------------ inside `from_ptr_range::<u32>` at $SRC_DIR/core/src/slice/raw.rs:LL:COL
232232
|
233-
::: $DIR/forbidden_slices.rs:80:34
233+
::: $DIR/forbidden_slices.rs:79:34
234234
|
235235
LL | pub static R9: &[u32] = unsafe { from_ptr_range(&D0..(&D0 as *const u32).add(1)) };
236-
| ----------------------------------------------- inside `R9` at $DIR/forbidden_slices.rs:80:34
236+
| ----------------------------------------------- inside `R9` at $DIR/forbidden_slices.rs:79:34
237237

238238
error[E0080]: could not evaluate static initializer
239239
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
@@ -249,10 +249,10 @@ LL | unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) }
249249
LL | unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) }
250250
| ------------------------------ inside `from_ptr_range::<u32>` at $SRC_DIR/core/src/slice/raw.rs:LL:COL
251251
|
252-
::: $DIR/forbidden_slices.rs:81:35
252+
::: $DIR/forbidden_slices.rs:80:35
253253
|
254254
LL | pub static R10: &[u32] = unsafe { from_ptr_range(&D0..&D0) };
255-
| ------------------------ inside `R10` at $DIR/forbidden_slices.rs:81:35
255+
| ------------------------ inside `R10` at $DIR/forbidden_slices.rs:80:35
256256

257257
error: aborting due to 18 previous errors
258258

0 commit comments

Comments
 (0)