From 1a5cc2552536eabe8c26a0592e71de5f7e3e0ebd Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Mon, 29 Mar 2021 13:56:25 -0400 Subject: [PATCH 1/2] Remove unused code from `rustc_data_structures::sync` Found using https://github.com/est31/warnalyzer. --- compiler/rustc_data_structures/src/sync.rs | 110 --------------------- 1 file changed, 110 deletions(-) diff --git a/compiler/rustc_data_structures/src/sync.rs b/compiler/rustc_data_structures/src/sync.rs index 26706cd2b1b77..357686342bed0 100644 --- a/compiler/rustc_data_structures/src/sync.rs +++ b/compiler/rustc_data_structures/src/sync.rs @@ -43,46 +43,6 @@ cfg_if! { use std::ops::Add; use std::panic::{resume_unwind, catch_unwind, AssertUnwindSafe}; - /// This is a single threaded variant of AtomicCell provided by crossbeam. - /// Unlike `Atomic` this is intended for all `Copy` types, - /// but it lacks the explicit ordering arguments. - #[derive(Debug)] - pub struct AtomicCell(Cell); - - impl AtomicCell { - #[inline] - pub fn new(v: T) -> Self { - AtomicCell(Cell::new(v)) - } - - #[inline] - pub fn get_mut(&mut self) -> &mut T { - self.0.get_mut() - } - } - - impl AtomicCell { - #[inline] - pub fn into_inner(self) -> T { - self.0.into_inner() - } - - #[inline] - pub fn load(&self) -> T { - self.0.get() - } - - #[inline] - pub fn store(&self, val: T) { - self.0.set(val) - } - - #[inline] - pub fn swap(&self, val: T) -> T { - self.0.replace(val) - } - } - /// This is a single threaded variant of `AtomicU64`, `AtomicUsize`, etc. /// It differs from `AtomicCell` in that it has explicit ordering arguments /// and is only intended for use with the native atomic types. @@ -99,11 +59,6 @@ cfg_if! { } impl Atomic { - #[inline] - pub fn into_inner(self) -> T { - self.0.into_inner() - } - #[inline] pub fn load(&self, _: Ordering) -> T { self.0.get() @@ -113,11 +68,6 @@ cfg_if! { pub fn store(&self, val: T, _: Ordering) { self.0.set(val) } - - #[inline] - pub fn swap(&self, val: T, _: Ordering) -> T { - self.0.replace(val) - } } impl Atomic { @@ -159,22 +109,6 @@ cfg_if! { (oper_a(), oper_b()) } - pub struct SerialScope; - - impl SerialScope { - pub fn spawn(&self, f: F) - where F: FnOnce(&SerialScope) - { - f(self) - } - } - - pub fn scope(f: F) -> R - where F: FnOnce(&SerialScope) -> R - { - f(&SerialScope) - } - #[macro_export] macro_rules! parallel { ($($blocks:tt),*) => { @@ -246,12 +180,6 @@ cfg_if! { pub fn new T>(mut f: F) -> WorkerLocal { WorkerLocal(OneThread::new(f(0))) } - - /// Returns the worker-local value for each thread - #[inline] - pub fn into_inner(self) -> Vec { - vec![OneThread::into_inner(self.0)] - } } impl Deref for WorkerLocal { @@ -279,16 +207,6 @@ cfg_if! { self.0 } - #[inline(always)] - pub fn get_mut(&mut self) -> &mut T { - &mut self.0 - } - - #[inline(always)] - pub fn lock(&self) -> &T { - &self.0 - } - #[inline(always)] pub fn lock_mut(&mut self) -> &mut T { &mut self.0 @@ -318,8 +236,6 @@ cfg_if! { pub use std::sync::atomic::{AtomicBool, AtomicUsize, AtomicU32, AtomicU64}; - pub use crossbeam_utils::atomic::AtomicCell; - pub use std::sync::Arc as Lrc; pub use std::sync::Weak as Weak; @@ -521,16 +437,6 @@ impl RwLock { RwLock(InnerRwLock::new(inner)) } - #[inline(always)] - pub fn into_inner(self) -> T { - self.0.into_inner() - } - - #[inline(always)] - pub fn get_mut(&mut self) -> &mut T { - self.0.get_mut() - } - #[cfg(not(parallel_compiler))] #[inline(always)] pub fn read(&self) -> ReadGuard<'_, T> { @@ -547,11 +453,6 @@ impl RwLock { } } - #[inline(always)] - pub fn with_read_lock R, R>(&self, f: F) -> R { - f(&*self.read()) - } - #[cfg(not(parallel_compiler))] #[inline(always)] pub fn try_write(&self) -> Result, ()> { @@ -580,11 +481,6 @@ impl RwLock { } } - #[inline(always)] - pub fn with_write_lock R, R>(&self, f: F) -> R { - f(&mut *self.write()) - } - #[inline(always)] pub fn borrow(&self) -> ReadGuard<'_, T> { self.read() @@ -633,12 +529,6 @@ impl OneThread { inner, } } - - #[inline(always)] - pub fn into_inner(value: Self) -> T { - value.check(); - value.inner - } } impl Deref for OneThread { From 3412957e7f50a586f8ac2c12d0ffd0384d546534 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Wed, 31 Mar 2021 12:29:34 -0400 Subject: [PATCH 2/2] Unify parallel and non-parallel APIs It's confusing for these to be different, even if some of the methods are unused. --- compiler/rustc_data_structures/src/sync.rs | 56 +++++++++++++++++++++- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_data_structures/src/sync.rs b/compiler/rustc_data_structures/src/sync.rs index 357686342bed0..722ce6b636726 100644 --- a/compiler/rustc_data_structures/src/sync.rs +++ b/compiler/rustc_data_structures/src/sync.rs @@ -44,8 +44,8 @@ cfg_if! { use std::panic::{resume_unwind, catch_unwind, AssertUnwindSafe}; /// This is a single threaded variant of `AtomicU64`, `AtomicUsize`, etc. - /// It differs from `AtomicCell` in that it has explicit ordering arguments - /// and is only intended for use with the native atomic types. + /// It has explicit ordering arguments and is only intended for use with + /// the native atomic types. /// You should use this type through the `AtomicU64`, `AtomicUsize`, etc, type aliases /// as it's not intended to be used separately. #[derive(Debug)] @@ -59,6 +59,11 @@ cfg_if! { } impl Atomic { + #[inline] + pub fn into_inner(self) -> T { + self.0.into_inner() + } + #[inline] pub fn load(&self, _: Ordering) -> T { self.0.get() @@ -68,6 +73,11 @@ cfg_if! { pub fn store(&self, val: T, _: Ordering) { self.0.set(val) } + + #[inline] + pub fn swap(&self, val: T, _: Ordering) -> T { + self.0.replace(val) + } } impl Atomic { @@ -180,6 +190,12 @@ cfg_if! { pub fn new T>(mut f: F) -> WorkerLocal { WorkerLocal(OneThread::new(f(0))) } + + /// Returns the worker-local value for each thread + #[inline] + pub fn into_inner(self) -> Vec { + vec![OneThread::into_inner(self.0)] + } } impl Deref for WorkerLocal { @@ -207,6 +223,16 @@ cfg_if! { self.0 } + #[inline(always)] + pub fn get_mut(&mut self) -> &mut T { + &mut self.0 + } + + #[inline(always)] + pub fn lock(&self) -> &T { + &self.0 + } + #[inline(always)] pub fn lock_mut(&mut self) -> &mut T { &mut self.0 @@ -437,6 +463,16 @@ impl RwLock { RwLock(InnerRwLock::new(inner)) } + #[inline(always)] + pub fn into_inner(self) -> T { + self.0.into_inner() + } + + #[inline(always)] + pub fn get_mut(&mut self) -> &mut T { + self.0.get_mut() + } + #[cfg(not(parallel_compiler))] #[inline(always)] pub fn read(&self) -> ReadGuard<'_, T> { @@ -453,6 +489,11 @@ impl RwLock { } } + #[inline(always)] + pub fn with_read_lock R, R>(&self, f: F) -> R { + f(&*self.read()) + } + #[cfg(not(parallel_compiler))] #[inline(always)] pub fn try_write(&self) -> Result, ()> { @@ -481,6 +522,11 @@ impl RwLock { } } + #[inline(always)] + pub fn with_write_lock R, R>(&self, f: F) -> R { + f(&mut *self.write()) + } + #[inline(always)] pub fn borrow(&self) -> ReadGuard<'_, T> { self.read() @@ -529,6 +575,12 @@ impl OneThread { inner, } } + + #[inline(always)] + pub fn into_inner(value: Self) -> T { + value.check(); + value.inner + } } impl Deref for OneThread {