From 884d0e031a046af894b43180032c1803bb6d0834 Mon Sep 17 00:00:00 2001 From: David Wood Date: Mon, 10 Feb 2025 14:18:20 +0000 Subject: library/compiler: add `PointeeSized` bounds As core uses an extern type (`ptr::VTable`), the default `?Sized` to `MetaSized` migration isn't sufficient, and some code that previously accepted `VTable` needs relaxed to continue to accept extern types. Similarly, the compiler uses many extern types in `rustc_codegen_llvm` and in the `rustc_middle::ty::List` implementation (`OpaqueListContents`) some bounds must be relaxed to continue to accept these types. Unfortunately, due to the current inability to relax `Deref::Target`, some of the bounds in the standard library are forced to be stricter than they ideally would be. --- compiler/rustc_data_structures/src/aligned.rs | 4 +++- compiler/rustc_data_structures/src/lib.rs | 4 ++++ compiler/rustc_data_structures/src/marker.rs | 30 ++++++++++++++------------- 3 files changed, 23 insertions(+), 15 deletions(-) (limited to 'compiler/rustc_data_structures/src') 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() -> Alignment { /// example `[T]` has alignment of `T`. /// /// [`align_of::()`]: 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 DynSend for &T {} +unsafe impl 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 where T: ?Sized] + [*const T where T: ?Sized + PointeeSized] + [*mut T where T: ?Sized + PointeeSized] + [std::ptr::NonNull where T: ?Sized + PointeeSized] [std::rc::Rc where T: ?Sized, A: Allocator] [std::rc::Weak 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 where T: ?Sized] [std::cell::RefCell where T: ?Sized] [std::cell::UnsafeCell where T: ?Sized] - [std::ptr::NonNull where T: ?Sized] + [std::ptr::NonNull where T: ?Sized + PointeeSized] [std::rc::Rc where T: ?Sized, A: Allocator] [std::rc::Weak where T: ?Sized, A: Allocator] [std::cell::OnceCell where T] @@ -175,10 +177,10 @@ impl_dyn_sync!( [thin_vec::ThinVec where T: DynSync] ); -pub fn assert_dyn_sync() {} -pub fn assert_dyn_send() {} -pub fn assert_dyn_send_val(_t: &T) {} -pub fn assert_dyn_send_sync_val(_t: &T) {} +pub fn assert_dyn_sync() {} +pub fn assert_dyn_send() {} +pub fn assert_dyn_send_val(_t: &T) {} +pub fn assert_dyn_send_sync_val(_t: &T) {} #[derive(Copy, Clone)] pub struct FromDyn(T); @@ -231,10 +233,10 @@ impl std::ops::DerefMut for FromDyn { // an instance of `DynSend` and `DynSync`, since the compiler cannot infer // it automatically in some cases. (e.g. Box) #[derive(Copy, Clone)] -pub struct IntoDynSyncSend(pub T); +pub struct IntoDynSyncSend(pub T); -unsafe impl DynSend for IntoDynSyncSend {} -unsafe impl DynSync for IntoDynSyncSend {} +unsafe impl DynSend for IntoDynSyncSend {} +unsafe impl DynSync for IntoDynSyncSend {} impl std::ops::Deref for IntoDynSyncSend { type Target = T; -- cgit 1.4.1-3-g733a5