Rollup merge of #137043 - Sky9x:unsafe-pinned-pt1-libs, r=tgross35,RalfJung,WaffleLapkin

Initial `UnsafePinned` implementation [Part 1: Libs]

Initial libs changes necessary to unblock further work on [RFC 3467](https://rust-lang.github.io/rfcs/3467-unsafe-pinned.html).
Tracking issue: #125735

This PR is split off from #136964, and includes just the libs changes:
- `UnsafePinned` struct
- private `UnsafeUnpin` structural auto trait
- Lang items for both
- Compiler changes necessary to block niches on `UnsafePinned`

This PR does not change codegen, miri, the existing `!Unpin` hack, or anything else. That work is to be split into later PRs.

---

cc ``@RalfJung`` ``@Noratrieb``

``@rustbot`` label F-unsafe_pinned T-libs-api
This commit is contained in:
Jacob Pratt 2025-04-13 17:37:51 -04:00 committed by GitHub
commit d04df1cba9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 290 additions and 8 deletions

View file

@ -0,0 +1,29 @@
//@ check-pass
// this test ensures that UnsafePinned hides the niche of its inner type, just like UnsafeCell does
#![crate_type = "lib"]
#![feature(unsafe_pinned)]
use std::num::NonZero;
use std::pin::UnsafePinned;
macro_rules! assert_size_is {
($ty:ty = $size:expr) => {
const _: () = assert!(size_of::<$ty>() == $size);
};
}
assert_size_is!(UnsafePinned<()> = 0);
assert_size_is!(UnsafePinned<u8> = 1);
assert_size_is!( UnsafePinned< u32> = 4);
assert_size_is!( UnsafePinned< NonZero<u32>> = 4);
assert_size_is!( UnsafePinned<Option<NonZero<u32>>> = 4);
assert_size_is!(Option<UnsafePinned< u32>> = 8);
assert_size_is!(Option<UnsafePinned< NonZero<u32>>> = 8);
assert_size_is!(Option<UnsafePinned<Option<NonZero<u32>>>> = 8);
assert_size_is!( UnsafePinned< &()> = size_of::<usize>());
assert_size_is!( UnsafePinned<Option<&()>> = size_of::<usize>());
assert_size_is!(Option<UnsafePinned< &()>> = size_of::<usize>() * 2);
assert_size_is!(Option<UnsafePinned<Option<&()>>> = size_of::<usize>() * 2);