Skip to content

Commit 728c16c

Browse files
committed
Move REGISTERED_DIAGNOSTICS to a ParseSess field
1 parent 2aa19fe commit 728c16c

File tree

3 files changed

+12
-20
lines changed

3 files changed

+12
-20
lines changed

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)