Skip to content

Commit 2811b34

Browse files
committed
Auto merge of rust-lang#137944 - davidtwco:sized-hierarchy, r=oli-obk
Sized Hierarchy: Part I This patch implements the non-const parts of rust-lang/rfcs#3729. It introduces two new traits to the standard library, `MetaSized` and `PointeeSized`. See the RFC for the rationale behind these traits and to discuss whether this change makes sense in the abstract. These traits are unstable (as is their constness), so users cannot refer to them without opting-in to `feature(sized_hierarchy)`. These traits are not behind `cfg`s as this would make implementation unfeasible, there would simply be too many `cfg`s required to add the necessary bounds everywhere. So, like `Sized`, these traits are automatically implemented by the compiler. RFC 3729 describes changes which are necessary to preserve backwards compatibility given the introduction of these traits, which are implemented and as follows: - `?Sized` is rewritten as `MetaSized` - `MetaSized` is added as a default supertrait for all traits w/out an explicit sizedness supertrait already. There are no edition migrations implemented in this, as these are primarily required for the constness parts of the RFC and prior to stabilisation of this (and so will come in follow-up PRs alongside the const parts). All diagnostic output should remain the same (showing `?Sized` even if the compiler sees `MetaSized`) unless the `sized_hierarchy` feature is enabled. Due to the use of unstable extern types in the standard library and rustc, some bounds in both projects have had to be relaxed already - this is unfortunate but unavoidable so that these extern types can continue to be used where they were before. Performing these relaxations in the standard library and rustc are desirable longer-term anyway, but some bounds are not as relaxed as they ideally would be due to the inability to relax `Deref::Target` (this will be investigated separately). It is hoped that this is implemented such that it could be merged and these traits could exist "under the hood" without that being observable to the user (other than in any performance impact this has on the compiler, etc). Some details might leak through due to the standard library relaxations, but this has not been observed in test output. **Notes:** - Any commits starting with "upstream:" can be ignored, as these correspond to other upstream PRs that this is based on which have yet to be merged. - This best reviewed commit-by-commit. I've attempted to make the implementation easy to follow and keep similar changes and test output updates together. - Each commit has a short description describing its purpose. - This patch is large but it's primarily in the test suite. - I've worked on the performance of this patch and a few optimisations are implemented so that the performance impact is neutral-to-minor. - `PointeeSized` is a different name from the RFC just to make it more obvious that it is different from `std::ptr::Pointee` but all the names are yet to be bikeshed anyway. - `@nikomatsakis` has confirmed [that this can proceed as an experiment from the t-lang side](https://rust-lang.zulipchat.com/#narrow/channel/435869-project-goals/topic/SVE.20and.20SME.20on.20AArch64.20.28goals.23270.29/near/506196491) - FCP in rust-lang#137944 (comment) Fixes rust-lang#79409. r? `@ghost` (I'll discuss this with relevant teams to find a reviewer)
2 parents 308ca33 + e9d795d commit 2811b34

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

example/mini_core.rs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ unsafe extern "C" fn _Unwind_Resume() {
1919
intrinsics::unreachable();
2020
}
2121

22+
#[lang = "pointee_sized"]
23+
pub trait PointeeSized {}
24+
25+
#[lang = "meta_sized"]
26+
pub trait MetaSized: PointeeSized {}
27+
2228
#[lang = "sized"]
23-
pub trait Sized {}
29+
pub trait Sized: MetaSized {}
2430

2531
#[lang = "destruct"]
2632
pub trait Destruct {}
@@ -29,35 +35,35 @@ pub trait Destruct {}
2935
pub trait Tuple {}
3036

3137
#[lang = "unsize"]
32-
pub trait Unsize<T: ?Sized> {}
38+
pub trait Unsize<T: PointeeSized>: PointeeSized {}
3339

3440
#[lang = "coerce_unsized"]
3541
pub trait CoerceUnsized<T> {}
3642

37-
impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
38-
impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
39-
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
40-
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
43+
impl<'a, 'b: 'a, T: PointeeSized + Unsize<U>, U: PointeeSized> CoerceUnsized<&'a U> for &'b T {}
44+
impl<'a, T: PointeeSized + Unsize<U>, U: PointeeSized> CoerceUnsized<&'a mut U> for &'a mut T {}
45+
impl<T: PointeeSized + Unsize<U>, U: PointeeSized> CoerceUnsized<*const U> for *const T {}
46+
impl<T: PointeeSized + Unsize<U>, U: PointeeSized> CoerceUnsized<*mut U> for *mut T {}
4147

4248
#[lang = "dispatch_from_dyn"]
4349
pub trait DispatchFromDyn<T> {}
4450

4551
// &T -> &U
46-
impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<&'a U> for &'a T {}
52+
impl<'a, T: PointeeSized + Unsize<U>, U: PointeeSized> DispatchFromDyn<&'a U> for &'a T {}
4753
// &mut T -> &mut U
48-
impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<&'a mut U> for &'a mut T {}
54+
impl<'a, T: PointeeSized + Unsize<U>, U: PointeeSized> DispatchFromDyn<&'a mut U> for &'a mut T {}
4955
// *const T -> *const U
50-
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<*const U> for *const T {}
56+
impl<T: PointeeSized + Unsize<U>, U: PointeeSized> DispatchFromDyn<*const U> for *const T {}
5157
// *mut T -> *mut U
52-
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<*mut U> for *mut T {}
53-
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U, ()>> for Box<T, ()> {}
58+
impl<T: PointeeSized + Unsize<U>, U: PointeeSized> DispatchFromDyn<*mut U> for *mut T {}
59+
impl<T: MetaSized + Unsize<U>, U: MetaSized> DispatchFromDyn<Box<U, ()>> for Box<T, ()> {}
5460

5561
#[lang = "legacy_receiver"]
5662
pub trait LegacyReceiver {}
5763

58-
impl<T: ?Sized> LegacyReceiver for &T {}
59-
impl<T: ?Sized> LegacyReceiver for &mut T {}
60-
impl<T: ?Sized, A: Allocator> LegacyReceiver for Box<T, A> {}
64+
impl<T: PointeeSized> LegacyReceiver for &T {}
65+
impl<T: PointeeSized> LegacyReceiver for &mut T {}
66+
impl<T: MetaSized> LegacyReceiver for Box<T> {}
6167

6268
#[lang = "receiver"]
6369
trait Receiver {}
@@ -84,9 +90,9 @@ impl Copy for i128 {}
8490
impl Copy for f32 {}
8591
impl Copy for f64 {}
8692
impl Copy for char {}
87-
impl<'a, T: ?Sized> Copy for &'a T {}
88-
impl<T: ?Sized> Copy for *const T {}
89-
impl<T: ?Sized> Copy for *mut T {}
93+
impl<'a, T: PointeeSized> Copy for &'a T {}
94+
impl<T: PointeeSized> Copy for *const T {}
95+
impl<T: PointeeSized> Copy for *mut T {}
9096

9197
#[lang = "sync"]
9298
pub unsafe trait Sync {}
@@ -102,17 +108,17 @@ unsafe impl Sync for i16 {}
102108
unsafe impl Sync for i32 {}
103109
unsafe impl Sync for isize {}
104110
unsafe impl Sync for char {}
105-
unsafe impl<'a, T: ?Sized> Sync for &'a T {}
111+
unsafe impl<'a, T: PointeeSized> Sync for &'a T {}
106112
unsafe impl Sync for [u8; 16] {}
107113

108114
#[lang = "freeze"]
109115
unsafe auto trait Freeze {}
110116

111-
unsafe impl<T: ?Sized> Freeze for PhantomData<T> {}
112-
unsafe impl<T: ?Sized> Freeze for *const T {}
113-
unsafe impl<T: ?Sized> Freeze for *mut T {}
114-
unsafe impl<T: ?Sized> Freeze for &T {}
115-
unsafe impl<T: ?Sized> Freeze for &mut T {}
117+
unsafe impl<T: PointeeSized> Freeze for PhantomData<T> {}
118+
unsafe impl<T: PointeeSized> Freeze for *const T {}
119+
unsafe impl<T: PointeeSized> Freeze for *mut T {}
120+
unsafe impl<T: PointeeSized> Freeze for &T {}
121+
unsafe impl<T: PointeeSized> Freeze for &mut T {}
116122

117123
#[lang = "structural_peq"]
118124
pub trait StructuralPartialEq {}
@@ -456,7 +462,7 @@ pub enum Option<T> {
456462
pub use Option::*;
457463

458464
#[lang = "phantom_data"]
459-
pub struct PhantomData<T: ?Sized>;
465+
pub struct PhantomData<T: PointeeSized>;
460466

461467
#[lang = "fn_once"]
462468
#[rustc_paren_sugar]
@@ -576,18 +582,18 @@ impl Allocator for Global {}
576582
#[repr(transparent)]
577583
#[rustc_layout_scalar_valid_range_start(1)]
578584
#[rustc_nonnull_optimization_guaranteed]
579-
pub struct NonNull<T: ?Sized>(pub *const T);
585+
pub struct NonNull<T: PointeeSized>(pub *const T);
580586

581-
impl<T: ?Sized, U: ?Sized> CoerceUnsized<NonNull<U>> for NonNull<T> where T: Unsize<U> {}
582-
impl<T: ?Sized, U: ?Sized> DispatchFromDyn<NonNull<U>> for NonNull<T> where T: Unsize<U> {}
587+
impl<T: PointeeSized, U: PointeeSized> CoerceUnsized<NonNull<U>> for NonNull<T> where T: Unsize<U> {}
588+
impl<T: PointeeSized, U: PointeeSized> DispatchFromDyn<NonNull<U>> for NonNull<T> where T: Unsize<U> {}
583589

584-
pub struct Unique<T: ?Sized> {
590+
pub struct Unique<T: PointeeSized> {
585591
pub pointer: NonNull<T>,
586592
pub _marker: PhantomData<T>,
587593
}
588594

589-
impl<T: ?Sized, U: ?Sized> CoerceUnsized<Unique<U>> for Unique<T> where T: Unsize<U> {}
590-
impl<T: ?Sized, U: ?Sized> DispatchFromDyn<Unique<U>> for Unique<T> where T: Unsize<U> {}
595+
impl<T: PointeeSized, U: PointeeSized> CoerceUnsized<Unique<U>> for Unique<T> where T: Unsize<U> {}
596+
impl<T: PointeeSized, U: PointeeSized> DispatchFromDyn<Unique<U>> for Unique<T> where T: Unsize<U> {}
591597

592598
#[lang = "owned_box"]
593599
pub struct Box<T: ?Sized, A: Allocator = Global>(Unique<T>, A);

0 commit comments

Comments
 (0)