Skip to content

Commit 5eef5ee

Browse files
committed
stabilize const_ptr_is_null
1 parent 46e8d20 commit 5eef5ee

File tree

9 files changed

+18
-13
lines changed

9 files changed

+18
-13
lines changed

compiler/rustc_const_eval/src/const_eval/machine.rs

+6
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ impl<'tcx> CompileTimeInterpCx<'tcx> {
263263
}
264264

265265
/// See documentation on the `ptr_guaranteed_cmp` intrinsic.
266+
/// Returns `2` if the result is unknown.
267+
/// Returns `1` if the pointers are guaranteed equal.
268+
/// Returns `0` if the pointers are guaranteed inequal.
269+
///
270+
/// Note that this intrinsic is exposed on stable for comparison with null. In other words, any
271+
/// change to this function that affects comparison with null is insta-stable!
266272
fn guaranteed_cmp(&mut self, a: Scalar, b: Scalar) -> InterpResult<'tcx, u8> {
267273
interp_ok(match (a, b) {
268274
// Comparisons between integers are always known.

library/core/src/intrinsics/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3292,8 +3292,8 @@ pub const unsafe fn ptr_offset_from_unsigned<T>(_ptr: *const T, _base: *const T)
32923292

32933293
/// See documentation of `<*const T>::guaranteed_eq` for details.
32943294
/// Returns `2` if the result is unknown.
3295-
/// Returns `1` if the pointers are guaranteed equal
3296-
/// Returns `0` if the pointers are guaranteed inequal
3295+
/// Returns `1` if the pointers are guaranteed equal.
3296+
/// Returns `0` if the pointers are guaranteed inequal.
32973297
#[cfg_attr(bootstrap, rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020"))]
32983298
#[rustc_intrinsic]
32993299
#[rustc_nounwind]

library/core/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@
121121
#![feature(const_heap)]
122122
#![feature(const_nonnull_new)]
123123
#![feature(const_pin_2)]
124-
#![feature(const_ptr_is_null)]
125124
#![feature(const_ptr_sub_ptr)]
126125
#![feature(const_raw_ptr_comparison)]
127126
#![feature(const_size_of_val)]

library/core/src/ptr/const_ptr.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ impl<T: ?Sized> *const T {
2929
/// assert!(!ptr.is_null());
3030
/// ```
3131
#[stable(feature = "rust1", since = "1.0.0")]
32-
#[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")]
32+
#[rustc_const_stable(feature = "const_ptr_is_null", since = "CURRENT_RUSTC_VERSION")]
3333
#[rustc_diagnostic_item = "ptr_const_is_null"]
3434
#[inline]
35+
#[rustc_allow_const_fn_unstable(const_eval_select)]
3536
pub const fn is_null(self) -> bool {
3637
// Compare via a cast to a thin pointer, so fat pointers are only
3738
// considering their "data" part for null-ness.
3839
let ptr = self as *const u8;
3940
const_eval_select!(
4041
@capture { ptr: *const u8 } -> bool:
41-
if const #[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")] {
42+
// This use of `const_raw_ptr_comparison` has been explicitly blessed by t-lang.
43+
if const #[rustc_allow_const_fn_unstable(const_raw_ptr_comparison)] {
4244
match (ptr).guaranteed_eq(null_mut()) {
4345
Some(res) => res,
4446
// To remain maximally convervative, we stop execution when we don't
@@ -280,7 +282,7 @@ impl<T: ?Sized> *const T {
280282
/// }
281283
/// ```
282284
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
283-
#[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")]
285+
#[rustc_const_stable(feature = "const_ptr_is_null", since = "CURRENT_RUSTC_VERSION")]
284286
#[inline]
285287
pub const unsafe fn as_ref<'a>(self) -> Option<&'a T> {
286288
// SAFETY: the caller must guarantee that `self` is valid

library/core/src/ptr/mut_ptr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<T: ?Sized> *mut T {
2929
/// assert!(!ptr.is_null());
3030
/// ```
3131
#[stable(feature = "rust1", since = "1.0.0")]
32-
#[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")]
32+
#[rustc_const_stable(feature = "const_ptr_is_null", since = "CURRENT_RUSTC_VERSION")]
3333
#[rustc_diagnostic_item = "ptr_is_null"]
3434
#[inline]
3535
pub const fn is_null(self) -> bool {
@@ -271,7 +271,7 @@ impl<T: ?Sized> *mut T {
271271
/// }
272272
/// ```
273273
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
274-
#[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")]
274+
#[rustc_const_stable(feature = "const_ptr_is_null", since = "CURRENT_RUSTC_VERSION")]
275275
#[inline]
276276
pub const unsafe fn as_ref<'a>(self) -> Option<&'a T> {
277277
// SAFETY: the caller must guarantee that `self` is valid for a
@@ -619,7 +619,7 @@ impl<T: ?Sized> *mut T {
619619
/// println!("{s:?}"); // It'll print: "[4, 2, 3]".
620620
/// ```
621621
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
622-
#[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")]
622+
#[rustc_const_stable(feature = "const_ptr_is_null", since = "CURRENT_RUSTC_VERSION")]
623623
#[inline]
624624
pub const unsafe fn as_mut<'a>(self) -> Option<&'a mut T> {
625625
// SAFETY: the caller must guarantee that `self` is be valid for

library/core/src/ub_checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ macro_rules! assert_unsafe_precondition {
6565
#[inline]
6666
#[rustc_nounwind]
6767
#[cfg_attr(bootstrap, rustc_const_unstable(feature = "const_ub_checks", issue = "none"))]
68-
#[rustc_allow_const_fn_unstable(const_ptr_is_null, const_ub_checks)] // only for UB checks
68+
#[rustc_allow_const_fn_unstable(const_ub_checks)] // only for UB checks
6969
const fn precondition_check($($name:$ty),*) {
7070
if !$e {
7171
::core::panicking::panic_nounwind(

tests/ui/consts/const-ptr-is-null.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(const_ptr_is_null)]
21
use std::ptr;
32

43
const IS_NULL: () = {

tests/ui/consts/const-ptr-is-null.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ note: inside `std::ptr::const_ptr::<impl *const T>::is_null::compiletime`
88
note: inside `std::ptr::const_ptr::<impl *const i32>::is_null`
99
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
1010
note: inside `MAYBE_NULL`
11-
--> $DIR/const-ptr-is-null.rs:17:14
11+
--> $DIR/const-ptr-is-null.rs:16:14
1212
|
1313
LL | assert!(!ptr.wrapping_sub(512).is_null());
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui/consts/ptr_is_null.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//@ compile-flags: --crate-type=lib
22
//@ check-pass
33

4-
#![feature(const_ptr_is_null)]
54
#![allow(useless_ptr_null_checks)]
65

76
const FOO: &usize = &42;

0 commit comments

Comments
 (0)