Skip to content

Commit 33970db

Browse files
committed
Add #[rustc_never_returns_null_ptr] to std functions
Add the attribute to standard library functions that are guaranteed to never return null pointers, as their originating data wouldn't allow it.
1 parent 8cfa4fe commit 33970db

File tree

10 files changed

+22
-0
lines changed

10 files changed

+22
-0
lines changed

library/alloc/src/rc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
13041304
/// assert_eq!(unsafe { &*x_ptr }, "hello");
13051305
/// ```
13061306
#[stable(feature = "rc_raw", since = "1.17.0")]
1307+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
13071308
pub fn into_raw(this: Self) -> *const T {
13081309
let ptr = Self::as_ptr(&this);
13091310
mem::forget(this);
@@ -1327,6 +1328,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
13271328
/// assert_eq!(unsafe { &*x_ptr }, "hello");
13281329
/// ```
13291330
#[stable(feature = "weak_into_raw", since = "1.45.0")]
1331+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
13301332
pub fn as_ptr(this: &Self) -> *const T {
13311333
let ptr: *mut RcBox<T> = NonNull::as_ptr(this.ptr);
13321334

library/alloc/src/sync.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,7 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
14551455
/// ```
14561456
#[must_use = "losing the pointer will leak memory"]
14571457
#[stable(feature = "rc_raw", since = "1.17.0")]
1458+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
14581459
pub fn into_raw(this: Self) -> *const T {
14591460
let ptr = Self::as_ptr(&this);
14601461
mem::forget(this);
@@ -1479,6 +1480,7 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
14791480
/// ```
14801481
#[must_use]
14811482
#[stable(feature = "rc_as_ptr", since = "1.45.0")]
1483+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
14821484
pub fn as_ptr(this: &Self) -> *const T {
14831485
let ptr: *mut ArcInner<T> = NonNull::as_ptr(this.ptr);
14841486

library/alloc/src/vec/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,7 @@ impl<T, A: Allocator> Vec<T, A> {
12331233
///
12341234
/// [`as_mut_ptr`]: Vec::as_mut_ptr
12351235
#[stable(feature = "vec_as_ptr", since = "1.37.0")]
1236+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
12361237
#[inline]
12371238
pub fn as_ptr(&self) -> *const T {
12381239
// We shadow the slice method of the same name to avoid going through
@@ -1266,6 +1267,7 @@ impl<T, A: Allocator> Vec<T, A> {
12661267
/// assert_eq!(&*x, &[0, 1, 2, 3]);
12671268
/// ```
12681269
#[stable(feature = "vec_as_ptr", since = "1.37.0")]
1270+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
12691271
#[inline]
12701272
pub fn as_mut_ptr(&mut self) -> *mut T {
12711273
// We shadow the slice method of the same name to avoid going through

library/core/src/cell.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ impl<T: ?Sized> Cell<T> {
543543
#[inline]
544544
#[stable(feature = "cell_as_ptr", since = "1.12.0")]
545545
#[rustc_const_stable(feature = "const_cell_as_ptr", since = "1.32.0")]
546+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
546547
pub const fn as_ptr(&self) -> *mut T {
547548
self.value.get()
548549
}
@@ -1076,6 +1077,7 @@ impl<T: ?Sized> RefCell<T> {
10761077
/// ```
10771078
#[inline]
10781079
#[stable(feature = "cell_as_ptr", since = "1.12.0")]
1080+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
10791081
pub fn as_ptr(&self) -> *mut T {
10801082
self.value.get()
10811083
}
@@ -2071,6 +2073,7 @@ impl<T: ?Sized> UnsafeCell<T> {
20712073
#[inline(always)]
20722074
#[stable(feature = "rust1", since = "1.0.0")]
20732075
#[rustc_const_stable(feature = "const_unsafecell_get", since = "1.32.0")]
2076+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
20742077
pub const fn get(&self) -> *mut T {
20752078
// We can just cast the pointer from `UnsafeCell<T>` to `T` because of
20762079
// #[repr(transparent)]. This exploits std's special status, there is
@@ -2213,6 +2216,7 @@ impl<T: ?Sized> SyncUnsafeCell<T> {
22132216
/// when casting to `&mut T`, and ensure that there are no mutations
22142217
/// or mutable aliases going on when casting to `&T`
22152218
#[inline]
2219+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
22162220
pub const fn get(&self) -> *mut T {
22172221
self.value.get()
22182222
}

library/core/src/ffi/c_str.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ impl CStr {
509509
#[must_use]
510510
#[stable(feature = "rust1", since = "1.0.0")]
511511
#[rustc_const_stable(feature = "const_str_as_ptr", since = "1.32.0")]
512+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
512513
pub const fn as_ptr(&self) -> *const c_char {
513514
self.inner.as_ptr()
514515
}

library/core/src/ptr/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ where
698698
#[inline(always)]
699699
#[must_use]
700700
#[unstable(feature = "ptr_from_ref", issue = "106116")]
701+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
701702
#[rustc_diagnostic_item = "ptr_from_ref"]
702703
pub const fn from_ref<T: ?Sized>(r: &T) -> *const T {
703704
r
@@ -710,6 +711,7 @@ pub const fn from_ref<T: ?Sized>(r: &T) -> *const T {
710711
#[inline(always)]
711712
#[must_use]
712713
#[unstable(feature = "ptr_from_ref", issue = "106116")]
714+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
713715
#[rustc_diagnostic_item = "ptr_from_mut"]
714716
pub const fn from_mut<T: ?Sized>(r: &mut T) -> *mut T {
715717
r

library/core/src/ptr/non_null.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ impl<T: ?Sized> NonNull<T> {
320320
/// ```
321321
#[stable(feature = "nonnull", since = "1.25.0")]
322322
#[rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0")]
323+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
323324
#[must_use]
324325
#[inline(always)]
325326
pub const fn as_ptr(self) -> *mut T {
@@ -579,6 +580,7 @@ impl<T> NonNull<[T]> {
579580
#[must_use]
580581
#[unstable(feature = "slice_ptr_get", issue = "74265")]
581582
#[rustc_const_unstable(feature = "slice_ptr_get", issue = "74265")]
583+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
582584
pub const fn as_mut_ptr(self) -> *mut T {
583585
self.as_non_null_ptr().as_ptr()
584586
}

library/core/src/slice/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,7 @@ impl<T> [T] {
730730
/// [`as_mut_ptr`]: slice::as_mut_ptr
731731
#[stable(feature = "rust1", since = "1.0.0")]
732732
#[rustc_const_stable(feature = "const_slice_as_ptr", since = "1.32.0")]
733+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
733734
#[inline(always)]
734735
#[must_use]
735736
pub const fn as_ptr(&self) -> *const T {
@@ -760,6 +761,7 @@ impl<T> [T] {
760761
#[stable(feature = "rust1", since = "1.0.0")]
761762
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
762763
#[rustc_allow_const_fn_unstable(const_mut_refs)]
764+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
763765
#[inline(always)]
764766
#[must_use]
765767
pub const fn as_mut_ptr(&mut self) -> *mut T {

library/core/src/str/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ impl str {
387387
/// ```
388388
#[stable(feature = "rust1", since = "1.0.0")]
389389
#[rustc_const_stable(feature = "rustc_str_as_ptr", since = "1.32.0")]
390+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
390391
#[must_use]
391392
#[inline(always)]
392393
pub const fn as_ptr(&self) -> *const u8 {
@@ -402,6 +403,7 @@ impl str {
402403
/// It is your responsibility to make sure that the string slice only gets
403404
/// modified in a way that it remains valid UTF-8.
404405
#[stable(feature = "str_as_mut_ptr", since = "1.36.0")]
406+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
405407
#[must_use]
406408
#[inline(always)]
407409
pub fn as_mut_ptr(&mut self) -> *mut u8 {

library/core/src/sync/atomic.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,7 @@ impl AtomicBool {
10181018
#[inline]
10191019
#[stable(feature = "atomic_as_ptr", since = "1.70.0")]
10201020
#[rustc_const_stable(feature = "atomic_as_ptr", since = "1.70.0")]
1021+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
10211022
pub const fn as_ptr(&self) -> *mut bool {
10221023
self.v.get().cast()
10231024
}
@@ -1953,6 +1954,7 @@ impl<T> AtomicPtr<T> {
19531954
#[inline]
19541955
#[stable(feature = "atomic_as_ptr", since = "1.70.0")]
19551956
#[rustc_const_stable(feature = "atomic_as_ptr", since = "1.70.0")]
1957+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
19561958
pub const fn as_ptr(&self) -> *mut *mut T {
19571959
self.p.get()
19581960
}
@@ -2891,6 +2893,7 @@ macro_rules! atomic_int {
28912893
#[inline]
28922894
#[stable(feature = "atomic_as_ptr", since = "1.70.0")]
28932895
#[rustc_const_stable(feature = "atomic_as_ptr", since = "1.70.0")]
2896+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
28942897
pub const fn as_ptr(&self) -> *mut $int_type {
28952898
self.v.get()
28962899
}

0 commit comments

Comments
 (0)