Skip to content

Commit e26c298

Browse files
authored
Rollup merge of rust-lang#132732 - gavincrawford:as_ptr_attribute, r=Urgau
Use attributes for `dangling_pointers_from_temporaries` lint Checking for dangling pointers by function name isn't ideal, and leaves out certain pointer-returning methods that don't follow the `as_ptr` naming convention. Using an attribute for this lint cleans things up and allows more thorough coverage of other methods, such as `UnsafeCell::get()`.
2 parents d6ee9db + 50ac725 commit e26c298

File tree

7 files changed

+16
-0
lines changed

7 files changed

+16
-0
lines changed

alloc/src/boxed.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
15001500
/// [`as_ptr`]: Self::as_ptr
15011501
#[unstable(feature = "box_as_ptr", issue = "129090")]
15021502
#[rustc_never_returns_null_ptr]
1503+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
15031504
#[inline]
15041505
pub fn as_mut_ptr(b: &mut Self) -> *mut T {
15051506
// This is a primitive deref, not going through `DerefMut`, and therefore not materializing
@@ -1548,6 +1549,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
15481549
/// [`as_ptr`]: Self::as_ptr
15491550
#[unstable(feature = "box_as_ptr", issue = "129090")]
15501551
#[rustc_never_returns_null_ptr]
1552+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
15511553
#[inline]
15521554
pub fn as_ptr(b: &Self) -> *const T {
15531555
// This is a primitive deref, not going through `DerefMut`, and therefore not materializing

alloc/src/vec/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,7 @@ impl<T, A: Allocator> Vec<T, A> {
16621662
#[stable(feature = "vec_as_ptr", since = "1.37.0")]
16631663
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
16641664
#[rustc_never_returns_null_ptr]
1665+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
16651666
#[inline]
16661667
pub const fn as_ptr(&self) -> *const T {
16671668
// We shadow the slice method of the same name to avoid going through
@@ -1724,6 +1725,7 @@ impl<T, A: Allocator> Vec<T, A> {
17241725
#[stable(feature = "vec_as_ptr", since = "1.37.0")]
17251726
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
17261727
#[rustc_never_returns_null_ptr]
1728+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
17271729
#[inline]
17281730
pub const fn as_mut_ptr(&mut self) -> *mut T {
17291731
// We shadow the slice method of the same name to avoid going through

core/src/cell.rs

+5
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ impl<T: ?Sized> Cell<T> {
587587
#[inline]
588588
#[stable(feature = "cell_as_ptr", since = "1.12.0")]
589589
#[rustc_const_stable(feature = "const_cell_as_ptr", since = "1.32.0")]
590+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
590591
#[rustc_never_returns_null_ptr]
591592
pub const fn as_ptr(&self) -> *mut T {
592593
self.value.get()
@@ -1149,6 +1150,7 @@ impl<T: ?Sized> RefCell<T> {
11491150
/// ```
11501151
#[inline]
11511152
#[stable(feature = "cell_as_ptr", since = "1.12.0")]
1153+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
11521154
#[rustc_never_returns_null_ptr]
11531155
pub fn as_ptr(&self) -> *mut T {
11541156
self.value.get()
@@ -2158,6 +2160,7 @@ impl<T: ?Sized> UnsafeCell<T> {
21582160
#[inline(always)]
21592161
#[stable(feature = "rust1", since = "1.0.0")]
21602162
#[rustc_const_stable(feature = "const_unsafecell_get", since = "1.32.0")]
2163+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
21612164
#[rustc_never_returns_null_ptr]
21622165
pub const fn get(&self) -> *mut T {
21632166
// We can just cast the pointer from `UnsafeCell<T>` to `T` because of
@@ -2271,6 +2274,7 @@ impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<UnsafeCell<U>> for UnsafeCell<T>
22712274
/// See [`UnsafeCell`] for details.
22722275
#[unstable(feature = "sync_unsafe_cell", issue = "95439")]
22732276
#[repr(transparent)]
2277+
#[rustc_diagnostic_item = "SyncUnsafeCell"]
22742278
#[rustc_pub_transparent]
22752279
pub struct SyncUnsafeCell<T: ?Sized> {
22762280
value: UnsafeCell<T>,
@@ -2304,6 +2308,7 @@ impl<T: ?Sized> SyncUnsafeCell<T> {
23042308
/// when casting to `&mut T`, and ensure that there are no mutations
23052309
/// or mutable aliases going on when casting to `&T`
23062310
#[inline]
2311+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
23072312
#[rustc_never_returns_null_ptr]
23082313
pub const fn get(&self) -> *mut T {
23092314
self.value.get()

core/src/ffi/c_str.rs

+1
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ impl CStr {
500500
#[must_use]
501501
#[stable(feature = "rust1", since = "1.0.0")]
502502
#[rustc_const_stable(feature = "const_str_as_ptr", since = "1.32.0")]
503+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
503504
#[rustc_never_returns_null_ptr]
504505
pub const fn as_ptr(&self) -> *const c_char {
505506
self.inner.as_ptr()

core/src/mem/maybe_uninit.rs

+2
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ impl<T> MaybeUninit<T> {
525525
/// until they are, it is advisable to avoid them.)
526526
#[stable(feature = "maybe_uninit", since = "1.36.0")]
527527
#[rustc_const_stable(feature = "const_maybe_uninit_as_ptr", since = "1.59.0")]
528+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
528529
#[inline(always)]
529530
pub const fn as_ptr(&self) -> *const T {
530531
// `MaybeUninit` and `ManuallyDrop` are both `repr(transparent)` so we can cast the pointer.
@@ -566,6 +567,7 @@ impl<T> MaybeUninit<T> {
566567
/// until they are, it is advisable to avoid them.)
567568
#[stable(feature = "maybe_uninit", since = "1.36.0")]
568569
#[rustc_const_stable(feature = "const_maybe_uninit_as_mut_ptr", since = "1.83.0")]
570+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
569571
#[inline(always)]
570572
pub const fn as_mut_ptr(&mut self) -> *mut T {
571573
// `MaybeUninit` and `ManuallyDrop` are both `repr(transparent)` so we can cast the pointer.

core/src/slice/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ impl<T> [T] {
735735
#[stable(feature = "rust1", since = "1.0.0")]
736736
#[rustc_const_stable(feature = "const_slice_as_ptr", since = "1.32.0")]
737737
#[rustc_never_returns_null_ptr]
738+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
738739
#[inline(always)]
739740
#[must_use]
740741
pub const fn as_ptr(&self) -> *const T {
@@ -765,6 +766,7 @@ impl<T> [T] {
765766
#[stable(feature = "rust1", since = "1.0.0")]
766767
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
767768
#[rustc_never_returns_null_ptr]
769+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
768770
#[inline(always)]
769771
#[must_use]
770772
pub const fn as_mut_ptr(&mut self) -> *mut T {

core/src/str/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ impl str {
373373
#[stable(feature = "rust1", since = "1.0.0")]
374374
#[rustc_const_stable(feature = "rustc_str_as_ptr", since = "1.32.0")]
375375
#[rustc_never_returns_null_ptr]
376+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
376377
#[must_use]
377378
#[inline(always)]
378379
pub const fn as_ptr(&self) -> *const u8 {
@@ -390,6 +391,7 @@ impl str {
390391
#[stable(feature = "str_as_mut_ptr", since = "1.36.0")]
391392
#[rustc_const_stable(feature = "const_str_as_mut", since = "1.83.0")]
392393
#[rustc_never_returns_null_ptr]
394+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
393395
#[must_use]
394396
#[inline(always)]
395397
pub const fn as_mut_ptr(&mut self) -> *mut u8 {

0 commit comments

Comments
 (0)