diff --git a/library/alloc/src/collections/btree/node.rs b/library/alloc/src/collections/btree/node.rs index 67c4f09306d1960ff13c09752b41261420c74896..1d632512c78b46785a13e34ead7a81d76a732baa 100644 --- a/library/alloc/src/collections/btree/node.rs +++ b/library/alloc/src/collections/btree/node.rs @@ -545,8 +545,8 @@ unsafe fn into_key_val_mut_at(mut self, idx: usize) -> (&'a K, &'a mut V) { // to avoid aliasing with outstanding references to other elements, // in particular, those returned to the caller in earlier iterations. let leaf = Self::as_leaf_ptr(&mut self); - let keys = unsafe { &raw const (*leaf).keys }; - let vals = unsafe { &raw mut (*leaf).vals }; + let keys = unsafe { ptr::addr_of!((*leaf).keys) }; + let vals = unsafe { ptr::addr_of_mut!((*leaf).vals) }; // We must coerce to unsized array pointers because of Rust issue #74679. let keys: *const [_] = keys; let vals: *mut [_] = vals; diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 5868f2b5b6562d48cc553e8ce267c08eda92afa1..0f9c5af4744190a8ac149683b47732e4673043b5 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -116,7 +116,6 @@ #![feature(pattern)] #![feature(ptr_internals)] #![feature(range_bounds_assert_len)] -#![feature(raw_ref_op)] #![feature(rustc_attrs)] #![feature(receiver_trait)] #![cfg_attr(bootstrap, feature(min_const_generics))] diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index ee03f15eece3f94225f630cc326efb242bc2336a..f67f5fc533b49f686541cdff2001dd6d5838acb8 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -398,7 +398,7 @@ pub fn new_cyclic(data_fn: impl FnOnce(&Weak) -> T) -> Rc { unsafe { let inner = init_ptr.as_ptr(); - ptr::write(&raw mut (*inner).value, data); + ptr::write(ptr::addr_of_mut!((*inner).value), data); let prev_value = (*inner).strong.get(); debug_assert_eq!(prev_value, 0, "No prior strong references should exist"); @@ -804,7 +804,7 @@ pub fn as_ptr(this: &Self) -> *const T { // SAFETY: This cannot go through Deref::deref or Rc::inner because // this is required to retain raw/mut provenance such that e.g. `get_mut` can // write through the pointer after the Rc is recovered through `from_raw`. - unsafe { &raw const (*ptr).value } + unsafe { ptr::addr_of_mut!((*ptr).value) } } /// Constructs an `Rc` from a raw pointer. @@ -1917,7 +1917,7 @@ pub fn as_ptr(&self) -> *const T { // SAFETY: if is_dangling returns false, then the pointer is dereferencable. // The payload may be dropped at this point, and we have to maintain provenance, // so use raw pointer manipulation. - unsafe { &raw const (*ptr).value } + unsafe { ptr::addr_of_mut!((*ptr).value) } } } diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index c0d684fbb45730a0e307f944e48f7a0999c83e45..d0081097fe10a663e5d4ce2fa04b5a41a36b36da 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -384,7 +384,7 @@ pub fn new_cyclic(data_fn: impl FnOnce(&Weak) -> T) -> Arc { // reference into a strong reference. unsafe { let inner = init_ptr.as_ptr(); - ptr::write(&raw mut (*inner).data, data); + ptr::write(ptr::addr_of_mut!((*inner).data), data); // The above write to the data field must be visible to any threads which // observe a non-zero strong count. Therefore we need at least "Release" ordering @@ -800,7 +800,7 @@ pub fn as_ptr(this: &Self) -> *const T { // SAFETY: This cannot go through Deref::deref or RcBoxPtr::inner because // this is required to retain raw/mut provenance such that e.g. `get_mut` can // write through the pointer after the Rc is recovered through `from_raw`. - unsafe { &raw const (*ptr).data } + unsafe { ptr::addr_of_mut!((*ptr).data) } } /// Constructs an `Arc` from a raw pointer. @@ -1677,7 +1677,7 @@ pub fn as_ptr(&self) -> *const T { // SAFETY: if is_dangling returns false, then the pointer is dereferencable. // The payload may be dropped at this point, and we have to maintain provenance, // so use raw pointer manipulation. - unsafe { &raw mut (*ptr).data } + unsafe { ptr::addr_of_mut!((*ptr).data) } } } diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index a4395ab57e8a1edf5787186e900585d3862be171..a69b840e4bf147a0c04f80eb0ae6a61f0d0b665c 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -126,7 +126,6 @@ #![feature(auto_traits)] #![feature(or_patterns)] #![feature(prelude_import)] -#![feature(raw_ref_macros)] #![feature(repr_simd, platform_intrinsics)] #![feature(rustc_attrs)] #![feature(simd_ffi)] diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index 8d901c08f91a36cd966b81b7401578ffaee10cd9..c0108c0f82e81e2a195592698d6f355e617d50e7 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -1501,7 +1501,6 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// # Example /// /// ``` -/// #![feature(raw_ref_macros)] /// use std::ptr; /// /// #[repr(packed)] @@ -1512,14 +1511,14 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// /// let packed = Packed { f1: 1, f2: 2 }; /// // `&packed.f2` would create an unaligned reference, and thus be Undefined Behavior! -/// let raw_f2 = ptr::raw_const!(packed.f2); +/// let raw_f2 = ptr::addr_of!(packed.f2); /// assert_eq!(unsafe { raw_f2.read_unaligned() }, 2); /// ``` -#[unstable(feature = "raw_ref_macros", issue = "73394")] +#[stable(feature = "raw_ref_macros", since = "1.51.0")] #[rustc_macro_transparency = "semitransparent"] #[allow_internal_unstable(raw_ref_op)] -pub macro raw_const($e:expr) { - &raw const $e +pub macro addr_of($place:expr) { + &raw const $place } /// Create a `mut` raw pointer to a place, without creating an intermediate reference. @@ -1534,7 +1533,6 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// # Example /// /// ``` -/// #![feature(raw_ref_macros)] /// use std::ptr; /// /// #[repr(packed)] @@ -1545,13 +1543,13 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// /// let mut packed = Packed { f1: 1, f2: 2 }; /// // `&mut packed.f2` would create an unaligned reference, and thus be Undefined Behavior! -/// let raw_f2 = ptr::raw_mut!(packed.f2); +/// let raw_f2 = ptr::addr_of_mut!(packed.f2); /// unsafe { raw_f2.write_unaligned(42); } /// assert_eq!({packed.f2}, 42); // `{...}` forces copying the field instead of creating a reference. /// ``` -#[unstable(feature = "raw_ref_macros", issue = "73394")] +#[stable(feature = "raw_ref_macros", since = "1.51.0")] #[rustc_macro_transparency = "semitransparent"] #[allow_internal_unstable(raw_ref_op)] -pub macro raw_mut($e:expr) { - &raw mut $e +pub macro addr_of_mut($place:expr) { + &raw mut $place } diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index b06b6e93373f3235e9f4d8c839d31e89f3861cc2..315df83115d8cc370a7e3f4786915e38d29c8c65 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -543,8 +543,8 @@ pub const fn as_mut_ptr_range(&mut self) -> Range<*mut T> { #[inline] pub fn swap(&mut self, a: usize, b: usize) { // Can't take two mutable loans from one vector, so instead use raw pointers. - let pa = ptr::raw_mut!(self[a]); - let pb = ptr::raw_mut!(self[b]); + let pa = ptr::addr_of_mut!(self[a]); + let pb = ptr::addr_of_mut!(self[b]); // SAFETY: `pa` and `pb` have been created from safe mutable references and refer // to elements in the slice and therefore are guaranteed to be valid and aligned. // Note that accessing the elements behind `a` and `b` is checked and will diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 09a05bc1b45e139237815272255bf534e7bac5d1..c1b79ff716c725af30fe8e271670e08ff44e4b09 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -299,7 +299,6 @@ #![feature(prelude_import)] #![feature(ptr_internals)] #![feature(raw)] -#![feature(raw_ref_macros)] #![feature(ready_macro)] #![feature(rustc_attrs)] #![feature(rustc_private)] diff --git a/src/test/ui/consts/min_const_fn/allow_raw_ptr_dereference_const_fn.rs b/src/test/ui/consts/min_const_fn/allow_raw_ptr_dereference_const_fn.rs index 25dc457d144551e1046592c68e402041870cc384..f4279e6b825e2d26ef15a75bef7d471b250f2558 100644 --- a/src/test/ui/consts/min_const_fn/allow_raw_ptr_dereference_const_fn.rs +++ b/src/test/ui/consts/min_const_fn/allow_raw_ptr_dereference_const_fn.rs @@ -1,11 +1,10 @@ // check-pass #![feature(const_raw_ptr_deref)] -#![feature(raw_ref_macros)] use std::ptr; const fn test_fn(x: *const i32) { - let x2 = unsafe { ptr::raw_const!(*x) }; + let x2 = unsafe { ptr::addr_of!(*x) }; } fn main() {} diff --git a/src/test/ui/consts/ptr_comparisons.rs b/src/test/ui/consts/ptr_comparisons.rs index 595ed30bf9c8209b9c7484cf244d13507eaf1467..f16f6fd6de4bae8a247387fc5bc59bfc08d16ca7 100644 --- a/src/test/ui/consts/ptr_comparisons.rs +++ b/src/test/ui/consts/ptr_comparisons.rs @@ -9,8 +9,7 @@ core_intrinsics, const_raw_ptr_comparison, const_ptr_offset, - const_raw_ptr_deref, - raw_ref_macros + const_raw_ptr_deref )] const FOO: &usize = &42; @@ -64,7 +63,7 @@ const _: *const u8 = //~^ NOTE - unsafe { std::ptr::raw_const!((*(FOO as *const usize as *const [u8; 1000]))[999]) }; + unsafe { std::ptr::addr_of!((*(FOO as *const usize as *const [u8; 1000]))[999]) }; //~^ ERROR any use of this value will cause an error //~| NOTE diff --git a/src/test/ui/consts/ptr_comparisons.stderr b/src/test/ui/consts/ptr_comparisons.stderr index 49511b84500de92fdd379c473cf4a5a0db96a96f..96b63c0acb0a108e19592ceec05cfd3670f8ce30 100644 --- a/src/test/ui/consts/ptr_comparisons.stderr +++ b/src/test/ui/consts/ptr_comparisons.stderr @@ -6,9 +6,9 @@ LL | unsafe { intrinsics::offset(self, count) } | | | inbounds test failed: pointer must be in-bounds at offset $TWO_WORDS, but is outside bounds of alloc2 which has size $WORD | inside `ptr::const_ptr::::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL - | inside `_` at $DIR/ptr_comparisons.rs:62:34 + | inside `_` at $DIR/ptr_comparisons.rs:61:34 | - ::: $DIR/ptr_comparisons.rs:62:1 + ::: $DIR/ptr_comparisons.rs:61:1 | LL | const _: *const usize = unsafe { (FOO as *const usize).offset(2) }; | ------------------------------------------------------------------- @@ -16,17 +16,17 @@ LL | const _: *const usize = unsafe { (FOO as *const usize).offset(2) }; = note: `#[deny(const_err)]` on by default error: any use of this value will cause an error - --> $DIR/ptr_comparisons.rs:67:35 + --> $DIR/ptr_comparisons.rs:66:33 | LL | / const _: *const u8 = LL | | -LL | | unsafe { std::ptr::raw_const!((*(FOO as *const usize as *const [u8; 1000]))[999]) }; - | |___________________________________^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^___- - | | - | memory access failed: pointer must be in-bounds at offset 1000, but is outside bounds of alloc2 which has size $WORD +LL | | unsafe { std::ptr::addr_of!((*(FOO as *const usize as *const [u8; 1000]))[999]) }; + | |_________________________________^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^___- + | | + | memory access failed: pointer must be in-bounds at offset 1000, but is outside bounds of alloc2 which has size $WORD error: any use of this value will cause an error - --> $DIR/ptr_comparisons.rs:71:27 + --> $DIR/ptr_comparisons.rs:70:27 | LL | const _: usize = unsafe { std::mem::transmute::<*const usize, usize>(FOO) + 4 }; | --------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--- @@ -34,7 +34,7 @@ LL | const _: usize = unsafe { std::mem::transmute::<*const usize, usize>(FOO) + | "pointer-to-integer cast" needs an rfc before being allowed inside constants error: any use of this value will cause an error - --> $DIR/ptr_comparisons.rs:76:27 + --> $DIR/ptr_comparisons.rs:75:27 | LL | const _: usize = unsafe { *std::mem::transmute::<&&usize, &usize>(&FOO) + 4 }; | --------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---