Skip to content

Commit f2becdf

Browse files
committed
Auto merge of #130865 - cuviper:library-raw_ref_op, r=tgross35
Use `&raw` in the standard library Since the stabilization in #127679 has reached stage0, 1.82-beta, we can start using `&raw` freely, and even the soft-deprecated `ptr::addr_of!` and `ptr::addr_of_mut!` can stop allowing the unstable feature. I intentionally did not change any documentation or tests, but the rest of those macro uses are all now using `&raw const` or `&raw mut` in the standard library.
2 parents 76ed7a1 + f4d9d1a commit f2becdf

51 files changed

Lines changed: 150 additions & 185 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎library/alloc/src/boxed.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ use core::ops::{
199199
DerefPure,DispatchFromDyn,Receiver,
200200
};
201201
use core::pin::{Pin,PinCoerceUnsized};
202-
use core::ptr::{self,NonNull,Unique, addr_of_mut};
202+
use core::ptr::{self,NonNull,Unique};
203203
use core::task::{Context,Poll};
204204
use core::{borrow, fmt, slice};
205205

@@ -1277,7 +1277,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
12771277
#[inline]
12781278
pubfninto_raw(b:Self) -> *mutT{
12791279
// Make sure Miri realizes that we transition from a noalias pointer to a raw pointer here.
1280-
unsafe{addr_of_mut!(*&mut*Self::into_raw_with_allocator(b).0)}
1280+
unsafe{&rawmut*&mut*Self::into_raw_with_allocator(b).0}
12811281
}
12821282

12831283
/// Consumes the `Box`, returning a wrapped `NonNull` pointer.
@@ -1396,7 +1396,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
13961396
// want *no* aliasing requirements here!
13971397
// In case `A` *is* `Global`, this does not quite have the right behavior; `into_raw`
13981398
// works around that.
1399-
let ptr = addr_of_mut!(**b);
1399+
let ptr = &rawmut**b;
14001400
let alloc = unsafe{ ptr::read(&b.1)};
14011401
(ptr, alloc)
14021402
}
@@ -1506,7 +1506,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
15061506
pubfnas_mut_ptr(b:&mutSelf) -> *mutT{
15071507
// This is a primitive deref, not going through `DerefMut`, and therefore not materializing
15081508
// any references.
1509-
ptr::addr_of_mut!(**b)
1509+
&rawmut**b
15101510
}
15111511

15121512
/// Returns a raw pointer to the `Box`'s contents.
@@ -1554,7 +1554,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
15541554
pubfnas_ptr(b:&Self) -> *constT{
15551555
// This is a primitive deref, not going through `DerefMut`, and therefore not materializing
15561556
// any references.
1557-
ptr::addr_of!(**b)
1557+
&rawconst**b
15581558
}
15591559

15601560
/// Returns a reference to the underlying allocator.

‎library/alloc/src/boxed/thin.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl<T: ?Sized> ThinBox<T> {
186186

187187
fnwith_header(&self) -> &WithHeader<<TasPointee>::Metadata>{
188188
// SAFETY: both types are transparent to `NonNull<u8>`
189-
unsafe{&*(core::ptr::addr_of!(self.ptr)as*constWithHeader<_>)}
189+
unsafe{&*((&rawconstself.ptr)as*constWithHeader<_>)}
190190
}
191191
}
192192

‎library/alloc/src/collections/btree/node.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ impl<K, V> LeafNode<K, V> {
7272
// be both slightly faster and easier to track in Valgrind.
7373
unsafe{
7474
// parent_idx, keys, and vals are all MaybeUninit
75-
ptr::addr_of_mut!((*this).parent).write(None);
76-
ptr::addr_of_mut!((*this).len).write(0);
75+
(&rawmut(*this).parent).write(None);
76+
(&rawmut(*this).len).write(0);
7777
}
7878
}
7979

@@ -114,7 +114,7 @@ impl<K, V> InternalNode<K, V> {
114114
unsafe{
115115
letmut node = Box::<Self,_>::new_uninit_in(alloc);
116116
// We only need to initialize the data; the edges are MaybeUninit.
117-
LeafNode::init(ptr::addr_of_mut!((*node.as_mut_ptr()).data));
117+
LeafNode::init(&rawmut(*node.as_mut_ptr()).data);
118118
node.assume_init()
119119
}
120120
}
@@ -525,8 +525,8 @@ impl<'a, K, V, Type> NodeRef<marker::ValMut<'a>, K, V, Type> {
525525
// to avoid aliasing with outstanding references to other elements,
526526
// in particular, those returned to the caller in earlier iterations.
527527
let leaf = Self::as_leaf_ptr(&mutself);
528-
let keys = unsafe{ptr::addr_of!((*leaf).keys)};
529-
let vals = unsafe{ptr::addr_of_mut!((*leaf).vals)};
528+
let keys = unsafe{&rawconst(*leaf).keys};
529+
let vals = unsafe{&rawmut(*leaf).vals};
530530
// We must coerce to unsized array pointers because of Rust issue #74679.
531531
let keys:*const[_] = keys;
532532
let vals:*mut[_] = vals;

‎library/alloc/src/rc.rs‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ impl<T, A: Allocator> Rc<T, A> {
787787

788788
let strong = unsafe{
789789
let inner = init_ptr.as_ptr();
790-
ptr::write(ptr::addr_of_mut!((*inner).value), data);
790+
ptr::write(&rawmut(*inner).value, data);
791791

792792
let prev_value = (*inner).strong.get();
793793
debug_assert_eq!(prev_value,0,"No prior strong references should exist");
@@ -1442,7 +1442,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
14421442
// SAFETY: This cannot go through Deref::deref or Rc::inner because
14431443
// this is required to retain raw/mut provenance such that e.g. `get_mut` can
14441444
// write through the pointer after the Rc is recovered through `from_raw`.
1445-
unsafe{ptr::addr_of_mut!((*ptr).value)}
1445+
unsafe{&rawmut(*ptr).value}
14461446
}
14471447

14481448
/// Constructs an `Rc<T, A>` from a raw pointer in the provided allocator.
@@ -2042,8 +2042,8 @@ impl<T: ?Sized> Rc<T> {
20422042
unsafe{
20432043
debug_assert_eq!(Layout::for_value_raw(inner), layout);
20442044

2045-
ptr::addr_of_mut!((*inner).strong).write(Cell::new(1));
2046-
ptr::addr_of_mut!((*inner).weak).write(Cell::new(1));
2045+
(&rawmut(*inner).strong).write(Cell::new(1));
2046+
(&rawmut(*inner).weak).write(Cell::new(1));
20472047
}
20482048

20492049
Ok(inner)
@@ -2072,8 +2072,8 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
20722072

20732073
// Copy value as bytes
20742074
ptr::copy_nonoverlapping(
2075-
core::ptr::addr_of!(*src)as*constu8,
2076-
ptr::addr_of_mut!((*ptr).value)as*mutu8,
2075+
(&rawconst*src)as*constu8,
2076+
(&rawmut(*ptr).value)as*mutu8,
20772077
value_size,
20782078
);
20792079

@@ -2107,11 +2107,7 @@ impl<T> Rc<[T]> {
21072107
unsafefncopy_from_slice(v:&[T]) -> Rc<[T]>{
21082108
unsafe{
21092109
let ptr = Self::allocate_for_slice(v.len());
2110-
ptr::copy_nonoverlapping(
2111-
v.as_ptr(),
2112-
ptr::addr_of_mut!((*ptr).value)as*mutT,
2113-
v.len(),
2114-
);
2110+
ptr::copy_nonoverlapping(v.as_ptr(),(&rawmut(*ptr).value)as*mutT, v.len());
21152111
Self::from_ptr(ptr)
21162112
}
21172113
}
@@ -2149,7 +2145,7 @@ impl<T> Rc<[T]> {
21492145
let layout = Layout::for_value_raw(ptr);
21502146

21512147
// Pointer to first element
2152-
let elems = ptr::addr_of_mut!((*ptr).value)as*mutT;
2148+
let elems = (&rawmut(*ptr).value)as*mutT;
21532149

21542150
letmut guard = Guard{mem:NonNull::new_unchecked(mem), elems, layout,n_elems:0};
21552151

@@ -2577,7 +2573,7 @@ impl<T: ?Sized + fmt::Debug, A: Allocator> fmt::Debug for Rc<T, A> {
25772573
#[stable(feature = "rust1", since = "1.0.0")]
25782574
impl<T: ?Sized,A:Allocator> fmt::PointerforRc<T,A>{
25792575
fnfmt(&self,f:&mut fmt::Formatter<'_>) -> fmt::Result{
2580-
fmt::Pointer::fmt(&core::ptr::addr_of!(**self), f)
2576+
fmt::Pointer::fmt(&(&rawconst**self), f)
25812577
}
25822578
}
25832579

@@ -2718,7 +2714,7 @@ impl<T, A: Allocator> From<Vec<T, A>> for Rc<[T], A> {
27182714
let(vec_ptr, len, cap, alloc) = v.into_raw_parts_with_alloc();
27192715

27202716
let rc_ptr = Self::allocate_for_slice_in(len,&alloc);
2721-
ptr::copy_nonoverlapping(vec_ptr,ptr::addr_of_mut!((*rc_ptr).value)as*mutT, len);
2717+
ptr::copy_nonoverlapping(vec_ptr,(&rawmut(*rc_ptr).value)as*mutT, len);
27222718

27232719
// Create a `Vec<T, &A>` with length 0, to deallocate the buffer
27242720
// without dropping its contents or the allocator
@@ -3084,7 +3080,7 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> {
30843080
// SAFETY: if is_dangling returns false, then the pointer is dereferenceable.
30853081
// The payload may be dropped at this point, and we have to maintain provenance,
30863082
// so use raw pointer manipulation.
3087-
unsafe{ptr::addr_of_mut!((*ptr).value)}
3083+
unsafe{&rawmut(*ptr).value}
30883084
}
30893085
}
30903086

‎library/alloc/src/sync.rs‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ impl<T, A: Allocator> Arc<T, A> {
797797
// reference into a strong reference.
798798
let strong = unsafe{
799799
let inner = init_ptr.as_ptr();
800-
ptr::write(ptr::addr_of_mut!((*inner).data), data);
800+
ptr::write(&rawmut(*inner).data, data);
801801

802802
// The above write to the data field must be visible to any threads which
803803
// observe a non-zero strong count. Therefore we need at least "Release" ordering
@@ -1583,7 +1583,7 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
15831583
// SAFETY: This cannot go through Deref::deref or RcBoxPtr::inner because
15841584
// this is required to retain raw/mut provenance such that e.g. `get_mut` can
15851585
// write through the pointer after the Rc is recovered through `from_raw`.
1586-
unsafe{ptr::addr_of_mut!((*ptr).data)}
1586+
unsafe{&rawmut(*ptr).data}
15871587
}
15881588

15891589
/// Constructs an `Arc<T, A>` from a raw pointer.
@@ -1955,8 +1955,8 @@ impl<T: ?Sized> Arc<T> {
19551955
debug_assert_eq!(unsafe{Layout::for_value_raw(inner)}, layout);
19561956

19571957
unsafe{
1958-
ptr::addr_of_mut!((*inner).strong).write(atomic::AtomicUsize::new(1));
1959-
ptr::addr_of_mut!((*inner).weak).write(atomic::AtomicUsize::new(1));
1958+
(&rawmut(*inner).strong).write(atomic::AtomicUsize::new(1));
1959+
(&rawmut(*inner).weak).write(atomic::AtomicUsize::new(1));
19601960
}
19611961

19621962
inner
@@ -1986,8 +1986,8 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
19861986

19871987
// Copy value as bytes
19881988
ptr::copy_nonoverlapping(
1989-
core::ptr::addr_of!(*src)as*constu8,
1990-
ptr::addr_of_mut!((*ptr).data)as*mutu8,
1989+
(&rawconst*src)as*constu8,
1990+
(&rawmut(*ptr).data)as*mutu8,
19911991
value_size,
19921992
);
19931993

@@ -2022,7 +2022,7 @@ impl<T> Arc<[T]> {
20222022
unsafe{
20232023
let ptr = Self::allocate_for_slice(v.len());
20242024

2025-
ptr::copy_nonoverlapping(v.as_ptr(),ptr::addr_of_mut!((*ptr).data)as*mutT, v.len());
2025+
ptr::copy_nonoverlapping(v.as_ptr(),(&rawmut(*ptr).data)as*mutT, v.len());
20262026

20272027
Self::from_ptr(ptr)
20282028
}
@@ -2061,7 +2061,7 @@ impl<T> Arc<[T]> {
20612061
let layout = Layout::for_value_raw(ptr);
20622062

20632063
// Pointer to first element
2064-
let elems = ptr::addr_of_mut!((*ptr).data)as*mutT;
2064+
let elems = (&rawmut(*ptr).data)as*mutT;
20652065

20662066
letmut guard = Guard{mem:NonNull::new_unchecked(mem), elems, layout,n_elems:0};
20672067

@@ -2805,7 +2805,7 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> {
28052805
// SAFETY: if is_dangling returns false, then the pointer is dereferenceable.
28062806
// The payload may be dropped at this point, and we have to maintain provenance,
28072807
// so use raw pointer manipulation.
2808-
unsafe{ptr::addr_of_mut!((*ptr).data)}
2808+
unsafe{&rawmut(*ptr).data}
28092809
}
28102810
}
28112811

@@ -3428,7 +3428,7 @@ impl<T: ?Sized + fmt::Debug, A: Allocator> fmt::Debug for Arc<T, A> {
34283428
#[stable(feature = "rust1", since = "1.0.0")]
34293429
impl<T: ?Sized,A:Allocator> fmt::PointerforArc<T,A>{
34303430
fnfmt(&self,f:&mut fmt::Formatter<'_>) -> fmt::Result{
3431-
fmt::Pointer::fmt(&core::ptr::addr_of!(**self), f)
3431+
fmt::Pointer::fmt(&(&rawconst**self), f)
34323432
}
34333433
}
34343434

@@ -3678,7 +3678,7 @@ impl<T, A: Allocator + Clone> From<Vec<T, A>> for Arc<[T], A> {
36783678
let(vec_ptr, len, cap, alloc) = v.into_raw_parts_with_alloc();
36793679

36803680
let rc_ptr = Self::allocate_for_slice_in(len,&alloc);
3681-
ptr::copy_nonoverlapping(vec_ptr,ptr::addr_of_mut!((*rc_ptr).data)as*mutT, len);
3681+
ptr::copy_nonoverlapping(vec_ptr,(&rawmut(*rc_ptr).data)as*mutT, len);
36823682

36833683
// Create a `Vec<T, &A>` with length 0, to deallocate the buffer
36843684
// without dropping its contents or the allocator

‎library/alloc/src/vec/into_iter.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ use crate::raw_vec::RawVec;
2121
macro non_null {
2222
(mut $place:expr, $t:ident) => {{
2323
#![allow(unused_unsafe)]// we're sometimes used within an unsafe block
24-
unsafe{&mut*(ptr::addr_of_mut!($place)as*mutNonNull<$t>)}
24+
unsafe{&mut*((&rawmut$place)as*mutNonNull<$t>)}
2525
}},
2626
($place:expr, $t:ident) => {{
2727
#![allow(unused_unsafe)]// we're sometimes used within an unsafe block
28-
unsafe{*(ptr::addr_of!($place)as*constNonNull<$t>)}
28+
unsafe{*((&rawconst$place)as*constNonNull<$t>)}
2929
}},
3030
}
3131

‎library/core/src/ffi/c_str.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::error::Error;
55
usecrate::ffi::c_char;
66
usecrate::iter::FusedIterator;
77
usecrate::marker::PhantomData;
8-
usecrate::ptr::{NonNull, addr_of};
8+
usecrate::ptr::NonNull;
99
usecrate::slice::memchr;
1010
usecrate::{fmt, intrinsics, ops, slice, str};
1111

@@ -623,7 +623,7 @@ impl CStr {
623623
pubconstfnto_bytes_with_nul(&self) -> &[u8]{
624624
// SAFETY: Transmuting a slice of `c_char`s to a slice of `u8`s
625625
// is safe on all supported targets.
626-
unsafe{&*(addr_of!(self.inner)as*const[u8])}
626+
unsafe{&*((&rawconstself.inner)as*const[u8])}
627627
}
628628

629629
/// Iterates over the bytes in this C string.

‎library/core/src/iter/adapters/filter_map.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::iter::{FusedIterator, InPlaceIterable, TrustedFused};
33
usecrate::mem::{ManuallyDrop,MaybeUninit};
44
usecrate::num::NonZero;
55
usecrate::ops::{ControlFlow,Try};
6-
usecrate::ptr::addr_of;
76
usecrate::{array, fmt};
87

98
/// An iterator that uses `f` to both filter and map elements from `iter`.
@@ -101,7 +100,7 @@ where
101100

102101
unsafe{
103102
let opt_payload_at:*constMaybeUninit<B> =
104-
addr_of!(val).byte_add(core::mem::offset_of!(Option<B>,Some.0)).cast();
103+
(&rawconstval).byte_add(core::mem::offset_of!(Option<B>,Some.0)).cast();
105104
let dst = guard.array.as_mut_ptr().add(idx);
106105
crate::ptr::copy_nonoverlapping(opt_payload_at, dst,1);
107106
crate::mem::forget(val);

‎library/core/src/ptr/mod.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,7 @@ pub const unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
17301730
// `dst` cannot overlap `src` because the caller has mutable access
17311731
// to `dst` while `src` is owned by this function.
17321732
unsafe{
1733-
copy_nonoverlapping(addr_of!(src)as*constu8, dst as*mutu8, mem::size_of::<T>());
1733+
copy_nonoverlapping((&rawconstsrc)as*constu8, dst as*mutu8, mem::size_of::<T>());
17341734
// We are calling the intrinsic directly to avoid function calls in the generated code.
17351735
intrinsics::forget(src);
17361736
}
@@ -2348,7 +2348,6 @@ impl<F: FnPtr> fmt::Debug for F {
23482348
/// no difference whether the pointer is null or dangling.)
23492349
#[stable(feature = "raw_ref_macros", since = "1.51.0")]
23502350
#[rustc_macro_transparency = "semitransparent"]
2351-
#[allow_internal_unstable(raw_ref_op)]
23522351
pub macro addr_of($place:expr){
23532352
&rawconst $place
23542353
}
@@ -2439,7 +2438,6 @@ pub macro addr_of($place:expr) {
24392438
/// makes no difference whether the pointer is null or dangling.)
24402439
#[stable(feature = "raw_ref_macros", since = "1.51.0")]
24412440
#[rustc_macro_transparency = "semitransparent"]
2442-
#[allow_internal_unstable(raw_ref_op)]
24432441
pub macro addr_of_mut($place:expr){
24442442
&rawmut $place
24452443
}

‎library/core/src/slice/iter.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::iter::{
1111
usecrate::marker::PhantomData;
1212
usecrate::mem::{self,SizedTypeProperties};
1313
usecrate::num::NonZero;
14-
usecrate::ptr::{self,NonNull, without_provenance, without_provenance_mut};
14+
usecrate::ptr::{NonNull, without_provenance, without_provenance_mut};
1515
usecrate::{cmp, fmt};
1616

1717
#[stable(feature = "boxed_slice_into_iter", since = "1.80.0")]

0 commit comments

Comments
 (0)