Skip to content

Commit 4579753

Browse files
authored
Rollup merge of #48808 - Zoxc:reg-diag, r=michaelwoerister
Move REGISTERED_DIAGNOSTICS to a ParseSess field r? @michaelwoerister
2 parents d17eb8f + 728c16c commit 4579753

File tree

4 files changed

+99
-59
lines changed

4 files changed

+99
-59
lines changed

src/librustc_data_structures/sync.rs

+87-39
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ cfg_if! {
6262
pub use std::cell::RefMut as WriteGuard;
6363
pub use std::cell::RefMut as LockGuard;
6464

65-
pub use std::cell::RefCell as RwLock;
65+
use std::cell::RefCell as InnerRwLock;
6666
use std::cell::RefCell as InnerLock;
6767

6868
use std::cell::Cell;
@@ -159,13 +159,12 @@ cfg_if! {
159159

160160
pub use parking_lot::MutexGuard as LockGuard;
161161

162-
use parking_lot;
163-
164162
pub use std::sync::Arc as Lrc;
165163

166164
pub use self::Lock as MTLock;
167165

168166
use parking_lot::Mutex as InnerLock;
167+
use parking_lot::RwLock as InnerRwLock;
169168

170169
pub type MetadataRef = OwningRef<Box<Erased + Send + Sync>, [u8]>;
171170

@@ -222,42 +221,6 @@ cfg_if! {
222221
self.0.lock().take()
223222
}
224223
}
225-
226-
#[derive(Debug)]
227-
pub struct RwLock<T>(parking_lot::RwLock<T>);
228-
229-
impl<T> RwLock<T> {
230-
#[inline(always)]
231-
pub fn new(inner: T) -> Self {
232-
RwLock(parking_lot::RwLock::new(inner))
233-
}
234-
235-
#[inline(always)]
236-
pub fn borrow(&self) -> ReadGuard<T> {
237-
if ERROR_CHECKING {
238-
self.0.try_read().expect("lock was already held")
239-
} else {
240-
self.0.read()
241-
}
242-
}
243-
244-
#[inline(always)]
245-
pub fn borrow_mut(&self) -> WriteGuard<T> {
246-
if ERROR_CHECKING {
247-
self.0.try_write().expect("lock was already held")
248-
} else {
249-
self.0.write()
250-
}
251-
}
252-
}
253-
254-
// FIXME: Probably a bad idea
255-
impl<T: Clone> Clone for RwLock<T> {
256-
#[inline]
257-
fn clone(&self) -> Self {
258-
RwLock::new(self.borrow().clone())
259-
}
260-
}
261224
}
262225
}
263226

@@ -384,6 +347,11 @@ impl<T> Lock<T> {
384347
self.0.borrow_mut()
385348
}
386349

350+
#[inline(always)]
351+
pub fn with_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
352+
f(&mut *self.lock())
353+
}
354+
387355
#[inline(always)]
388356
pub fn borrow(&self) -> LockGuard<T> {
389357
self.lock()
@@ -402,3 +370,83 @@ impl<T: Clone> Clone for Lock<T> {
402370
Lock::new(self.borrow().clone())
403371
}
404372
}
373+
374+
#[derive(Debug)]
375+
pub struct RwLock<T>(InnerRwLock<T>);
376+
377+
impl<T> RwLock<T> {
378+
#[inline(always)]
379+
pub fn new(inner: T) -> Self {
380+
RwLock(InnerRwLock::new(inner))
381+
}
382+
383+
#[inline(always)]
384+
pub fn into_inner(self) -> T {
385+
self.0.into_inner()
386+
}
387+
388+
#[inline(always)]
389+
pub fn get_mut(&mut self) -> &mut T {
390+
self.0.get_mut()
391+
}
392+
393+
#[cfg(not(parallel_queries))]
394+
#[inline(always)]
395+
pub fn read(&self) -> ReadGuard<T> {
396+
self.0.borrow()
397+
}
398+
399+
#[cfg(parallel_queries)]
400+
#[inline(always)]
401+
pub fn read(&self) -> ReadGuard<T> {
402+
if ERROR_CHECKING {
403+
self.0.try_read().expect("lock was already held")
404+
} else {
405+
self.0.read()
406+
}
407+
}
408+
409+
#[inline(always)]
410+
pub fn with_read_lock<F: FnOnce(&T) -> R, R>(&self, f: F) -> R {
411+
f(&*self.read())
412+
}
413+
414+
#[cfg(not(parallel_queries))]
415+
#[inline(always)]
416+
pub fn write(&self) -> WriteGuard<T> {
417+
self.0.borrow_mut()
418+
}
419+
420+
#[cfg(parallel_queries)]
421+
#[inline(always)]
422+
pub fn write(&self) -> WriteGuard<T> {
423+
if ERROR_CHECKING {
424+
self.0.try_write().expect("lock was already held")
425+
} else {
426+
self.0.write()
427+
}
428+
}
429+
430+
#[inline(always)]
431+
pub fn with_write_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
432+
f(&mut *self.write())
433+
}
434+
435+
#[inline(always)]
436+
pub fn borrow(&self) -> ReadGuard<T> {
437+
self.read()
438+
}
439+
440+
#[inline(always)]
441+
pub fn borrow_mut(&self) -> WriteGuard<T> {
442+
self.write()
443+
}
444+
}
445+
446+
// FIXME: Probably a bad idea
447+
impl<T: Clone> Clone for RwLock<T> {
448+
#[inline]
449+
fn clone(&self) -> Self {
450+
RwLock::new(self.borrow().clone())
451+
}
452+
}

src/libsyntax/diagnostics/plugin.rs

+4-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::cell::RefCell;
1211
use std::collections::BTreeMap;
1312
use std::env;
1413

@@ -31,12 +30,6 @@ pub use errors::*;
3130
// Maximum width of any line in an extended error description (inclusive).
3231
const MAX_DESCRIPTION_WIDTH: usize = 80;
3332

34-
thread_local! {
35-
static REGISTERED_DIAGNOSTICS: RefCell<ErrorMap> = {
36-
RefCell::new(BTreeMap::new())
37-
}
38-
}
39-
4033
/// Error information type.
4134
pub struct ErrorInfo {
4235
pub description: Option<Name>,
@@ -46,14 +39,6 @@ pub struct ErrorInfo {
4639
/// Mapping from error codes to metadata.
4740
pub type ErrorMap = BTreeMap<Name, ErrorInfo>;
4841

49-
fn with_registered_diagnostics<T, F>(f: F) -> T where
50-
F: FnOnce(&mut ErrorMap) -> T,
51-
{
52-
REGISTERED_DIAGNOSTICS.with(move |slot| {
53-
f(&mut *slot.borrow_mut())
54-
})
55-
}
56-
5742
pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt,
5843
span: Span,
5944
token_tree: &[TokenTree])
@@ -63,7 +48,7 @@ pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt,
6348
_ => unreachable!()
6449
};
6550

66-
with_registered_diagnostics(|diagnostics| {
51+
ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| {
6752
match diagnostics.get_mut(&code.name) {
6853
// Previously used errors.
6954
Some(&mut ErrorInfo { description: _, use_site: Some(previous_span) }) => {
@@ -132,7 +117,7 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt,
132117
}
133118
});
134119
// Add the error to the map.
135-
with_registered_diagnostics(|diagnostics| {
120+
ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| {
136121
let info = ErrorInfo {
137122
description,
138123
use_site: None
@@ -174,7 +159,7 @@ pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
174159

175160
// Output error metadata to `tmp/extended-errors/<target arch>/<crate name>.json`
176161
if let Ok(target_triple) = env::var("CFG_COMPILER_HOST_TRIPLE") {
177-
with_registered_diagnostics(|diagnostics| {
162+
ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| {
178163
if let Err(e) = output_metadata(ecx,
179164
&target_triple,
180165
&crate_name.name.as_str(),
@@ -194,7 +179,7 @@ pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
194179

195180
// Construct the output expression.
196181
let (count, expr) =
197-
with_registered_diagnostics(|diagnostics| {
182+
ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| {
198183
let descriptions: Vec<P<ast::Expr>> =
199184
diagnostics.iter().filter_map(|(&code, info)| {
200185
info.description.map(|description| {

src/libsyntax/parse/lexer/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1764,6 +1764,8 @@ mod tests {
17641764
use std::collections::HashSet;
17651765
use std::io;
17661766
use std::path::PathBuf;
1767+
use diagnostics::plugin::ErrorMap;
1768+
use rustc_data_structures::sync::Lock;
17671769
fn mk_sess(cm: Lrc<CodeMap>) -> ParseSess {
17681770
let emitter = errors::emitter::EmitterWriter::new(Box::new(io::sink()),
17691771
Some(cm.clone()),
@@ -1776,6 +1778,7 @@ mod tests {
17761778
included_mod_stack: RefCell::new(Vec::new()),
17771779
code_map: cm,
17781780
missing_fragment_specifiers: RefCell::new(HashSet::new()),
1781+
registered_diagnostics: Lock::new(ErrorMap::new()),
17791782
non_modrs_mods: RefCell::new(vec![]),
17801783
}
17811784
}

src/libsyntax/parse/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! The main parser interface
1212
13-
use rustc_data_structures::sync::Lrc;
13+
use rustc_data_structures::sync::{Lrc, Lock};
1414
use ast::{self, CrateConfig};
1515
use codemap::{CodeMap, FilePathMapping};
1616
use syntax_pos::{self, Span, FileMap, NO_EXPANSION, FileName};
@@ -21,6 +21,7 @@ use ptr::P;
2121
use str::char_at;
2222
use symbol::Symbol;
2323
use tokenstream::{TokenStream, TokenTree};
24+
use diagnostics::plugin::ErrorMap;
2425

2526
use std::cell::RefCell;
2627
use std::collections::HashSet;
@@ -47,6 +48,8 @@ pub struct ParseSess {
4748
pub unstable_features: UnstableFeatures,
4849
pub config: CrateConfig,
4950
pub missing_fragment_specifiers: RefCell<HashSet<Span>>,
51+
/// The registered diagnostics codes
52+
pub registered_diagnostics: Lock<ErrorMap>,
5053
// Spans where a `mod foo;` statement was included in a non-mod.rs file.
5154
// These are used to issue errors if the non_modrs_mods feature is not enabled.
5255
pub non_modrs_mods: RefCell<Vec<(ast::Ident, Span)>>,
@@ -71,6 +74,7 @@ impl ParseSess {
7174
unstable_features: UnstableFeatures::from_environment(),
7275
config: HashSet::new(),
7376
missing_fragment_specifiers: RefCell::new(HashSet::new()),
77+
registered_diagnostics: Lock::new(ErrorMap::new()),
7478
included_mod_stack: RefCell::new(vec![]),
7579
code_map,
7680
non_modrs_mods: RefCell::new(vec![]),

0 commit comments

Comments
 (0)