diff options
| author | bors <bors@rust-lang.org> | 2025-06-17 15:08:50 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-06-17 15:08:50 +0000 |
| commit | 86d0aef80403f095d8bbabf44d9fdecfcd45f076 (patch) | |
| tree | 5188883db34cd10db6dc7fb22c9d62a801800b1b /compiler/rustc_data_structures | |
| parent | 55d436467c351b56253deeba209ae2553d1c243f (diff) | |
| parent | a31e1f12d9c0fa1b8f5d19aa989f0e2236de3bb8 (diff) | |
| download | rust-86d0aef80403f095d8bbabf44d9fdecfcd45f076.tar.gz rust-86d0aef80403f095d8bbabf44d9fdecfcd45f076.zip | |
Auto merge of #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 https://github.com/rust-lang/rust/pull/137944#issuecomment-2912207485 Fixes rust-lang/rust#79409. r? `@ghost` (I'll discuss this with relevant teams to find a reviewer)
Diffstat (limited to 'compiler/rustc_data_structures')
| -rw-r--r-- | compiler/rustc_data_structures/src/aligned.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_data_structures/src/lib.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_data_structures/src/marker.rs | 30 |
3 files changed, 23 insertions, 15 deletions
diff --git a/compiler/rustc_data_structures/src/aligned.rs b/compiler/rustc_data_structures/src/aligned.rs index a636d09fcae..111740e5509 100644 --- a/compiler/rustc_data_structures/src/aligned.rs +++ b/compiler/rustc_data_structures/src/aligned.rs @@ -1,5 +1,7 @@ use std::ptr::Alignment; +use rustc_serialize::PointeeSized; + /// Returns the ABI-required minimum alignment of a type in bytes. /// /// This is equivalent to [`align_of`], but also works for some unsized @@ -17,7 +19,7 @@ pub const fn align_of<T: ?Sized + Aligned>() -> Alignment { /// example `[T]` has alignment of `T`. /// /// [`align_of::<Self>()`]: align_of -pub unsafe trait Aligned { +pub unsafe trait Aligned: PointeeSized { /// Alignment of `Self`. const ALIGN: Alignment; } diff --git a/compiler/rustc_data_structures/src/lib.rs b/compiler/rustc_data_structures/src/lib.rs index eb3817a80a7..0431182e9e2 100644 --- a/compiler/rustc_data_structures/src/lib.rs +++ b/compiler/rustc_data_structures/src/lib.rs @@ -12,6 +12,7 @@ #![allow(rustc::potential_query_instability)] #![cfg_attr(bootstrap, feature(cfg_match))] #![cfg_attr(not(bootstrap), feature(cfg_select))] +#![cfg_attr(not(bootstrap), feature(sized_hierarchy))] #![deny(unsafe_op_in_unsafe_fn)] #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")] #![doc(rust_logo)] @@ -43,6 +44,9 @@ use std::fmt; pub use atomic_ref::AtomicRef; pub use ena::{snapshot_vec, undo_log, unify}; pub use rustc_index::static_assert_size; +// re-exported for `rustc_smir` +// FIXME(sized_hierarchy): remove with `cfg(bootstrap)`, see `rustc_serialize/src/lib.rs` +pub use rustc_serialize::PointeeSized; pub mod aligned; pub mod base_n; diff --git a/compiler/rustc_data_structures/src/marker.rs b/compiler/rustc_data_structures/src/marker.rs index e0df1b232e1..4846bc997f1 100644 --- a/compiler/rustc_data_structures/src/marker.rs +++ b/compiler/rustc_data_structures/src/marker.rs @@ -1,5 +1,7 @@ use std::alloc::Allocator; +use rustc_serialize::PointeeSized; + #[diagnostic::on_unimplemented(message = "`{Self}` doesn't implement `DynSend`. \ Add it to `rustc_data_structures::marker` or use `IntoDynSyncSend` if it's already `Send`")] // This is an auto trait for types which can be sent across threads if `sync::is_dyn_thread_safe()` @@ -15,7 +17,7 @@ pub unsafe auto trait DynSend {} pub unsafe auto trait DynSync {} // Same with `Sync` and `Send`. -unsafe impl<T: DynSync + ?Sized> DynSend for &T {} +unsafe impl<T: DynSync + ?Sized + PointeeSized> DynSend for &T {} macro_rules! impls_dyn_send_neg { ($([$t1: ty $(where $($generics1: tt)*)?])*) => { @@ -27,9 +29,9 @@ macro_rules! impls_dyn_send_neg { impls_dyn_send_neg!( [std::env::Args] [std::env::ArgsOs] - [*const T where T: ?Sized] - [*mut T where T: ?Sized] - [std::ptr::NonNull<T> where T: ?Sized] + [*const T where T: ?Sized + PointeeSized] + [*mut T where T: ?Sized + PointeeSized] + [std::ptr::NonNull<T> where T: ?Sized + PointeeSized] [std::rc::Rc<T, A> where T: ?Sized, A: Allocator] [std::rc::Weak<T, A> where T: ?Sized, A: Allocator] [std::sync::MutexGuard<'_, T> where T: ?Sized] @@ -100,12 +102,12 @@ macro_rules! impls_dyn_sync_neg { impls_dyn_sync_neg!( [std::env::Args] [std::env::ArgsOs] - [*const T where T: ?Sized] - [*mut T where T: ?Sized] + [*const T where T: ?Sized + PointeeSized] + [*mut T where T: ?Sized + PointeeSized] [std::cell::Cell<T> where T: ?Sized] [std::cell::RefCell<T> where T: ?Sized] [std::cell::UnsafeCell<T> where T: ?Sized] - [std::ptr::NonNull<T> where T: ?Sized] + [std::ptr::NonNull<T> where T: ?Sized + PointeeSized] [std::rc::Rc<T, A> where T: ?Sized, A: Allocator] [std::rc::Weak<T, A> where T: ?Sized, A: Allocator] [std::cell::OnceCell<T> where T] @@ -175,10 +177,10 @@ impl_dyn_sync!( [thin_vec::ThinVec<T> where T: DynSync] ); -pub fn assert_dyn_sync<T: ?Sized + DynSync>() {} -pub fn assert_dyn_send<T: ?Sized + DynSend>() {} -pub fn assert_dyn_send_val<T: ?Sized + DynSend>(_t: &T) {} -pub fn assert_dyn_send_sync_val<T: ?Sized + DynSync + DynSend>(_t: &T) {} +pub fn assert_dyn_sync<T: ?Sized + PointeeSized + DynSync>() {} +pub fn assert_dyn_send<T: ?Sized + PointeeSized + DynSend>() {} +pub fn assert_dyn_send_val<T: ?Sized + PointeeSized + DynSend>(_t: &T) {} +pub fn assert_dyn_send_sync_val<T: ?Sized + PointeeSized + DynSync + DynSend>(_t: &T) {} #[derive(Copy, Clone)] pub struct FromDyn<T>(T); @@ -231,10 +233,10 @@ impl<T> std::ops::DerefMut for FromDyn<T> { // an instance of `DynSend` and `DynSync`, since the compiler cannot infer // it automatically in some cases. (e.g. Box<dyn Send / Sync>) #[derive(Copy, Clone)] -pub struct IntoDynSyncSend<T: ?Sized>(pub T); +pub struct IntoDynSyncSend<T: ?Sized + PointeeSized>(pub T); -unsafe impl<T: ?Sized + Send> DynSend for IntoDynSyncSend<T> {} -unsafe impl<T: ?Sized + Sync> DynSync for IntoDynSyncSend<T> {} +unsafe impl<T: ?Sized + PointeeSized + Send> DynSend for IntoDynSyncSend<T> {} +unsafe impl<T: ?Sized + PointeeSized + Sync> DynSync for IntoDynSyncSend<T> {} impl<T> std::ops::Deref for IntoDynSyncSend<T> { type Target = T; |
