use std::alloc::Allocator; #[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()` // is true. These types can be wrapped in a `FromDyn` to get a `Send` type. Wrapping a // `Send` type in `IntoDynSyncSend` will create a `DynSend` type. pub unsafe auto trait DynSend {} #[diagnostic::on_unimplemented(message = "`{Self}` doesn't implement `DynSync`. \ Add it to `rustc_data_structures::marker` or use `IntoDynSyncSend` if it's already `Sync`")] // This is an auto trait for types which can be shared across threads if `sync::is_dyn_thread_safe()` // is true. These types can be wrapped in a `FromDyn` to get a `Sync` type. Wrapping a // `Sync` type in `IntoDynSyncSend` will create a `DynSync` type. pub unsafe auto trait DynSync {} // Same with `Sync` and `Send`. unsafe impl DynSend for &T {} macro_rules! impls_dyn_send_neg { ($([$t1: ty $(where $($generics1: tt)*)?])*) => { $(impl$(<$($generics1)*>)? !DynSend for $t1 {})* }; } // Consistent with `std` 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] [std::rc::Rc where T: ?Sized, A: Allocator] [std::rc::Weak where T: ?Sized, A: Allocator] [std::sync::MutexGuard<'_, T> where T: ?Sized] [std::sync::RwLockReadGuard<'_, T> where T: ?Sized] [std::sync::RwLockWriteGuard<'_, T> where T: ?Sized] [std::io::StdoutLock<'_>] [std::io::StderrLock<'_>] ); #[cfg(any(unix, target_os = "hermit", target_os = "wasi", target_os = "solid_asp3"))] // Consistent with `std`, `os_imp::Env` is `!Sync` in these platforms impl !DynSend for std::env::VarsOs {} macro_rules! already_send { ($([$ty: ty])*) => { $(unsafe impl DynSend for $ty where $ty: Send {})* }; } // These structures are already `Send`. already_send!( [std::backtrace::Backtrace][std::io::Stdout][std::io::Stderr][std::io::Error][std::fs::File] [rustc_arena::DroplessArena][crate::memmap::Mmap][crate::profiling::SelfProfiler] [crate::owned_slice::OwnedSlice] ); macro_rules! impl_dyn_send { ($($($attr: meta)* [$ty: ty where $($generics2: tt)*])*) => { $(unsafe impl<$($generics2)*> DynSend for $ty {})* }; } impl_dyn_send!( [std::sync::atomic::AtomicPtr where T] [std::sync::Mutex where T: ?Sized+ DynSend] [std::sync::mpsc::Sender where T: DynSend] [std::sync::Arc where T: ?Sized + DynSync + DynSend] [std::sync::LazyLock where T: DynSend, F: DynSend] [std::collections::HashSet where K: DynSend, S: DynSend] [std::collections::HashMap where K: DynSend, V: DynSend, S: DynSend] [std::collections::BTreeMap where K: DynSend, V: DynSend, A: std::alloc::Allocator + Clone + DynSend] [Vec where T: DynSend, A: std::alloc::Allocator + DynSend] [Box where T: ?Sized + DynSend, A: std::alloc::Allocator + DynSend] [crate::sync::RwLock where T: DynSend] [crate::tagged_ptr::TaggedRef<'a, P, T> where 'a, P: Sync, T: Send + crate::tagged_ptr::Tag] [rustc_arena::TypedArena where T: DynSend] [hashbrown::HashTable where T: DynSend] [indexmap::IndexSet where V: DynSend, S: DynSend] [indexmap::IndexMap where K: DynSend, V: DynSend, S: DynSend] [thin_vec::ThinVec where T: DynSend] [smallvec::SmallVec where A: smallvec::Array + DynSend] ); macro_rules! impls_dyn_sync_neg { ($([$t1: ty $(where $($generics1: tt)*)?])*) => { $(impl$(<$($generics1)*>)? !DynSync for $t1 {})* }; } // Consistent with `std` impls_dyn_sync_neg!( [std::env::Args] [std::env::ArgsOs] [*const T where T: ?Sized] [*mut T where T: ?Sized] [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::rc::Rc where T: ?Sized, A: Allocator] [std::rc::Weak where T: ?Sized, A: Allocator] [std::cell::OnceCell where T] [std::sync::mpsc::Receiver where T] [std::sync::mpsc::Sender where T] ); #[cfg(any(unix, target_os = "hermit", target_os = "wasi", target_os = "solid_asp3"))] // Consistent with `std`, `os_imp::Env` is `!Sync` in these platforms impl !DynSync for std::env::VarsOs {} macro_rules! already_sync { ($([$ty: ty])*) => { $(unsafe impl DynSync for $ty where $ty: Sync {})* }; } // These structures are already `Sync`. already_sync!( [std::sync::atomic::AtomicBool][std::sync::atomic::AtomicUsize][std::sync::atomic::AtomicU8] [std::sync::atomic::AtomicU32][std::backtrace::Backtrace][std::io::Error][std::fs::File] [jobserver_crate::Client][crate::memmap::Mmap][crate::profiling::SelfProfiler] [crate::owned_slice::OwnedSlice] ); // Use portable AtomicU64 for targets without native 64-bit atomics #[cfg(target_has_atomic = "64")] already_sync!([std::sync::atomic::AtomicU64]); #[cfg(not(target_has_atomic = "64"))] already_sync!([portable_atomic::AtomicU64]); macro_rules! impl_dyn_sync { ($($($attr: meta)* [$ty: ty where $($generics2: tt)*])*) => { $(unsafe impl<$($generics2)*> DynSync for $ty {})* }; } impl_dyn_sync!( [std::sync::atomic::AtomicPtr where T] [std::sync::OnceLock where T: DynSend + DynSync] [std::sync::Mutex where T: ?Sized + DynSend] [std::sync::Arc where T: ?Sized + DynSync + DynSend] [std::sync::LazyLock where T: DynSend + DynSync, F: DynSend] [std::collections::HashSet where K: DynSync, S: DynSync] [std::collections::HashMap where K: DynSync, V: DynSync, S: DynSync] [std::collections::BTreeMap where K: DynSync, V: DynSync, A: std::alloc::Allocator + Clone + DynSync] [Vec where T: DynSync, A: std::alloc::Allocator + DynSync] [Box where T: ?Sized + DynSync, A: std::alloc::Allocator + DynSync] [crate::sync::RwLock where T: DynSend + DynSync] [crate::sync::WorkerLocal where T: DynSend] [crate::intern::Interned<'a, T> where 'a, T: DynSync] [crate::tagged_ptr::TaggedRef<'a, P, T> where 'a, P: Sync, T: Sync + crate::tagged_ptr::Tag] [parking_lot::lock_api::Mutex where R: DynSync, T: ?Sized + DynSend] [parking_lot::lock_api::RwLock where R: DynSync, T: ?Sized + DynSend + DynSync] [hashbrown::HashTable where T: DynSync] [indexmap::IndexSet where V: DynSync, S: DynSync] [indexmap::IndexMap where K: DynSync, V: DynSync, S: DynSync] [smallvec::SmallVec where A: smallvec::Array + DynSync] [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) {} #[derive(Copy, Clone)] pub struct FromDyn(T); impl FromDyn { #[inline(always)] pub fn from(val: T) -> Self { // Check that `sync::is_dyn_thread_safe()` is true on creation so we can // implement `Send` and `Sync` for this structure when `T` // implements `DynSend` and `DynSync` respectively. assert!(crate::sync::is_dyn_thread_safe()); FromDyn(val) } #[inline(always)] pub fn derive(&self, val: O) -> FromDyn { // We already did the check for `sync::is_dyn_thread_safe()` when creating `Self` FromDyn(val) } #[inline(always)] pub fn into_inner(self) -> T { self.0 } } // `FromDyn` is `Send` if `T` is `DynSend`, since it ensures that sync::is_dyn_thread_safe() is true. unsafe impl Send for FromDyn {} // `FromDyn` is `Sync` if `T` is `DynSync`, since it ensures that sync::is_dyn_thread_safe() is true. unsafe impl Sync for FromDyn {} impl std::ops::Deref for FromDyn { type Target = T; #[inline(always)] fn deref(&self) -> &Self::Target { &self.0 } } impl std::ops::DerefMut for FromDyn { #[inline(always)] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } // A wrapper to convert a struct that is already a `Send` or `Sync` into // 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); unsafe impl DynSend for IntoDynSyncSend {} unsafe impl DynSync for IntoDynSyncSend {} impl std::ops::Deref for IntoDynSyncSend { type Target = T; #[inline(always)] fn deref(&self) -> &T { &self.0 } } impl std::ops::DerefMut for IntoDynSyncSend { #[inline(always)] fn deref_mut(&mut self) -> &mut T { &mut self.0 } }