Rollup merge of #139097 - m-ou-se:pin-tests, r=WaffleLapkin

Add more tests for pin!().

This adds the tests suggested by `@danielhenrymantilla` in this comment: https://github.com/rust-lang/rust/pull/138717#discussion_r2005433640 by
This commit is contained in:
Matthias Krüger 2025-03-29 11:43:49 +01:00 committed by GitHub
commit 8b7088ab5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 75 additions and 0 deletions

View File

@ -47,3 +47,14 @@ fn temp_lifetime() {
}
async fn foo(_: &mut usize) {}
}
#[test]
fn transitive_extension() {
async fn temporary() {}
// `pin!` witnessed in the wild being used like this, even if it yields
// a `Pin<&mut &mut impl Unpin>`; it does work because `pin!`
// happens to transitively extend the lifespan of `temporary()`.
let p = pin!(&mut temporary());
let _use = p;
}

View File

@ -0,0 +1,26 @@
//@ edition:2024
use core::marker::PhantomPinned;
use core::pin::pin;
fn a() {
struct NotCopy<T>(T);
#[allow(unused_mut)]
let mut pointee = NotCopy(PhantomPinned);
pin!(pointee);
let _moved = pointee;
//~^ ERROR use of moved value
}
fn b() {
struct NotCopy<T>(T);
let mut pointee = NotCopy(PhantomPinned);
pin!(*&mut pointee);
//~^ ERROR cannot move
let _moved = pointee;
}
fn main() {
a();
b();
}

View File

@ -0,0 +1,38 @@
error[E0382]: use of moved value: `pointee`
--> $DIR/pin_move.rs:11:18
|
LL | let mut pointee = NotCopy(PhantomPinned);
| ----------- move occurs because `pointee` has type `a::NotCopy<PhantomPinned>`, which does not implement the `Copy` trait
LL | pin!(pointee);
| ------- value moved here
LL | let _moved = pointee;
| ^^^^^^^ value used here after move
|
note: if `a::NotCopy<PhantomPinned>` implemented `Clone`, you could clone the value
--> $DIR/pin_move.rs:7:5
|
LL | struct NotCopy<T>(T);
| ^^^^^^^^^^^^^^^^^ consider implementing `Clone` for this type
...
LL | pin!(pointee);
| ------- you could clone this value
error[E0507]: cannot move out of a mutable reference
--> $DIR/pin_move.rs:18:10
|
LL | pin!(*&mut pointee);
| ^^^^^^^^^^^^^ move occurs because value has type `b::NotCopy<PhantomPinned>`, which does not implement the `Copy` trait
|
note: if `b::NotCopy<PhantomPinned>` implemented `Clone`, you could clone the value
--> $DIR/pin_move.rs:16:5
|
LL | struct NotCopy<T>(T);
| ^^^^^^^^^^^^^^^^^ consider implementing `Clone` for this type
LL | let mut pointee = NotCopy(PhantomPinned);
LL | pin!(*&mut pointee);
| ------------- you could clone this value
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0382, E0507.
For more information about an error, try `rustc --explain E0382`.