Skip to content

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

Closed
@ammaraskar

Description

@ammaraskar

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-parallel-compilerArea: parallel compilerC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions