Skip to content

Commit 13ffa43

Browse files
committed
rename raw_const/mut -> const/mut_addr_of, and stabilize them
1 parent d3163e9 commit 13ffa43

File tree

11 files changed

+30
-37
lines changed

11 files changed

+30
-37
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,8 @@ impl<'a, K, V, Type> NodeRef<marker::ValMut<'a>, K, V, Type> {
543543
// to avoid aliasing with outstanding references to other elements,
544544
// in particular, those returned to the caller in earlier iterations.
545545
let leaf = Self::as_leaf_ptr(&mut self);
546-
let keys = unsafe { &raw const (*leaf).keys };
547-
let vals = unsafe { &raw mut (*leaf).vals };
546+
let keys = unsafe { ptr::addr_of!((*leaf).keys) };
547+
let vals = unsafe { ptr::addr_of_mut!((*leaf).vals) };
548548
// We must coerce to unsized array pointers because of Rust issue #74679.
549549
let keys: *const [_] = keys;
550550
let vals: *mut [_] = vals;

library/alloc/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@
115115
#![feature(pattern)]
116116
#![feature(ptr_internals)]
117117
#![feature(range_bounds_assert_len)]
118-
#![feature(raw_ref_op)]
119118
#![feature(rustc_attrs)]
120119
#![feature(receiver_trait)]
121120
#![cfg_attr(bootstrap, feature(min_const_generics))]

library/alloc/src/rc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl<T> Rc<T> {
398398

399399
unsafe {
400400
let inner = init_ptr.as_ptr();
401-
ptr::write(&raw mut (*inner).value, data);
401+
ptr::write(ptr::addr_of_mut!((*inner).value), data);
402402

403403
let prev_value = (*inner).strong.get();
404404
debug_assert_eq!(prev_value, 0, "No prior strong references should exist");
@@ -804,7 +804,7 @@ impl<T: ?Sized> Rc<T> {
804804
// SAFETY: This cannot go through Deref::deref or Rc::inner because
805805
// this is required to retain raw/mut provenance such that e.g. `get_mut` can
806806
// write through the pointer after the Rc is recovered through `from_raw`.
807-
unsafe { &raw const (*ptr).value }
807+
unsafe { ptr::addr_of_mut!((*ptr).value) }
808808
}
809809

810810
/// Constructs an `Rc<T>` from a raw pointer.
@@ -1917,7 +1917,7 @@ impl<T: ?Sized> Weak<T> {
19171917
// SAFETY: if is_dangling returns false, then the pointer is dereferencable.
19181918
// The payload may be dropped at this point, and we have to maintain provenance,
19191919
// so use raw pointer manipulation.
1920-
unsafe { &raw const (*ptr).value }
1920+
unsafe { ptr::addr_of_mut!((*ptr).value) }
19211921
}
19221922
}
19231923

library/alloc/src/sync.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ impl<T> Arc<T> {
384384
// reference into a strong reference.
385385
unsafe {
386386
let inner = init_ptr.as_ptr();
387-
ptr::write(&raw mut (*inner).data, data);
387+
ptr::write(ptr::addr_of_mut!((*inner).data), data);
388388

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

806806
/// Constructs an `Arc<T>` from a raw pointer.
@@ -1677,7 +1677,7 @@ impl<T: ?Sized> Weak<T> {
16771677
// SAFETY: if is_dangling returns false, then the pointer is dereferencable.
16781678
// The payload may be dropped at this point, and we have to maintain provenance,
16791679
// so use raw pointer manipulation.
1680-
unsafe { &raw mut (*ptr).data }
1680+
unsafe { ptr::addr_of_mut!((*ptr).data) }
16811681
}
16821682
}
16831683

library/core/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@
126126
#![feature(auto_traits)]
127127
#![feature(or_patterns)]
128128
#![feature(prelude_import)]
129-
#![feature(raw_ref_macros)]
130129
#![feature(repr_simd, platform_intrinsics)]
131130
#![feature(rustc_attrs)]
132131
#![feature(simd_ffi)]

library/core/src/ptr/mod.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,6 @@ fnptr_impls_args! { A, B, C, D, E, F, G, H, I, J, K, L }
15011501
/// # Example
15021502
///
15031503
/// ```
1504-
/// #![feature(raw_ref_macros)]
15051504
/// use std::ptr;
15061505
///
15071506
/// #[repr(packed)]
@@ -1512,14 +1511,14 @@ fnptr_impls_args! { A, B, C, D, E, F, G, H, I, J, K, L }
15121511
///
15131512
/// let packed = Packed { f1: 1, f2: 2 };
15141513
/// // `&packed.f2` would create an unaligned reference, and thus be Undefined Behavior!
1515-
/// let raw_f2 = ptr::raw_const!(packed.f2);
1514+
/// let raw_f2 = ptr::addr_of!(packed.f2);
15161515
/// assert_eq!(unsafe { raw_f2.read_unaligned() }, 2);
15171516
/// ```
1518-
#[unstable(feature = "raw_ref_macros", issue = "73394")]
1517+
#[stable(feature = "raw_ref_macros", since = "1.51.0")]
15191518
#[rustc_macro_transparency = "semitransparent"]
15201519
#[allow_internal_unstable(raw_ref_op)]
1521-
pub macro raw_const($e:expr) {
1522-
&raw const $e
1520+
pub macro addr_of($place:expr) {
1521+
&raw const $place
15231522
}
15241523

15251524
/// Create a `mut` raw pointer to a place, without creating an intermediate reference.
@@ -1534,7 +1533,6 @@ pub macro raw_const($e:expr) {
15341533
/// # Example
15351534
///
15361535
/// ```
1537-
/// #![feature(raw_ref_macros)]
15381536
/// use std::ptr;
15391537
///
15401538
/// #[repr(packed)]
@@ -1545,13 +1543,13 @@ pub macro raw_const($e:expr) {
15451543
///
15461544
/// let mut packed = Packed { f1: 1, f2: 2 };
15471545
/// // `&mut packed.f2` would create an unaligned reference, and thus be Undefined Behavior!
1548-
/// let raw_f2 = ptr::raw_mut!(packed.f2);
1546+
/// let raw_f2 = ptr::addr_of_mut!(packed.f2);
15491547
/// unsafe { raw_f2.write_unaligned(42); }
15501548
/// assert_eq!({packed.f2}, 42); // `{...}` forces copying the field instead of creating a reference.
15511549
/// ```
1552-
#[unstable(feature = "raw_ref_macros", issue = "73394")]
1550+
#[stable(feature = "raw_ref_macros", since = "1.51.0")]
15531551
#[rustc_macro_transparency = "semitransparent"]
15541552
#[allow_internal_unstable(raw_ref_op)]
1555-
pub macro raw_mut($e:expr) {
1556-
&raw mut $e
1553+
pub macro addr_of_mut($place:expr) {
1554+
&raw mut $place
15571555
}

library/core/src/slice/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,8 @@ impl<T> [T] {
543543
#[inline]
544544
pub fn swap(&mut self, a: usize, b: usize) {
545545
// Can't take two mutable loans from one vector, so instead use raw pointers.
546-
let pa = ptr::raw_mut!(self[a]);
547-
let pb = ptr::raw_mut!(self[b]);
546+
let pa = ptr::addr_of_mut!(self[a]);
547+
let pb = ptr::addr_of_mut!(self[b]);
548548
// SAFETY: `pa` and `pb` have been created from safe mutable references and refer
549549
// to elements in the slice and therefore are guaranteed to be valid and aligned.
550550
// Note that accessing the elements behind `a` and `b` is checked and will

library/std/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@
298298
#![feature(prelude_import)]
299299
#![feature(ptr_internals)]
300300
#![feature(raw)]
301-
#![feature(raw_ref_macros)]
302301
#![feature(ready_macro)]
303302
#![feature(rustc_attrs)]
304303
#![feature(rustc_private)]
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// check-pass
22
#![feature(const_raw_ptr_deref)]
3-
#![feature(raw_ref_macros)]
43

54
use std::ptr;
65

76
const fn test_fn(x: *const i32) {
8-
let x2 = unsafe { ptr::raw_const!(*x) };
7+
let x2 = unsafe { ptr::addr_of!(*x) };
98
}
109

1110
fn main() {}

src/test/ui/consts/ptr_comparisons.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
core_intrinsics,
1010
const_raw_ptr_comparison,
1111
const_ptr_offset,
12-
const_raw_ptr_deref,
13-
raw_ref_macros
12+
const_raw_ptr_deref
1413
)]
1514

1615
const FOO: &usize = &42;
@@ -64,7 +63,7 @@ const _: *const usize = unsafe { (FOO as *const usize).offset(2) };
6463

6564
const _: *const u8 =
6665
//~^ NOTE
67-
unsafe { std::ptr::raw_const!((*(FOO as *const usize as *const [u8; 1000]))[999]) };
66+
unsafe { std::ptr::addr_of!((*(FOO as *const usize as *const [u8; 1000]))[999]) };
6867
//~^ ERROR any use of this value will cause an error
6968
//~| NOTE
7069

src/test/ui/consts/ptr_comparisons.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,35 @@ LL | unsafe { intrinsics::offset(self, count) }
66
| |
77
| inbounds test failed: pointer must be in-bounds at offset $TWO_WORDS, but is outside bounds of alloc2 which has size $WORD
88
| inside `ptr::const_ptr::<impl *const usize>::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
9-
| inside `_` at $DIR/ptr_comparisons.rs:62:34
9+
| inside `_` at $DIR/ptr_comparisons.rs:61:34
1010
|
11-
::: $DIR/ptr_comparisons.rs:62:1
11+
::: $DIR/ptr_comparisons.rs:61:1
1212
|
1313
LL | const _: *const usize = unsafe { (FOO as *const usize).offset(2) };
1414
| -------------------------------------------------------------------
1515
|
1616
= note: `#[deny(const_err)]` on by default
1717

1818
error: any use of this value will cause an error
19-
--> $DIR/ptr_comparisons.rs:67:35
19+
--> $DIR/ptr_comparisons.rs:66:33
2020
|
2121
LL | / const _: *const u8 =
2222
LL | |
23-
LL | | unsafe { std::ptr::raw_const!((*(FOO as *const usize as *const [u8; 1000]))[999]) };
24-
| |___________________________________^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^___-
25-
| |
26-
| memory access failed: pointer must be in-bounds at offset 1000, but is outside bounds of alloc2 which has size $WORD
23+
LL | | unsafe { std::ptr::addr_of!((*(FOO as *const usize as *const [u8; 1000]))[999]) };
24+
| |_________________________________^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^___-
25+
| |
26+
| memory access failed: pointer must be in-bounds at offset 1000, but is outside bounds of alloc2 which has size $WORD
2727

2828
error: any use of this value will cause an error
29-
--> $DIR/ptr_comparisons.rs:71:27
29+
--> $DIR/ptr_comparisons.rs:70:27
3030
|
3131
LL | const _: usize = unsafe { std::mem::transmute::<*const usize, usize>(FOO) + 4 };
3232
| --------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---
3333
| |
3434
| "pointer-to-integer cast" needs an rfc before being allowed inside constants
3535

3636
error: any use of this value will cause an error
37-
--> $DIR/ptr_comparisons.rs:76:27
37+
--> $DIR/ptr_comparisons.rs:75:27
3838
|
3939
LL | const _: usize = unsafe { *std::mem::transmute::<&&usize, &usize>(&FOO) + 4 };
4040
| --------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---

0 commit comments

Comments
 (0)