Skip to content

Commit 985402d

Browse files
Rename pointer field on Pin
The internal, unstable field of `Pin` can conflict with fields from the inner type accessed via the `Deref` impl. Rename it from `pointer` to `__pointer`, to make it less likely to conflict with anything else.
1 parent 76101ee commit 985402d

6 files changed

+32
-26
lines changed

library/core/src/pin.rs

+26-20
Original file line numberDiff line numberDiff line change
@@ -1092,14 +1092,20 @@ pub struct Pin<Ptr> {
10921092
// - deter downstream users from accessing it (which would be unsound!),
10931093
// - let the `pin!` macro access it (such a macro requires using struct
10941094
// literal syntax in order to benefit from lifetime extension).
1095-
// Long-term, `unsafe` fields or macro hygiene are expected to offer more robust alternatives.
1095+
//
1096+
// However, if the `Deref` impl exposes a field with the same name as this
1097+
// field, then the two will collide, resulting in a confusing error when the
1098+
// user attempts to access the field through a `Pin<Ptr>`. Therefore, the
1099+
// name `__pointer` is designed to be unlikely to collide with any other
1100+
// field. Long-term, macro hygiene is expected to offer a more robust
1101+
// alternative, alongside `unsafe` fields.
10961102
#[unstable(feature = "unsafe_pin_internals", issue = "none")]
10971103
#[doc(hidden)]
1098-
pub pointer: Ptr,
1104+
pub __pointer: Ptr,
10991105
}
11001106

11011107
// The following implementations aren't derived in order to avoid soundness
1102-
// issues. `&self.pointer` should not be accessible to untrusted trait
1108+
// issues. `&self.__pointer` should not be accessible to untrusted trait
11031109
// implementations.
11041110
//
11051111
// See <https://internals.rust-lang.org/t/unsoundness-in-pin/11311/73> for more details.
@@ -1212,7 +1218,7 @@ impl<Ptr: Deref<Target: Unpin>> Pin<Ptr> {
12121218
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
12131219
#[stable(feature = "pin_into_inner", since = "1.39.0")]
12141220
pub const fn into_inner(pin: Pin<Ptr>) -> Ptr {
1215-
pin.pointer
1221+
pin.__pointer
12161222
}
12171223
}
12181224

@@ -1349,7 +1355,7 @@ impl<Ptr: Deref> Pin<Ptr> {
13491355
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
13501356
#[stable(feature = "pin", since = "1.33.0")]
13511357
pub const unsafe fn new_unchecked(pointer: Ptr) -> Pin<Ptr> {
1352-
Pin { pointer }
1358+
Pin { __pointer: pointer }
13531359
}
13541360

13551361
/// Gets a shared reference to the pinned value this [`Pin`] points to.
@@ -1363,7 +1369,7 @@ impl<Ptr: Deref> Pin<Ptr> {
13631369
#[inline(always)]
13641370
pub fn as_ref(&self) -> Pin<&Ptr::Target> {
13651371
// SAFETY: see documentation on this function
1366-
unsafe { Pin::new_unchecked(&*self.pointer) }
1372+
unsafe { Pin::new_unchecked(&*self.__pointer) }
13671373
}
13681374

13691375
/// Unwraps this `Pin<Ptr>`, returning the underlying `Ptr`.
@@ -1388,7 +1394,7 @@ impl<Ptr: Deref> Pin<Ptr> {
13881394
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
13891395
#[stable(feature = "pin_into_inner", since = "1.39.0")]
13901396
pub const unsafe fn into_inner_unchecked(pin: Pin<Ptr>) -> Ptr {
1391-
pin.pointer
1397+
pin.__pointer
13921398
}
13931399
}
13941400

@@ -1426,7 +1432,7 @@ impl<Ptr: DerefMut> Pin<Ptr> {
14261432
#[inline(always)]
14271433
pub fn as_mut(&mut self) -> Pin<&mut Ptr::Target> {
14281434
// SAFETY: see documentation on this function
1429-
unsafe { Pin::new_unchecked(&mut *self.pointer) }
1435+
unsafe { Pin::new_unchecked(&mut *self.__pointer) }
14301436
}
14311437

14321438
/// Assigns a new value to the memory location pointed to by the `Pin<Ptr>`.
@@ -1455,7 +1461,7 @@ impl<Ptr: DerefMut> Pin<Ptr> {
14551461
where
14561462
Ptr::Target: Sized,
14571463
{
1458-
*(self.pointer) = value;
1464+
*(self.__pointer) = value;
14591465
}
14601466
}
14611467

@@ -1481,7 +1487,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
14811487
U: ?Sized,
14821488
F: FnOnce(&T) -> &U,
14831489
{
1484-
let pointer = &*self.pointer;
1490+
let pointer = &*self.__pointer;
14851491
let new_pointer = func(pointer);
14861492

14871493
// SAFETY: the safety contract for `new_unchecked` must be
@@ -1511,7 +1517,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
15111517
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
15121518
#[stable(feature = "pin", since = "1.33.0")]
15131519
pub const fn get_ref(self) -> &'a T {
1514-
self.pointer
1520+
self.__pointer
15151521
}
15161522
}
15171523

@@ -1522,7 +1528,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
15221528
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
15231529
#[stable(feature = "pin", since = "1.33.0")]
15241530
pub const fn into_ref(self) -> Pin<&'a T> {
1525-
Pin { pointer: self.pointer }
1531+
Pin { __pointer: self.__pointer }
15261532
}
15271533

15281534
/// Gets a mutable reference to the data inside of this `Pin`.
@@ -1542,7 +1548,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
15421548
where
15431549
T: Unpin,
15441550
{
1545-
self.pointer
1551+
self.__pointer
15461552
}
15471553

15481554
/// Gets a mutable reference to the data inside of this `Pin`.
@@ -1560,7 +1566,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
15601566
#[stable(feature = "pin", since = "1.33.0")]
15611567
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
15621568
pub const unsafe fn get_unchecked_mut(self) -> &'a mut T {
1563-
self.pointer
1569+
self.__pointer
15641570
}
15651571

15661572
/// Construct a new pin by mapping the interior value.
@@ -1684,21 +1690,21 @@ impl<Ptr: Receiver> Receiver for Pin<Ptr> {}
16841690
#[stable(feature = "pin", since = "1.33.0")]
16851691
impl<Ptr: fmt::Debug> fmt::Debug for Pin<Ptr> {
16861692
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1687-
fmt::Debug::fmt(&self.pointer, f)
1693+
fmt::Debug::fmt(&self.__pointer, f)
16881694
}
16891695
}
16901696

16911697
#[stable(feature = "pin", since = "1.33.0")]
16921698
impl<Ptr: fmt::Display> fmt::Display for Pin<Ptr> {
16931699
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1694-
fmt::Display::fmt(&self.pointer, f)
1700+
fmt::Display::fmt(&self.__pointer, f)
16951701
}
16961702
}
16971703

16981704
#[stable(feature = "pin", since = "1.33.0")]
16991705
impl<Ptr: fmt::Pointer> fmt::Pointer for Pin<Ptr> {
17001706
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1701-
fmt::Pointer::fmt(&self.pointer, f)
1707+
fmt::Pointer::fmt(&self.__pointer, f)
17021708
}
17031709
}
17041710

@@ -1941,16 +1947,16 @@ pub macro pin($value:expr $(,)?) {
19411947
// instead, dropped _at the end of the enscoping block_.
19421948
// For instance,
19431949
// ```rust
1944-
// let p = Pin { pointer: &mut <temporary> };
1950+
// let p = Pin { __pointer: &mut <temporary> };
19451951
// ```
19461952
// becomes:
19471953
// ```rust
19481954
// let mut anon = <temporary>;
1949-
// let p = Pin { pointer: &mut anon };
1955+
// let p = Pin { __pointer: &mut anon };
19501956
// ```
19511957
// which is *exactly* what we want.
19521958
//
19531959
// See https://doc.rust-lang.org/1.58.1/reference/destructors.html#temporary-lifetime-extension
19541960
// for more info.
1955-
$crate::pin::Pin::<&mut _> { pointer: &mut { $value } }
1961+
$crate::pin::Pin::<&mut _> { __pointer: &mut { $value } }
19561962
}

tests/mir-opt/inline/inline_coroutine.main.Inline.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
- _4 = g() -> [return: bb1, unwind unreachable];
3737
+ _4 = {coroutine@$DIR/inline_coroutine.rs:19:5: 19:8 (#0)};
3838
+ _3 = &mut _4;
39-
+ _2 = Pin::<&mut {coroutine@$DIR/inline_coroutine.rs:19:5: 19:8}> { pointer: move _3 };
39+
+ _2 = Pin::<&mut {coroutine@$DIR/inline_coroutine.rs:19:5: 19:8}> { __pointer: move _3 };
4040
+ StorageDead(_3);
4141
+ StorageLive(_5);
4242
+ _5 = const false;

tests/mir-opt/inline/inline_coroutine.main.Inline.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
- _4 = g() -> [return: bb1, unwind continue];
3737
+ _4 = {coroutine@$DIR/inline_coroutine.rs:19:5: 19:8 (#0)};
3838
+ _3 = &mut _4;
39-
+ _2 = Pin::<&mut {coroutine@$DIR/inline_coroutine.rs:19:5: 19:8}> { pointer: move _3 };
39+
+ _2 = Pin::<&mut {coroutine@$DIR/inline_coroutine.rs:19:5: 19:8}> { __pointer: move _3 };
4040
+ StorageDead(_3);
4141
+ StorageLive(_5);
4242
+ _5 = const false;

tests/ui/feature-gates/feature-gate-unsafe_pin_internals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::{marker::PhantomPinned, pin::Pin};
77

88
/// The `unsafe_pin_internals` is indeed unsound.
99
fn non_unsafe_pin_new_unchecked<T>(pointer: &mut T) -> Pin<&mut T> {
10-
Pin { pointer }
10+
Pin { __pointer: pointer }
1111
}
1212

1313
fn main() {

tests/ui/pin-macro/cant_access_internals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ use core::{
88

99
fn main() {
1010
let mut phantom_pinned = pin!(PhantomPinned);
11-
mem::take(phantom_pinned.pointer); //~ ERROR use of unstable library feature 'unsafe_pin_internals'
11+
mem::take(phantom_pinned.__pointer); //~ ERROR use of unstable library feature 'unsafe_pin_internals'
1212
}

tests/ui/pin-macro/cant_access_internals.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0658]: use of unstable library feature 'unsafe_pin_internals'
22
--> $DIR/cant_access_internals.rs:11:15
33
|
4-
LL | mem::take(phantom_pinned.pointer);
5-
| ^^^^^^^^^^^^^^^^^^^^^^
4+
LL | mem::take(phantom_pinned.__pointer);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= help: add `#![feature(unsafe_pin_internals)]` to the crate attributes to enable
88

0 commit comments

Comments
 (0)