Skip to content

Commit 1dd77cd

Browse files
committed
Implement pin!() using super let.
1 parent 58c2dd9 commit 1dd77cd

File tree

7 files changed

+36
-158
lines changed

7 files changed

+36
-158
lines changed

Diff for: compiler/rustc_span/src/symbol.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2215,7 +2215,6 @@ symbols! {
22152215
unsafe_extern_blocks,
22162216
unsafe_fields,
22172217
unsafe_no_drop_flag,
2218-
unsafe_pin_internals,
22192218
unsafe_pinned,
22202219
unsafe_unpin,
22212220
unsize,

Diff for: library/core/src/pin.rs

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

11111102
// The following implementations aren't derived in order to avoid soundness
1112-
// issues. `&self.__pointer` should not be accessible to untrusted trait
1103+
// issues. `&self.pointer` should not be accessible to untrusted trait
11131104
// implementations.
11141105
//
11151106
// See <https://internals.rust-lang.org/t/unsoundness-in-pin/11311/73> for more details.
@@ -1223,7 +1214,7 @@ impl<Ptr: Deref<Target: Unpin>> Pin<Ptr> {
12231214
#[rustc_const_stable(feature = "const_pin", since = "1.84.0")]
12241215
#[stable(feature = "pin_into_inner", since = "1.39.0")]
12251216
pub const fn into_inner(pin: Pin<Ptr>) -> Ptr {
1226-
pin.__pointer
1217+
pin.pointer
12271218
}
12281219
}
12291220

@@ -1360,7 +1351,7 @@ impl<Ptr: Deref> Pin<Ptr> {
13601351
#[rustc_const_stable(feature = "const_pin", since = "1.84.0")]
13611352
#[stable(feature = "pin", since = "1.33.0")]
13621353
pub const unsafe fn new_unchecked(pointer: Ptr) -> Pin<Ptr> {
1363-
Pin { __pointer: pointer }
1354+
Pin { pointer }
13641355
}
13651356

13661357
/// Gets a shared reference to the pinned value this [`Pin`] points to.
@@ -1374,7 +1365,7 @@ impl<Ptr: Deref> Pin<Ptr> {
13741365
#[inline(always)]
13751366
pub fn as_ref(&self) -> Pin<&Ptr::Target> {
13761367
// SAFETY: see documentation on this function
1377-
unsafe { Pin::new_unchecked(&*self.__pointer) }
1368+
unsafe { Pin::new_unchecked(&*self.pointer) }
13781369
}
13791370
}
13801371

@@ -1418,7 +1409,7 @@ impl<Ptr: DerefMut> Pin<Ptr> {
14181409
#[inline(always)]
14191410
pub fn as_mut(&mut self) -> Pin<&mut Ptr::Target> {
14201411
// SAFETY: see documentation on this function
1421-
unsafe { Pin::new_unchecked(&mut *self.__pointer) }
1412+
unsafe { Pin::new_unchecked(&mut *self.pointer) }
14221413
}
14231414

14241415
/// Gets `Pin<&mut T>` to the underlying pinned value from this nested `Pin`-pointer.
@@ -1485,7 +1476,7 @@ impl<Ptr: DerefMut> Pin<Ptr> {
14851476
where
14861477
Ptr::Target: Sized,
14871478
{
1488-
*(self.__pointer) = value;
1479+
*(self.pointer) = value;
14891480
}
14901481
}
14911482

@@ -1513,7 +1504,7 @@ impl<Ptr: Deref> Pin<Ptr> {
15131504
#[rustc_const_stable(feature = "const_pin", since = "1.84.0")]
15141505
#[stable(feature = "pin_into_inner", since = "1.39.0")]
15151506
pub const unsafe fn into_inner_unchecked(pin: Pin<Ptr>) -> Ptr {
1516-
pin.__pointer
1507+
pin.pointer
15171508
}
15181509
}
15191510

@@ -1539,7 +1530,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
15391530
U: ?Sized,
15401531
F: FnOnce(&T) -> &U,
15411532
{
1542-
let pointer = &*self.__pointer;
1533+
let pointer = &*self.pointer;
15431534
let new_pointer = func(pointer);
15441535

15451536
// SAFETY: the safety contract for `new_unchecked` must be
@@ -1569,7 +1560,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
15691560
#[rustc_const_stable(feature = "const_pin", since = "1.84.0")]
15701561
#[stable(feature = "pin", since = "1.33.0")]
15711562
pub const fn get_ref(self) -> &'a T {
1572-
self.__pointer
1563+
self.pointer
15731564
}
15741565
}
15751566

@@ -1580,7 +1571,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
15801571
#[rustc_const_stable(feature = "const_pin", since = "1.84.0")]
15811572
#[stable(feature = "pin", since = "1.33.0")]
15821573
pub const fn into_ref(self) -> Pin<&'a T> {
1583-
Pin { __pointer: self.__pointer }
1574+
Pin { pointer: self.pointer }
15841575
}
15851576

15861577
/// Gets a mutable reference to the data inside of this `Pin`.
@@ -1600,7 +1591,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
16001591
where
16011592
T: Unpin,
16021593
{
1603-
self.__pointer
1594+
self.pointer
16041595
}
16051596

16061597
/// Gets a mutable reference to the data inside of this `Pin`.
@@ -1618,7 +1609,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
16181609
#[stable(feature = "pin", since = "1.33.0")]
16191610
#[rustc_const_stable(feature = "const_pin", since = "1.84.0")]
16201611
pub const unsafe fn get_unchecked_mut(self) -> &'a mut T {
1621-
self.__pointer
1612+
self.pointer
16221613
}
16231614

16241615
/// Constructs a new pin by mapping the interior value.
@@ -1705,21 +1696,21 @@ impl<Ptr: LegacyReceiver> LegacyReceiver for Pin<Ptr> {}
17051696
#[stable(feature = "pin", since = "1.33.0")]
17061697
impl<Ptr: fmt::Debug> fmt::Debug for Pin<Ptr> {
17071698
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1708-
fmt::Debug::fmt(&self.__pointer, f)
1699+
fmt::Debug::fmt(&self.pointer, f)
17091700
}
17101701
}
17111702

17121703
#[stable(feature = "pin", since = "1.33.0")]
17131704
impl<Ptr: fmt::Display> fmt::Display for Pin<Ptr> {
17141705
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1715-
fmt::Display::fmt(&self.__pointer, f)
1706+
fmt::Display::fmt(&self.pointer, f)
17161707
}
17171708
}
17181709

17191710
#[stable(feature = "pin", since = "1.33.0")]
17201711
impl<Ptr: fmt::Pointer> fmt::Pointer for Pin<Ptr> {
17211712
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1722-
fmt::Pointer::fmt(&self.__pointer, f)
1713+
fmt::Pointer::fmt(&self.pointer, f)
17231714
}
17241715
}
17251716

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

Diff for: library/coretests/tests/pin_macro.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ fn rust_2024_expr() {
3838
}
3939

4040
#[test]
41+
#[cfg(not(bootstrap))]
4142
fn temp_lifetime() {
4243
// Check that temporary lifetimes work as in Rust 2021.
4344
// Regression test for https://github.com/rust-lang/rust/issues/138596

Diff for: tests/ui/feature-gates/feature-gate-unsafe_pin_internals.rs

-16
This file was deleted.

Diff for: tests/ui/feature-gates/feature-gate-unsafe_pin_internals.stderr

-15
This file was deleted.

Diff for: tests/ui/pin-macro/cant_access_internals.rs

-12
This file was deleted.

Diff for: tests/ui/pin-macro/cant_access_internals.stderr

-12
This file was deleted.

0 commit comments

Comments
 (0)