Skip to content

Commit 48fbe38

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 090d5ea commit 48fbe38

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

library/core/src/pin.rs

+26-20
Original file line numberDiff line numberDiff line change
@@ -411,14 +411,20 @@ pub struct Pin<P> {
411411
// - deter downstream users from accessing it (which would be unsound!),
412412
// - let the `pin!` macro access it (such a macro requires using struct
413413
// literal syntax in order to benefit from lifetime extension).
414-
// Long-term, `unsafe` fields or macro hygiene are expected to offer more robust alternatives.
414+
//
415+
// However, if `P::Deref` exposes a field with the same name as this field,
416+
// then the two will collide, resulting in a confusing error when the user
417+
// attempts to access the field through a `Pin<P>`. Therefore, the name
418+
// `__pointer` is designed to be unlikely to collide with any other
419+
// field. Long-term, macro hygiene is expected to offer a more robust
420+
// alternative, alongside `unsafe` fields.
415421
#[unstable(feature = "unsafe_pin_internals", issue = "none")]
416422
#[doc(hidden)]
417-
pub pointer: P,
423+
pub __pointer: P,
418424
}
419425

420426
// The following implementations aren't derived in order to avoid soundness
421-
// issues. `&self.pointer` should not be accessible to untrusted trait
427+
// issues. `&self.__pointer` should not be accessible to untrusted trait
422428
// implementations.
423429
//
424430
// See <https://internals.rust-lang.org/t/unsoundness-in-pin/11311/73> for more details.
@@ -525,7 +531,7 @@ impl<P: Deref<Target: Unpin>> Pin<P> {
525531
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
526532
#[stable(feature = "pin_into_inner", since = "1.39.0")]
527533
pub const fn into_inner(pin: Pin<P>) -> P {
528-
pin.pointer
534+
pin.__pointer
529535
}
530536
}
531537

@@ -654,7 +660,7 @@ impl<P: Deref> Pin<P> {
654660
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
655661
#[stable(feature = "pin", since = "1.33.0")]
656662
pub const unsafe fn new_unchecked(pointer: P) -> Pin<P> {
657-
Pin { pointer }
663+
Pin { __pointer: pointer }
658664
}
659665

660666
/// Gets a pinned shared reference from this pinned pointer.
@@ -668,7 +674,7 @@ impl<P: Deref> Pin<P> {
668674
#[inline(always)]
669675
pub fn as_ref(&self) -> Pin<&P::Target> {
670676
// SAFETY: see documentation on this function
671-
unsafe { Pin::new_unchecked(&*self.pointer) }
677+
unsafe { Pin::new_unchecked(&*self.__pointer) }
672678
}
673679

674680
/// Unwraps this `Pin<P>` returning the underlying pointer.
@@ -688,7 +694,7 @@ impl<P: Deref> Pin<P> {
688694
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
689695
#[stable(feature = "pin_into_inner", since = "1.39.0")]
690696
pub const unsafe fn into_inner_unchecked(pin: Pin<P>) -> P {
691-
pin.pointer
697+
pin.__pointer
692698
}
693699
}
694700

@@ -725,7 +731,7 @@ impl<P: DerefMut> Pin<P> {
725731
#[inline(always)]
726732
pub fn as_mut(&mut self) -> Pin<&mut P::Target> {
727733
// SAFETY: see documentation on this function
728-
unsafe { Pin::new_unchecked(&mut *self.pointer) }
734+
unsafe { Pin::new_unchecked(&mut *self.__pointer) }
729735
}
730736

731737
/// Assigns a new value to the memory behind the pinned reference.
@@ -750,7 +756,7 @@ impl<P: DerefMut> Pin<P> {
750756
where
751757
P::Target: Sized,
752758
{
753-
*(self.pointer) = value;
759+
*(self.__pointer) = value;
754760
}
755761
}
756762

@@ -776,7 +782,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
776782
U: ?Sized,
777783
F: FnOnce(&T) -> &U,
778784
{
779-
let pointer = &*self.pointer;
785+
let pointer = &*self.__pointer;
780786
let new_pointer = func(pointer);
781787

782788
// SAFETY: the safety contract for `new_unchecked` must be
@@ -806,7 +812,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
806812
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
807813
#[stable(feature = "pin", since = "1.33.0")]
808814
pub const fn get_ref(self) -> &'a T {
809-
self.pointer
815+
self.__pointer
810816
}
811817
}
812818

@@ -817,7 +823,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
817823
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
818824
#[stable(feature = "pin", since = "1.33.0")]
819825
pub const fn into_ref(self) -> Pin<&'a T> {
820-
Pin { pointer: self.pointer }
826+
Pin { __pointer: self.__pointer }
821827
}
822828

823829
/// Gets a mutable reference to the data inside of this `Pin`.
@@ -837,7 +843,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
837843
where
838844
T: Unpin,
839845
{
840-
self.pointer
846+
self.__pointer
841847
}
842848

843849
/// Gets a mutable reference to the data inside of this `Pin`.
@@ -855,7 +861,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
855861
#[stable(feature = "pin", since = "1.33.0")]
856862
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
857863
pub const unsafe fn get_unchecked_mut(self) -> &'a mut T {
858-
self.pointer
864+
self.__pointer
859865
}
860866

861867
/// Construct a new pin by mapping the interior value.
@@ -978,21 +984,21 @@ impl<P: Receiver> Receiver for Pin<P> {}
978984
#[stable(feature = "pin", since = "1.33.0")]
979985
impl<P: fmt::Debug> fmt::Debug for Pin<P> {
980986
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
981-
fmt::Debug::fmt(&self.pointer, f)
987+
fmt::Debug::fmt(&self.__pointer, f)
982988
}
983989
}
984990

985991
#[stable(feature = "pin", since = "1.33.0")]
986992
impl<P: fmt::Display> fmt::Display for Pin<P> {
987993
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
988-
fmt::Display::fmt(&self.pointer, f)
994+
fmt::Display::fmt(&self.__pointer, f)
989995
}
990996
}
991997

992998
#[stable(feature = "pin", since = "1.33.0")]
993999
impl<P: fmt::Pointer> fmt::Pointer for Pin<P> {
9941000
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
995-
fmt::Pointer::fmt(&self.pointer, f)
1001+
fmt::Pointer::fmt(&self.__pointer, f)
9961002
}
9971003
}
9981004

@@ -1235,16 +1241,16 @@ pub macro pin($value:expr $(,)?) {
12351241
// instead, dropped _at the end of the enscoping block_.
12361242
// For instance,
12371243
// ```rust
1238-
// let p = Pin { pointer: &mut <temporary> };
1244+
// let p = Pin { __pointer: &mut <temporary> };
12391245
// ```
12401246
// becomes:
12411247
// ```rust
12421248
// let mut anon = <temporary>;
1243-
// let p = Pin { pointer: &mut anon };
1249+
// let p = Pin { __pointer: &mut anon };
12441250
// ```
12451251
// which is *exactly* what we want.
12461252
//
12471253
// See https://doc.rust-lang.org/1.58.1/reference/destructors.html#temporary-lifetime-extension
12481254
// for more info.
1249-
$crate::pin::Pin::<&mut _> { pointer: &mut { $value } }
1255+
$crate::pin::Pin::<&mut _> { __pointer: &mut { $value } }
12501256
}

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)