Skip to content

Commit 62a8263

Browse files
committed
Implement pin!() using super let.
1 parent 52e3fb4 commit 62a8263

File tree

6 files changed

+35
-158
lines changed

6 files changed

+35
-158
lines changed

compiler/rustc_span/src/symbol.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2198,7 +2198,6 @@ symbols! {
21982198
unsafe_extern_blocks,
21992199
unsafe_fields,
22002200
unsafe_no_drop_flag,
2201-
unsafe_pin_internals,
22022201
unsize,
22032202
unsized_const_param_ty,
22042203
unsized_const_params,

library/core/src/pin.rs

+35-102
Original file line numberDiff line numberDiff line change
@@ -1087,24 +1087,15 @@ use crate::{cmp, fmt};
10871087
#[rustc_pub_transparent]
10881088
#[derive(Copy, Clone)]
10891089
pub struct Pin<Ptr> {
1090-
// FIXME(#93176): this field is made `#[unstable] #[doc(hidden)] pub` to:
1091-
// - deter downstream users from accessing it (which would be unsound!),
1092-
// - let the `pin!` macro access it (such a macro requires using struct
1093-
// literal syntax in order to benefit from lifetime extension).
1094-
//
1095-
// However, if the `Deref` impl exposes a field with the same name as this
1096-
// field, then the two will collide, resulting in a confusing error when the
1097-
// user attempts to access the field through a `Pin<Ptr>`. Therefore, the
1098-
// name `__pointer` is designed to be unlikely to collide with any other
1099-
// field. Long-term, macro hygiene is expected to offer a more robust
1100-
// alternative, alongside `unsafe` fields.
1101-
#[unstable(feature = "unsafe_pin_internals", issue = "none")]
1102-
#[doc(hidden)]
1103-
pub __pointer: Ptr,
1090+
/// Only public for bootstrap.
1091+
#[cfg(bootstrap)]
1092+
pub pointer: Ptr,
1093+
#[cfg(not(bootstrap))]
1094+
pointer: Ptr,
11041095
}
11051096

11061097
// The following implementations aren't derived in order to avoid soundness
1107-
// issues. `&self.__pointer` should not be accessible to untrusted trait
1098+
// issues. `&self.pointer` should not be accessible to untrusted trait
11081099
// implementations.
11091100
//
11101101
// See <https://internals.rust-lang.org/t/unsoundness-in-pin/11311/73> for more details.
@@ -1218,7 +1209,7 @@ impl<Ptr: Deref<Target: Unpin>> Pin<Ptr> {
12181209
#[rustc_const_stable(feature = "const_pin", since = "1.84.0")]
12191210
#[stable(feature = "pin_into_inner", since = "1.39.0")]
12201211
pub const fn into_inner(pin: Pin<Ptr>) -> Ptr {
1221-
pin.__pointer
1212+
pin.pointer
12221213
}
12231214
}
12241215

@@ -1355,7 +1346,7 @@ impl<Ptr: Deref> Pin<Ptr> {
13551346
#[rustc_const_stable(feature = "const_pin", since = "1.84.0")]
13561347
#[stable(feature = "pin", since = "1.33.0")]
13571348
pub const unsafe fn new_unchecked(pointer: Ptr) -> Pin<Ptr> {
1358-
Pin { __pointer: pointer }
1349+
Pin { pointer }
13591350
}
13601351

13611352
/// Gets a shared reference to the pinned value this [`Pin`] points to.
@@ -1369,7 +1360,7 @@ impl<Ptr: Deref> Pin<Ptr> {
13691360
#[inline(always)]
13701361
pub fn as_ref(&self) -> Pin<&Ptr::Target> {
13711362
// SAFETY: see documentation on this function
1372-
unsafe { Pin::new_unchecked(&*self.__pointer) }
1363+
unsafe { Pin::new_unchecked(&*self.pointer) }
13731364
}
13741365
}
13751366

@@ -1413,7 +1404,7 @@ impl<Ptr: DerefMut> Pin<Ptr> {
14131404
#[inline(always)]
14141405
pub fn as_mut(&mut self) -> Pin<&mut Ptr::Target> {
14151406
// SAFETY: see documentation on this function
1416-
unsafe { Pin::new_unchecked(&mut *self.__pointer) }
1407+
unsafe { Pin::new_unchecked(&mut *self.pointer) }
14171408
}
14181409

14191410
/// Gets `Pin<&mut T>` to the underlying pinned value from this nested `Pin`-pointer.
@@ -1480,7 +1471,7 @@ impl<Ptr: DerefMut> Pin<Ptr> {
14801471
where
14811472
Ptr::Target: Sized,
14821473
{
1483-
*(self.__pointer) = value;
1474+
*(self.pointer) = value;
14841475
}
14851476
}
14861477

@@ -1508,7 +1499,7 @@ impl<Ptr: Deref> Pin<Ptr> {
15081499
#[rustc_const_stable(feature = "const_pin", since = "1.84.0")]
15091500
#[stable(feature = "pin_into_inner", since = "1.39.0")]
15101501
pub const unsafe fn into_inner_unchecked(pin: Pin<Ptr>) -> Ptr {
1511-
pin.__pointer
1502+
pin.pointer
15121503
}
15131504
}
15141505

@@ -1534,7 +1525,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
15341525
U: ?Sized,
15351526
F: FnOnce(&T) -> &U,
15361527
{
1537-
let pointer = &*self.__pointer;
1528+
let pointer = &*self.pointer;
15381529
let new_pointer = func(pointer);
15391530

15401531
// SAFETY: the safety contract for `new_unchecked` must be
@@ -1564,7 +1555,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
15641555
#[rustc_const_stable(feature = "const_pin", since = "1.84.0")]
15651556
#[stable(feature = "pin", since = "1.33.0")]
15661557
pub const fn get_ref(self) -> &'a T {
1567-
self.__pointer
1558+
self.pointer
15681559
}
15691560
}
15701561

@@ -1575,7 +1566,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
15751566
#[rustc_const_stable(feature = "const_pin", since = "1.84.0")]
15761567
#[stable(feature = "pin", since = "1.33.0")]
15771568
pub const fn into_ref(self) -> Pin<&'a T> {
1578-
Pin { __pointer: self.__pointer }
1569+
Pin { pointer: self.pointer }
15791570
}
15801571

15811572
/// Gets a mutable reference to the data inside of this `Pin`.
@@ -1595,7 +1586,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
15951586
where
15961587
T: Unpin,
15971588
{
1598-
self.__pointer
1589+
self.pointer
15991590
}
16001591

16011592
/// Gets a mutable reference to the data inside of this `Pin`.
@@ -1613,7 +1604,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
16131604
#[stable(feature = "pin", since = "1.33.0")]
16141605
#[rustc_const_stable(feature = "const_pin", since = "1.84.0")]
16151606
pub const unsafe fn get_unchecked_mut(self) -> &'a mut T {
1616-
self.__pointer
1607+
self.pointer
16171608
}
16181609

16191610
/// Constructs a new pin by mapping the interior value.
@@ -1700,21 +1691,21 @@ impl<Ptr: LegacyReceiver> LegacyReceiver for Pin<Ptr> {}
17001691
#[stable(feature = "pin", since = "1.33.0")]
17011692
impl<Ptr: fmt::Debug> fmt::Debug for Pin<Ptr> {
17021693
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1703-
fmt::Debug::fmt(&self.__pointer, f)
1694+
fmt::Debug::fmt(&self.pointer, f)
17041695
}
17051696
}
17061697

17071698
#[stable(feature = "pin", since = "1.33.0")]
17081699
impl<Ptr: fmt::Display> fmt::Display for Pin<Ptr> {
17091700
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1710-
fmt::Display::fmt(&self.__pointer, f)
1701+
fmt::Display::fmt(&self.pointer, f)
17111702
}
17121703
}
17131704

17141705
#[stable(feature = "pin", since = "1.33.0")]
17151706
impl<Ptr: fmt::Pointer> fmt::Pointer for Pin<Ptr> {
17161707
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1717-
fmt::Pointer::fmt(&self.__pointer, f)
1708+
fmt::Pointer::fmt(&self.pointer, f)
17181709
}
17191710
}
17201711

@@ -1942,78 +1933,20 @@ unsafe impl<T: ?Sized> PinCoerceUnsized for *mut T {}
19421933
/// [`Box::pin`]: ../../std/boxed/struct.Box.html#method.pin
19431934
#[stable(feature = "pin_macro", since = "1.68.0")]
19441935
#[rustc_macro_transparency = "semitransparent"]
1945-
#[allow_internal_unstable(unsafe_pin_internals)]
1946-
#[cfg_attr(not(bootstrap), rustc_macro_edition_2021)]
1936+
#[allow_internal_unstable(super_let)]
1937+
#[cfg(not(bootstrap))]
19471938
pub macro pin($value:expr $(,)?) {
1948-
// This is `Pin::new_unchecked(&mut { $value })`, so, for starters, let's
1949-
// review such a hypothetical macro (that any user-code could define):
1950-
//
1951-
// ```rust
1952-
// macro_rules! pin {( $value:expr ) => (
1953-
// match &mut { $value } { at_value => unsafe { // Do not wrap `$value` in an `unsafe` block.
1954-
// $crate::pin::Pin::<&mut _>::new_unchecked(at_value)
1955-
// }}
1956-
// )}
1957-
// ```
1958-
//
1959-
// Safety:
1960-
// - `type P = &mut _`. There are thus no pathological `Deref{,Mut}` impls
1961-
// that would break `Pin`'s invariants.
1962-
// - `{ $value }` is braced, making it a _block expression_, thus **moving**
1963-
// the given `$value`, and making it _become an **anonymous** temporary_.
1964-
// By virtue of being anonymous, it can no longer be accessed, thus
1965-
// preventing any attempts to `mem::replace` it or `mem::forget` it, _etc._
1966-
//
1967-
// This gives us a `pin!` definition that is sound, and which works, but only
1968-
// in certain scenarios:
1969-
// - If the `pin!(value)` expression is _directly_ fed to a function call:
1970-
// `let poll = pin!(fut).poll(cx);`
1971-
// - If the `pin!(value)` expression is part of a scrutinee:
1972-
// ```rust
1973-
// match pin!(fut) { pinned_fut => {
1974-
// pinned_fut.as_mut().poll(...);
1975-
// pinned_fut.as_mut().poll(...);
1976-
// }} // <- `fut` is dropped here.
1977-
// ```
1978-
// Alas, it doesn't work for the more straight-forward use-case: `let` bindings.
1979-
// ```rust
1980-
// let pinned_fut = pin!(fut); // <- temporary value is freed at the end of this statement
1981-
// pinned_fut.poll(...) // error[E0716]: temporary value dropped while borrowed
1982-
// // note: consider using a `let` binding to create a longer lived value
1983-
// ```
1984-
// - Issues such as this one are the ones motivating https://github.com/rust-lang/rfcs/pull/66
1985-
//
1986-
// This makes such a macro incredibly unergonomic in practice, and the reason most macros
1987-
// out there had to take the path of being a statement/binding macro (_e.g._, `pin!(future);`)
1988-
// instead of featuring the more intuitive ergonomics of an expression macro.
1989-
//
1990-
// Luckily, there is a way to avoid the problem. Indeed, the problem stems from the fact that a
1991-
// temporary is dropped at the end of its enclosing statement when it is part of the parameters
1992-
// given to function call, which has precisely been the case with our `Pin::new_unchecked()`!
1993-
// For instance,
1994-
// ```rust
1995-
// let p = Pin::new_unchecked(&mut <temporary>);
1996-
// ```
1997-
// becomes:
1998-
// ```rust
1999-
// let p = { let mut anon = <temporary>; &mut anon };
2000-
// ```
2001-
//
2002-
// However, when using a literal braced struct to construct the value, references to temporaries
2003-
// can then be taken. This makes Rust change the lifespan of such temporaries so that they are,
2004-
// instead, dropped _at the end of the enscoping block_.
2005-
// For instance,
2006-
// ```rust
2007-
// let p = Pin { __pointer: &mut <temporary> };
2008-
// ```
2009-
// becomes:
2010-
// ```rust
2011-
// let mut anon = <temporary>;
2012-
// let p = Pin { __pointer: &mut anon };
2013-
// ```
2014-
// which is *exactly* what we want.
2015-
//
2016-
// See https://doc.rust-lang.org/1.58.1/reference/destructors.html#temporary-lifetime-extension
2017-
// for more info.
2018-
$crate::pin::Pin::<&mut _> { __pointer: &mut { $value } }
1939+
{
1940+
super let mut pinned = $value;
1941+
// SAFETY: The value is pinned: it is the local above which cannot be named outisde this macro.
1942+
unsafe { $crate::pin::Pin::new_unchecked(&mut pinned) }
1943+
}
1944+
}
1945+
1946+
/// Only for bootstrap.
1947+
#[cfg(bootstrap)]
1948+
#[stable(feature = "pin_macro", since = "1.68.0")]
1949+
#[rustc_macro_transparency = "semitransparent"]
1950+
pub macro pin($value:expr $(,)?) {
1951+
$crate::pin::Pin::<&mut _> { pointer: &mut { $value } }
20191952
}

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

-16
This file was deleted.

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

-15
This file was deleted.

tests/ui/pin-macro/cant_access_internals.rs

-12
This file was deleted.

tests/ui/pin-macro/cant_access_internals.stderr

-12
This file was deleted.

0 commit comments

Comments
 (0)