rust-lang / rust

Empowering everyone to build reliable and efficient software.

Home Page:https://www.rust-lang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Loose Send/Sync bounds for WorkerLocal used in rayon fork for parallel-compiler

ammaraskar opened this issue · comments

The WorkerLocal type used in the parallel-compiler cfg:

pub use rayon_core::WorkerLocal;

which comes from the Rust fork of rayon, rustc-rayon, implements Send and Sync unconditionally for all types T:

https://github.com/rust-lang/rustc-rayon/blob/ae7bbbd2756a324c493aef5f5d52473101b2f491/rayon-core/src/worker_local.rs#L10-L19

/// Holds worker-locals values for each thread in a thread pool.
/// You can only access the worker local value through the Deref impl
/// on the thread pool it was constructed on. It will panic otherwise
pub struct WorkerLocal<T> {
    locals: Vec<CacheAligned<T>>,
    registry: Arc<Registry>,
}

unsafe impl<T> Send for WorkerLocal<T> {}
unsafe impl<T> Sync for WorkerLocal<T> {}

While all the current inhabitants of the type T in rustc are sound, as in they are safe to send across threads, this allows for the potential of data races if someone accidentally uses a non-Send type such as Rc in a WorkerLocal.

As per @cuviper in #t-compiler/wg-parallel-rustc on Zulip, this should be bound by T: Send. https://rust-lang.zulipchat.com/#narrow/stream/187679-t-compiler.2Fwg-parallel-rustc/topic/WorkerLocal.20type.20in.20rustc.20rayon.20fork/near/224118580

Opened rust-lang/rustc-rayon#8 to fix this.