Skip to content

Commit

Permalink
Rename pointer field on Pin
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LegionMammal978 committed Jan 4, 2024
1 parent 090d5ea commit 816f1a7
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 26 deletions.
46 changes: 26 additions & 20 deletions library/core/src/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,14 +411,20 @@ pub struct Pin<P> {
// - deter downstream users from accessing it (which would be unsound!),
// - let the `pin!` macro access it (such a macro requires using struct
// literal syntax in order to benefit from lifetime extension).
// Long-term, `unsafe` fields or macro hygiene are expected to offer more robust alternatives.
//
// However, if `P::Deref` exposes a field with the same name as this field,
// then the two will collide, resulting in a confusing error when the user
// attempts to access the field through a `Pin<P>`. Therefore, the name
// `__pointer` is designed to be unlikely to collide with any other
// field. Long-term, macro hygiene is expected to offer a more robust
// alternative, alongside `unsafe` fields.
#[unstable(feature = "unsafe_pin_internals", issue = "none")]
#[doc(hidden)]
pub pointer: P,
pub __pointer: P,
}

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

Expand Down Expand Up @@ -654,7 +660,7 @@ impl<P: Deref> Pin<P> {
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
#[stable(feature = "pin", since = "1.33.0")]
pub const unsafe fn new_unchecked(pointer: P) -> Pin<P> {
Pin { pointer }
Pin { __pointer: pointer }
}

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

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

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

/// Assigns a new value to the memory behind the pinned reference.
Expand All @@ -750,7 +756,7 @@ impl<P: DerefMut> Pin<P> {
where
P::Target: Sized,
{
*(self.pointer) = value;
*(self.__pointer) = value;
}
}

Expand All @@ -776,7 +782,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
U: ?Sized,
F: FnOnce(&T) -> &U,
{
let pointer = &*self.pointer;
let pointer = &*self.__pointer;
let new_pointer = func(pointer);

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

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

/// Gets a mutable reference to the data inside of this `Pin`.
Expand All @@ -837,7 +843,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
where
T: Unpin,
{
self.pointer
self.__pointer
}

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

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

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

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

Expand Down Expand Up @@ -1235,16 +1241,16 @@ pub macro pin($value:expr $(,)?) {
// instead, dropped _at the end of the enscoping block_.
// For instance,
// ```rust
// let p = Pin { pointer: &mut <temporary> };
// let p = Pin { __pointer: &mut <temporary> };
// ```
// becomes:
// ```rust
// let mut anon = <temporary>;
// let p = Pin { pointer: &mut anon };
// let p = Pin { __pointer: &mut anon };
// ```
// which is *exactly* what we want.
//
// See https://doc.rust-lang.org/1.58.1/reference/destructors.html#temporary-lifetime-extension
// for more info.
$crate::pin::Pin::<&mut _> { pointer: &mut { $value } }
$crate::pin::Pin::<&mut _> { __pointer: &mut { $value } }
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
- _4 = g() -> [return: bb1, unwind unreachable];
+ _4 = {coroutine@$DIR/inline_coroutine.rs:19:5: 19:8 (#0)};
+ _3 = &mut _4;
+ _2 = Pin::<&mut {coroutine@$DIR/inline_coroutine.rs:19:5: 19:8}> { pointer: move _3 };
+ _2 = Pin::<&mut {coroutine@$DIR/inline_coroutine.rs:19:5: 19:8}> { __pointer: move _3 };
+ StorageDead(_3);
+ StorageLive(_5);
+ _5 = const false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
- _4 = g() -> [return: bb1, unwind continue];
+ _4 = {coroutine@$DIR/inline_coroutine.rs:19:5: 19:8 (#0)};
+ _3 = &mut _4;
+ _2 = Pin::<&mut {coroutine@$DIR/inline_coroutine.rs:19:5: 19:8}> { pointer: move _3 };
+ _2 = Pin::<&mut {coroutine@$DIR/inline_coroutine.rs:19:5: 19:8}> { __pointer: move _3 };
+ StorageDead(_3);
+ StorageLive(_5);
+ _5 = const false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::{marker::PhantomPinned, pin::Pin};

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

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/pin-macro/cant_access_internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ use core::{

fn main() {
let mut phantom_pinned = pin!(PhantomPinned);
mem::take(phantom_pinned.pointer); //~ ERROR use of unstable library feature 'unsafe_pin_internals'
mem::take(phantom_pinned.__pointer); //~ ERROR use of unstable library feature 'unsafe_pin_internals'
}
4 changes: 2 additions & 2 deletions tests/ui/pin-macro/cant_access_internals.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0658]: use of unstable library feature 'unsafe_pin_internals'
--> $DIR/cant_access_internals.rs:11:15
|
LL | mem::take(phantom_pinned.pointer);
| ^^^^^^^^^^^^^^^^^^^^^^
LL | mem::take(phantom_pinned.__pointer);
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add `#![feature(unsafe_pin_internals)]` to the crate attributes to enable

Expand Down

0 comments on commit 816f1a7

Please sign in to comment.