Auto merge of #139114 - m-ou-se:super-let-pin, r=davidtwco

Implement `pin!()` using `super let`

Tracking issue for super let: https://github.com/rust-lang/rust/issues/139076

This uses `super let` to implement `pin!()`.

This means we can remove [the hack](https://github.com/rust-lang/rust/pull/138717) we had to put in to fix https://github.com/rust-lang/rust/issues/138596.

It also means we can remove the original hack to make `pin!()` work, which used a questionable public-but-unstable field rather than a proper private field.

While `super let` is still unstable and subject to change, it seems safe to assume that future Rust will always have a way to express `pin!()` in a compatible way, considering `pin!()` is already stable.

It'd help [the experiment](https://github.com/rust-lang/rust/issues/139076) to have `pin!()` use `super let`, so we can get some more experience with it.
This commit is contained in:
bors 2025-04-19 08:01:53 +00:00
commit a7c39b6861
20 changed files with 78 additions and 192 deletions

View file

@ -33,7 +33,7 @@
- _4 = g() -> [return: bb1, unwind unreachable];
+ _4 = {coroutine@$DIR/inline_coroutine.rs:20:5: 20:8 (#0)};
+ _3 = &mut _4;
+ _2 = Pin::<&mut {coroutine@$DIR/inline_coroutine.rs:20:5: 20:8}> { __pointer: copy _3 };
+ _2 = Pin::<&mut {coroutine@$DIR/inline_coroutine.rs:20:5: 20:8}> { pointer: copy _3 };
+ StorageDead(_3);
+ StorageLive(_5);
+ _5 = const false;

View file

@ -33,7 +33,7 @@
- _4 = g() -> [return: bb1, unwind continue];
+ _4 = {coroutine@$DIR/inline_coroutine.rs:20:5: 20:8 (#0)};
+ _3 = &mut _4;
+ _2 = Pin::<&mut {coroutine@$DIR/inline_coroutine.rs:20:5: 20:8}> { __pointer: copy _3 };
+ _2 = Pin::<&mut {coroutine@$DIR/inline_coroutine.rs:20:5: 20:8}> { pointer: copy _3 };
+ StorageDead(_3);
+ StorageLive(_5);
+ _5 = const false;

View file

@ -121,7 +121,7 @@
- }
-
- bb2: {
+ _4 = Pin::<&mut {async fn body of ActionPermit<'_, T>::perform()}> { __pointer: copy _5 };
+ _4 = Pin::<&mut {async fn body of ActionPermit<'_, T>::perform()}> { pointer: copy _5 };
StorageDead(_5);
StorageLive(_6);
StorageLive(_7);
@ -218,7 +218,7 @@
+ _37 = deref_copy (_8.0: &mut {async fn body of ActionPermit<'_, T>::perform()});
+ _21 = &mut (((*_37) as variant#3).1: std::future::Ready<()>);
+ _20 = &mut (*_21);
+ _19 = Pin::<&mut std::future::Ready<()>> { __pointer: copy _20 };
+ _19 = Pin::<&mut std::future::Ready<()>> { pointer: copy _20 };
+ StorageDead(_20);
+ StorageLive(_22);
+ StorageLive(_23);
@ -239,7 +239,7 @@
+ _48 = &mut (_19.0: &mut std::future::Ready<()>);
+ _45 = copy (_19.0: &mut std::future::Ready<()>);
+ StorageDead(_48);
+ _47 = Pin::<&mut std::future::Ready<()>> { __pointer: copy _45 };
+ _47 = Pin::<&mut std::future::Ready<()>> { pointer: copy _45 };
+ StorageDead(_47);
+ _44 = &mut ((*_45).0: std::option::Option<()>);
+ StorageLive(_49);

View file

@ -123,7 +123,7 @@
- }
-
- bb2: {
+ _4 = Pin::<&mut {async fn body of ActionPermit<'_, T>::perform()}> { __pointer: copy _5 };
+ _4 = Pin::<&mut {async fn body of ActionPermit<'_, T>::perform()}> { pointer: copy _5 };
StorageDead(_5);
StorageLive(_6);
StorageLive(_7);
@ -235,7 +235,7 @@
+ _37 = deref_copy (_8.0: &mut {async fn body of ActionPermit<'_, T>::perform()});
+ _21 = &mut (((*_37) as variant#3).1: std::future::Ready<()>);
+ _20 = &mut (*_21);
+ _19 = Pin::<&mut std::future::Ready<()>> { __pointer: copy _20 };
+ _19 = Pin::<&mut std::future::Ready<()>> { pointer: copy _20 };
+ StorageDead(_20);
+ StorageLive(_22);
+ StorageLive(_23);
@ -256,7 +256,7 @@
+ _50 = &mut (_19.0: &mut std::future::Ready<()>);
+ _47 = copy (_19.0: &mut std::future::Ready<()>);
+ StorageDead(_50);
+ _49 = Pin::<&mut std::future::Ready<()>> { __pointer: copy _47 };
+ _49 = Pin::<&mut std::future::Ready<()>> { pointer: copy _47 };
+ StorageDead(_49);
+ _46 = &mut ((*_47).0: std::option::Option<()>);
+ StorageLive(_51);

View file

@ -2,7 +2,8 @@
//@ edition: 2024
//@ pp-exact
#![feature(gen_blocks, coroutines, coroutine_trait, yield_expr)]
#![feature(gen_blocks, coroutines, coroutine_trait, yield_expr,
stmt_expr_attributes)]
use std::ops::{Coroutine, CoroutineState};
use std::pin::pin;

View file

@ -1,4 +1,4 @@
error[E0499]: cannot borrow `*x.__pointer` as mutable more than once at a time
error[E0499]: cannot borrow `*x.pointer` as mutable more than once at a time
--> $DIR/reborrow-once.rs:12:14
|
LL | twice(x, x);

View file

@ -3,7 +3,7 @@
//@ run-pass
//@ edition: 2024
#![feature(gen_blocks, coroutines, coroutine_trait, yield_expr)]
#![feature(gen_blocks, coroutines, coroutine_trait, yield_expr, stmt_expr_attributes)]
use std::ops::{Coroutine, CoroutineState};
use std::pin::pin;

View file

@ -1,16 +0,0 @@
//@ edition:2018
#![forbid(internal_features, unsafe_code)]
#![feature(unsafe_pin_internals)]
//~^ ERROR the feature `unsafe_pin_internals` is internal to the compiler or standard library
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: pointer }
}
fn main() {
let mut self_referential = PhantomPinned;
let _: Pin<&mut PhantomPinned> = non_unsafe_pin_new_unchecked(&mut self_referential);
}

View file

@ -1,15 +0,0 @@
error: the feature `unsafe_pin_internals` is internal to the compiler or standard library
--> $DIR/feature-gate-unsafe_pin_internals.rs:3:12
|
LL | #![feature(unsafe_pin_internals)]
| ^^^^^^^^^^^^^^^^^^^^
|
= note: using it is strongly discouraged
note: the lint level is defined here
--> $DIR/feature-gate-unsafe_pin_internals.rs:2:11
|
LL | #![forbid(internal_features, unsafe_code)]
| ^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error

View file

@ -1,12 +0,0 @@
//@ edition:2018
use core::{
marker::PhantomPinned,
mem,
pin::{pin, Pin},
};
fn main() {
let mut phantom_pinned = pin!(PhantomPinned);
mem::take(phantom_pinned.__pointer); //~ ERROR use of unstable library feature `unsafe_pin_internals`
}

View file

@ -1,12 +0,0 @@
error[E0658]: use of unstable library feature `unsafe_pin_internals`
--> $DIR/cant_access_internals.rs:11:15
|
LL | mem::take(phantom_pinned.__pointer);
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add `#![feature(unsafe_pin_internals)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0658`.

View file

@ -9,14 +9,14 @@ use core::{
fn function_call_stops_borrow_extension() {
let phantom_pinned = identity(pin!(PhantomPinned));
//~^ ERROR temporary value dropped while borrowed
//~^ ERROR does not live long enough
stuff(phantom_pinned)
}
fn promotion_only_works_for_the_innermost_block() {
let phantom_pinned = {
let phantom_pinned = pin!(PhantomPinned);
//~^ ERROR temporary value dropped while borrowed
//~^ ERROR does not live long enough
phantom_pinned
};
stuff(phantom_pinned)

View file

@ -1,35 +1,29 @@
error[E0716]: temporary value dropped while borrowed
error[E0597]: value does not live long enough
--> $DIR/lifetime_errors_on_promotion_misusage.rs:11:35
|
LL | let phantom_pinned = identity(pin!(PhantomPinned));
| ^^^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| ^^^^^^^^^^^^^^^^^^^ - value dropped here while still borrowed
| |
| creates a temporary value which is freed while still in use
| borrowed value does not live long enough
LL |
LL | stuff(phantom_pinned)
| -------------- borrow later used here
|
= note: this error originates in the macro `pin` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider using a `let` binding to create a longer lived value
|
LL ~ let binding = pin!(PhantomPinned);
LL ~ let phantom_pinned = identity(binding);
|
error[E0716]: temporary value dropped while borrowed
error[E0597]: value does not live long enough
--> $DIR/lifetime_errors_on_promotion_misusage.rs:18:30
|
LL | let phantom_pinned = {
| -------------- borrow later stored here
LL | let phantom_pinned = pin!(PhantomPinned);
| ^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
| ^^^^^^^^^^^^^^^^^^^ borrowed value does not live long enough
...
LL | };
| - temporary value is freed at the end of this statement
| - value dropped here while still borrowed
|
= note: consider using a `let` binding to create a longer lived value
= note: this error originates in the macro `pin` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0716`.
For more information about this error, try `rustc --explain E0597`.

View file

@ -31,6 +31,11 @@ LL | struct NotCopy<T>(T);
LL | let mut pointee = NotCopy(PhantomPinned);
LL | pin!(*&mut pointee);
| ------------- you could clone this value
help: consider removing the dereference here
|
LL - pin!(*&mut pointee);
LL + pin!(&mut pointee);
|
error: aborting due to 2 previous errors