Skip to content

Commit 88fbfaf

Browse files
authored
Rollup merge of #97320 - usbalbin:stabilize_const_ptr_read, r=m-ou-se
Stabilize const_ptr_read Stabilizes const_ptr_read, with tracking issue #80377
2 parents 3a37c2f + 81c2459 commit 88fbfaf

17 files changed

+35
-42
lines changed

library/alloc/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@
113113
#![feature(const_maybe_uninit_write)]
114114
#![feature(const_maybe_uninit_zeroed)]
115115
#![feature(const_pin)]
116-
#![feature(const_ptr_read)]
117116
#![feature(const_refs_to_cell)]
118117
#![feature(const_size_of_val)]
119118
#![feature(const_waker)]

library/core/src/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2260,7 +2260,7 @@ extern "rust-intrinsic" {
22602260
/// This intrinsic can *only* be called where the pointer is a local without
22612261
/// projections (`read_via_copy(ptr)`, not `read_via_copy(*ptr)`) so that it
22622262
/// trivially obeys runtime-MIR rules about derefs in operands.
2263-
#[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
2263+
#[rustc_const_stable(feature = "const_ptr_read", since = "CURRENT_RUSTC_VERSION")]
22642264
#[rustc_nounwind]
22652265
pub fn read_via_copy<T>(ptr: *const T) -> T;
22662266

library/core/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@
140140
#![feature(const_pointer_is_aligned)]
141141
#![feature(const_ptr_as_ref)]
142142
#![feature(const_ptr_is_null)]
143-
#![feature(const_ptr_read)]
144143
#![feature(const_ptr_sub_ptr)]
145144
#![feature(const_ptr_write)]
146145
#![feature(const_raw_ptr_comparison)]

library/core/src/ptr/const_ptr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ impl<T: ?Sized> *const T {
11951195
///
11961196
/// [`ptr::read`]: crate::ptr::read()
11971197
#[stable(feature = "pointer_methods", since = "1.26.0")]
1198-
#[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
1198+
#[rustc_const_stable(feature = "const_ptr_read", since = "CURRENT_RUSTC_VERSION")]
11991199
#[inline]
12001200
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
12011201
pub const unsafe fn read(self) -> T
@@ -1236,7 +1236,7 @@ impl<T: ?Sized> *const T {
12361236
///
12371237
/// [`ptr::read_unaligned`]: crate::ptr::read_unaligned()
12381238
#[stable(feature = "pointer_methods", since = "1.26.0")]
1239-
#[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
1239+
#[rustc_const_stable(feature = "const_ptr_read", since = "CURRENT_RUSTC_VERSION")]
12401240
#[inline]
12411241
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
12421242
pub const unsafe fn read_unaligned(self) -> T

library/core/src/ptr/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,8 @@ pub const unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
11331133
/// [valid]: self#safety
11341134
#[inline]
11351135
#[stable(feature = "rust1", since = "1.0.0")]
1136-
#[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
1136+
#[rustc_const_stable(feature = "const_ptr_read", since = "CURRENT_RUSTC_VERSION")]
1137+
#[rustc_allow_const_fn_unstable(const_mut_refs, const_maybe_uninit_as_mut_ptr)]
11371138
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
11381139
pub const unsafe fn read<T>(src: *const T) -> T {
11391140
// It would be semantically correct to implement this via `copy_nonoverlapping`
@@ -1249,7 +1250,8 @@ pub const unsafe fn read<T>(src: *const T) -> T {
12491250
/// ```
12501251
#[inline]
12511252
#[stable(feature = "ptr_unaligned", since = "1.17.0")]
1252-
#[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
1253+
#[rustc_const_stable(feature = "const_ptr_read", since = "CURRENT_RUSTC_VERSION")]
1254+
#[rustc_allow_const_fn_unstable(const_mut_refs, const_maybe_uninit_as_mut_ptr)]
12531255
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
12541256
pub const unsafe fn read_unaligned<T>(src: *const T) -> T {
12551257
let mut tmp = MaybeUninit::<T>::uninit();

library/core/src/ptr/mut_ptr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ impl<T: ?Sized> *mut T {
13051305
///
13061306
/// [`ptr::read`]: crate::ptr::read()
13071307
#[stable(feature = "pointer_methods", since = "1.26.0")]
1308-
#[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
1308+
#[rustc_const_stable(feature = "const_ptr_read", since = "CURRENT_RUSTC_VERSION")]
13091309
#[inline(always)]
13101310
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
13111311
pub const unsafe fn read(self) -> T
@@ -1346,7 +1346,7 @@ impl<T: ?Sized> *mut T {
13461346
///
13471347
/// [`ptr::read_unaligned`]: crate::ptr::read_unaligned()
13481348
#[stable(feature = "pointer_methods", since = "1.26.0")]
1349-
#[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
1349+
#[rustc_const_stable(feature = "const_ptr_read", since = "CURRENT_RUSTC_VERSION")]
13501350
#[inline(always)]
13511351
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
13521352
pub const unsafe fn read_unaligned(self) -> T

library/core/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#![feature(const_pointer_byte_offsets)]
1919
#![feature(const_pointer_is_aligned)]
2020
#![feature(const_ptr_as_ref)]
21-
#![feature(const_ptr_read)]
2221
#![feature(const_ptr_write)]
2322
#![feature(const_trait_impl)]
2423
#![feature(const_likely)]

src/tools/miri/tests/fail/const-ub-checks.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(const_ptr_read)]
21

32
const UNALIGNED_READ: () = unsafe {
43
let x = &[0u8; 4];

tests/ui/const-generics/issues/issue-105821.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// check-pass
22

33
#![allow(incomplete_features)]
4-
#![feature(adt_const_params, const_ptr_read, generic_const_exprs)]
4+
#![feature(adt_const_params, generic_const_exprs)]
55
#![allow(dead_code)]
66

77
const fn catone<const M: usize>(_a: &[u8; M]) -> [u8; M + 1]

tests/ui/const-ptr/out_of_bounds_read.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// error-pattern: evaluation of constant value failed
22

3-
#![feature(const_ptr_read)]
4-
53
fn main() {
64
use std::ptr;
75

tests/ui/const-ptr/out_of_bounds_read.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ error[E0080]: evaluation of constant value failed
66
note: inside `std::ptr::read::<u32>`
77
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
88
note: inside `_READ`
9-
--> $DIR/out_of_bounds_read.rs:12:33
9+
--> $DIR/out_of_bounds_read.rs:10:33
1010
|
1111
LL | const _READ: u32 = unsafe { ptr::read(PAST_END_PTR) };
1212
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -21,7 +21,7 @@ note: inside `std::ptr::read::<u32>`
2121
note: inside `ptr::const_ptr::<impl *const u32>::read`
2222
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
2323
note: inside `_CONST_READ`
24-
--> $DIR/out_of_bounds_read.rs:13:39
24+
--> $DIR/out_of_bounds_read.rs:11:39
2525
|
2626
LL | const _CONST_READ: u32 = unsafe { PAST_END_PTR.read() };
2727
| ^^^^^^^^^^^^^^^^^^^
@@ -36,7 +36,7 @@ note: inside `std::ptr::read::<u32>`
3636
note: inside `ptr::mut_ptr::<impl *mut u32>::read`
3737
--> $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
3838
note: inside `_MUT_READ`
39-
--> $DIR/out_of_bounds_read.rs:14:37
39+
--> $DIR/out_of_bounds_read.rs:12:37
4040
|
4141
LL | const _MUT_READ: u32 = unsafe { (PAST_END_PTR as *mut u32).read() };
4242
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui/consts/const-eval/ub-ref-ptr.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// normalize-stderr-test "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
44
// normalize-stderr-test "([0-9a-f][0-9a-f] |╾─*a(lloc)?[0-9]+(\+[a-z0-9]+)?─*╼ )+ *│.*" -> "HEX_DUMP"
55
#![allow(invalid_value)]
6-
#![feature(const_ptr_read)]
76

87
use std::mem;
98

tests/ui/consts/const-eval/ub-ref-ptr.stderr

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0080]: it is undefined behavior to use this value
2-
--> $DIR/ub-ref-ptr.rs:16:1
2+
--> $DIR/ub-ref-ptr.rs:15:1
33
|
44
LL | const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
55
| ^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1)
@@ -10,7 +10,7 @@ LL | const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
1010
}
1111

1212
error[E0080]: it is undefined behavior to use this value
13-
--> $DIR/ub-ref-ptr.rs:20:1
13+
--> $DIR/ub-ref-ptr.rs:19:1
1414
|
1515
LL | const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered an unaligned box (required 2 byte alignment but found 1)
@@ -21,7 +21,7 @@ LL | const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
2121
}
2222

2323
error[E0080]: it is undefined behavior to use this value
24-
--> $DIR/ub-ref-ptr.rs:24:1
24+
--> $DIR/ub-ref-ptr.rs:23:1
2525
|
2626
LL | const NULL: &u16 = unsafe { mem::transmute(0usize) };
2727
| ^^^^^^^^^^^^^^^^ constructing invalid value: encountered a null reference
@@ -32,7 +32,7 @@ LL | const NULL: &u16 = unsafe { mem::transmute(0usize) };
3232
}
3333

3434
error[E0080]: it is undefined behavior to use this value
35-
--> $DIR/ub-ref-ptr.rs:27:1
35+
--> $DIR/ub-ref-ptr.rs:26:1
3636
|
3737
LL | const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a null box
@@ -43,7 +43,7 @@ LL | const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
4343
}
4444

4545
error[E0080]: evaluation of constant value failed
46-
--> $DIR/ub-ref-ptr.rs:34:1
46+
--> $DIR/ub-ref-ptr.rs:33:1
4747
|
4848
LL | const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes
@@ -52,7 +52,7 @@ LL | const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
5252
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
5353

5454
error[E0080]: evaluation of constant value failed
55-
--> $DIR/ub-ref-ptr.rs:37:39
55+
--> $DIR/ub-ref-ptr.rs:36:39
5656
|
5757
LL | const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
5858
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes
@@ -61,13 +61,13 @@ LL | const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
6161
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
6262

6363
note: erroneous constant used
64-
--> $DIR/ub-ref-ptr.rs:37:38
64+
--> $DIR/ub-ref-ptr.rs:36:38
6565
|
6666
LL | const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6868

6969
error[E0080]: evaluation of constant value failed
70-
--> $DIR/ub-ref-ptr.rs:40:86
70+
--> $DIR/ub-ref-ptr.rs:39:86
7171
|
7272
LL | const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) };
7373
| ^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes
@@ -76,13 +76,13 @@ LL | const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[us
7676
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
7777

7878
note: erroneous constant used
79-
--> $DIR/ub-ref-ptr.rs:40:85
79+
--> $DIR/ub-ref-ptr.rs:39:85
8080
|
8181
LL | const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) };
8282
| ^^^^^^^^^^^^^^^^^^^^^
8383

8484
error[E0080]: it is undefined behavior to use this value
85-
--> $DIR/ub-ref-ptr.rs:43:1
85+
--> $DIR/ub-ref-ptr.rs:42:1
8686
|
8787
LL | const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
8888
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a dangling reference (0x539[noalloc] has no provenance)
@@ -93,7 +93,7 @@ LL | const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
9393
}
9494

9595
error[E0080]: it is undefined behavior to use this value
96-
--> $DIR/ub-ref-ptr.rs:46:1
96+
--> $DIR/ub-ref-ptr.rs:45:1
9797
|
9898
LL | const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
9999
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a dangling box (0x539[noalloc] has no provenance)
@@ -104,13 +104,13 @@ LL | const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
104104
}
105105

106106
error[E0080]: evaluation of constant value failed
107-
--> $DIR/ub-ref-ptr.rs:49:41
107+
--> $DIR/ub-ref-ptr.rs:48:41
108108
|
109109
LL | const UNINIT_PTR: *const i32 = unsafe { MaybeUninit { uninit: () }.init };
110110
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
111111

112112
error[E0080]: it is undefined behavior to use this value
113-
--> $DIR/ub-ref-ptr.rs:53:1
113+
--> $DIR/ub-ref-ptr.rs:52:1
114114
|
115115
LL | const NULL_FN_PTR: fn() = unsafe { mem::transmute(0usize) };
116116
| ^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered null pointer, but expected a function pointer
@@ -121,13 +121,13 @@ LL | const NULL_FN_PTR: fn() = unsafe { mem::transmute(0usize) };
121121
}
122122

123123
error[E0080]: evaluation of constant value failed
124-
--> $DIR/ub-ref-ptr.rs:55:38
124+
--> $DIR/ub-ref-ptr.rs:54:38
125125
|
126126
LL | const UNINIT_FN_PTR: fn() = unsafe { MaybeUninit { uninit: () }.init };
127127
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
128128

129129
error[E0080]: it is undefined behavior to use this value
130-
--> $DIR/ub-ref-ptr.rs:58:1
130+
--> $DIR/ub-ref-ptr.rs:57:1
131131
|
132132
LL | const DANGLING_FN_PTR: fn() = unsafe { mem::transmute(13usize) };
133133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0xd[noalloc], but expected a function pointer
@@ -138,7 +138,7 @@ LL | const DANGLING_FN_PTR: fn() = unsafe { mem::transmute(13usize) };
138138
}
139139

140140
error[E0080]: it is undefined behavior to use this value
141-
--> $DIR/ub-ref-ptr.rs:60:1
141+
--> $DIR/ub-ref-ptr.rs:59:1
142142
|
143143
LL | const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) };
144144
| ^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered alloc41, but expected a function pointer
@@ -158,7 +158,7 @@ note: inside `std::ptr::read::<u32>`
158158
note: inside `ptr::const_ptr::<impl *const u32>::read`
159159
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
160160
note: inside `UNALIGNED_READ`
161-
--> $DIR/ub-ref-ptr.rs:67:5
161+
--> $DIR/ub-ref-ptr.rs:66:5
162162
|
163163
LL | ptr.read();
164164
| ^^^^^^^^^^

tests/ui/consts/extra-const-ub/detect-extra-ub.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// revisions: no_flag with_flag
22
// [no_flag] check-pass
33
// [with_flag] compile-flags: -Zextra-const-ub-checks
4-
#![feature(const_ptr_read)]
54

65
use std::mem::transmute;
76

tests/ui/consts/extra-const-ub/detect-extra-ub.with_flag.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0080]: evaluation of constant value failed
2-
--> $DIR/detect-extra-ub.rs:9:20
2+
--> $DIR/detect-extra-ub.rs:8:20
33
|
44
LL | let _x: bool = transmute(3u8);
55
| ^^^^^^^^^^^^^^ constructing invalid value: encountered 0x03, but expected a boolean
66

77
error[E0080]: evaluation of constant value failed
8-
--> $DIR/detect-extra-ub.rs:15:21
8+
--> $DIR/detect-extra-ub.rs:14:21
99
|
1010
LL | let _x: usize = transmute(&3u8);
1111
| ^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes
@@ -14,7 +14,7 @@ LL | let _x: usize = transmute(&3u8);
1414
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
1515

1616
error[E0080]: evaluation of constant value failed
17-
--> $DIR/detect-extra-ub.rs:21:30
17+
--> $DIR/detect-extra-ub.rs:20:30
1818
|
1919
LL | let _x: (usize, usize) = transmute(x);
2020
| ^^^^^^^^^^^^ unable to turn pointer into raw bytes
@@ -23,7 +23,7 @@ LL | let _x: (usize, usize) = transmute(x);
2323
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
2424

2525
error[E0080]: evaluation of constant value failed
26-
--> $DIR/detect-extra-ub.rs:26:20
26+
--> $DIR/detect-extra-ub.rs:25:20
2727
|
2828
LL | let _x: &u32 = transmute(&[0u8; 4]);
2929
| ^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered an unaligned reference (required 4 byte alignment but found 1)

tests/ui/consts/issue-miri-1910.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// error-pattern unable to turn pointer into raw bytes
22
// normalize-stderr-test: "alloc[0-9]+\+0x[a-z0-9]+" -> "ALLOC"
3-
#![feature(const_ptr_read)]
43

54
const C: () = unsafe {
65
let foo = Some(&42 as *const i32);

tests/ui/consts/issue-miri-1910.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ note: inside `std::ptr::read::<u8>`
1010
note: inside `ptr::const_ptr::<impl *const u8>::read`
1111
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
1212
note: inside `C`
13-
--> $DIR/issue-miri-1910.rs:8:5
13+
--> $DIR/issue-miri-1910.rs:7:5
1414
|
1515
LL | (&foo as *const _ as *const u8).add(one_and_a_half_pointers).read();
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)