The code is an example of Pin, it is clearly documented to be unsound, which I don't understand.
Well, my previous posts about UB always lead to because It is volatile the API contract or it is documented as UB, then the code is unsound.
This time, I admit it is unsound first, The question is what makes the API contract to be what we see today.
To my understanding, The API contract is a result, there must be something difficult for the compiler to make the code safe, only with this the protection of the contact, can codes always be sound.
My question is what are the difficult things, for this particular example.
use std::mem;
use std::pin::Pin;
fn move_pinned_ref<T>(mut a: T, mut b: T) {
unsafe {
let p: Pin<&mut T> = Pin::new_unchecked(&mut a);
// This should mean the pointee `a` can never move again.
}
mem::swap(&mut a, &mut b); // Potential UB down the road ⚠️
// The address of `a` changed to `b`'s stack slot, so `a` got moved even
// though we have previously pinned it! We have violated the pinning API contract.
}