Skip to content

Commit 17192b9

Browse files
committed
Auto merge of rust-lang#114494 - est31:extend_useless_ptr_null_checks, r=jackh726
Make useless_ptr_null_checks smarter about some std functions This teaches the `useless_ptr_null_checks` lint that some std functions can't ever return null pointers, because they need to point to valid data, get references as input, etc. This is achieved by introducing an `#[rustc_never_returns_null_ptr]` attribute and adding it to these std functions (gated behind bootstrap `cfg_attr`). Later on, the attribute could maybe be used to tell LLVM that the returned pointer is never null. I don't expect much impact of that though, as the functions are pretty shallow and usually the input data is already never null. Follow-up of PR rust-lang#113657 Fixes rust-lang#114442
2 parents 86b4bd9 + 5e734f8 commit 17192b9

File tree

10 files changed

+22
-1
lines changed

10 files changed

+22
-1
lines changed

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

alloc/src/sync.rs

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

alloc/src/vec/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,7 @@ impl<T, A: Allocator> Vec<T, A> {
12581258
/// [`as_mut_ptr`]: Vec::as_mut_ptr
12591259
/// [`as_ptr`]: Vec::as_ptr
12601260
#[stable(feature = "vec_as_ptr", since = "1.37.0")]
1261+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
12611262
#[inline]
12621263
pub fn as_ptr(&self) -> *const T {
12631264
// We shadow the slice method of the same name to avoid going through
@@ -1317,6 +1318,7 @@ impl<T, A: Allocator> Vec<T, A> {
13171318
/// [`as_mut_ptr`]: Vec::as_mut_ptr
13181319
/// [`as_ptr`]: Vec::as_ptr
13191320
#[stable(feature = "vec_as_ptr", since = "1.37.0")]
1321+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
13201322
#[inline]
13211323
pub fn as_mut_ptr(&mut self) -> *mut T {
13221324
// We shadow the slice method of the same name to avoid going through

core/src/cell.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ impl<T: ?Sized> Cell<T> {
556556
#[inline]
557557
#[stable(feature = "cell_as_ptr", since = "1.12.0")]
558558
#[rustc_const_stable(feature = "const_cell_as_ptr", since = "1.32.0")]
559+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
559560
pub const fn as_ptr(&self) -> *mut T {
560561
self.value.get()
561562
}
@@ -1111,6 +1112,7 @@ impl<T: ?Sized> RefCell<T> {
11111112
/// ```
11121113
#[inline]
11131114
#[stable(feature = "cell_as_ptr", since = "1.12.0")]
1115+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
11141116
pub fn as_ptr(&self) -> *mut T {
11151117
self.value.get()
11161118
}
@@ -2105,6 +2107,7 @@ impl<T: ?Sized> UnsafeCell<T> {
21052107
#[inline(always)]
21062108
#[stable(feature = "rust1", since = "1.0.0")]
21072109
#[rustc_const_stable(feature = "const_unsafecell_get", since = "1.32.0")]
2110+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
21082111
pub const fn get(&self) -> *mut T {
21092112
// We can just cast the pointer from `UnsafeCell<T>` to `T` because of
21102113
// #[repr(transparent)]. This exploits std's special status, there is
@@ -2248,6 +2251,7 @@ impl<T: ?Sized> SyncUnsafeCell<T> {
22482251
/// when casting to `&mut T`, and ensure that there are no mutations
22492252
/// or mutable aliases going on when casting to `&T`
22502253
#[inline]
2254+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
22512255
pub const fn get(&self) -> *mut T {
22522256
self.value.get()
22532257
}

core/src/ffi/c_str.rs

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

core/src/ptr/mod.rs

Lines changed: 2 additions & 1 deletion
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,7 +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")]
713-
#[rustc_diagnostic_item = "ptr_from_mut"]
714+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
714715
pub const fn from_mut<T: ?Sized>(r: &mut T) -> *mut T {
715716
r
716717
}

core/src/ptr/non_null.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ impl<T: ?Sized> NonNull<T> {
338338
/// ```
339339
#[stable(feature = "nonnull", since = "1.25.0")]
340340
#[rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0")]
341+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
341342
#[must_use]
342343
#[inline(always)]
343344
pub const fn as_ptr(self) -> *mut T {
@@ -597,6 +598,7 @@ impl<T> NonNull<[T]> {
597598
#[must_use]
598599
#[unstable(feature = "slice_ptr_get", issue = "74265")]
599600
#[rustc_const_unstable(feature = "slice_ptr_get", issue = "74265")]
601+
#[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
600602
pub const fn as_mut_ptr(self) -> *mut T {
601603
self.as_non_null_ptr().as_ptr()
602604
}

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 {

core/src/str/mod.rs

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

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)