Skip to content

Commit

Permalink
Move REGISTERED_DIAGNOSTICS to a ParseSess field
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Mar 8, 2018
1 parent 2aa19fe commit 728c16c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
23 changes: 4 additions & 19 deletions src/libsyntax/diagnostics/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::cell::RefCell;
use std::collections::BTreeMap;
use std::env;

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

thread_local! {
static REGISTERED_DIAGNOSTICS: RefCell<ErrorMap> = {
RefCell::new(BTreeMap::new())
}
}

/// Error information type.
pub struct ErrorInfo {
pub description: Option<Name>,
Expand All @@ -46,14 +39,6 @@ pub struct ErrorInfo {
/// Mapping from error codes to metadata.
pub type ErrorMap = BTreeMap<Name, ErrorInfo>;

fn with_registered_diagnostics<T, F>(f: F) -> T where
F: FnOnce(&mut ErrorMap) -> T,
{
REGISTERED_DIAGNOSTICS.with(move |slot| {
f(&mut *slot.borrow_mut())
})
}

pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt,
span: Span,
token_tree: &[TokenTree])
Expand All @@ -63,7 +48,7 @@ pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt,
_ => unreachable!()
};

with_registered_diagnostics(|diagnostics| {
ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| {
match diagnostics.get_mut(&code.name) {
// Previously used errors.
Some(&mut ErrorInfo { description: _, use_site: Some(previous_span) }) => {
Expand Down Expand Up @@ -132,7 +117,7 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt,
}
});
// Add the error to the map.
with_registered_diagnostics(|diagnostics| {
ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| {
let info = ErrorInfo {
description,
use_site: None
Expand Down Expand Up @@ -174,7 +159,7 @@ pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,

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

// Construct the output expression.
let (count, expr) =
with_registered_diagnostics(|diagnostics| {
ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| {
let descriptions: Vec<P<ast::Expr>> =
diagnostics.iter().filter_map(|(&code, info)| {
info.description.map(|description| {
Expand Down
3 changes: 3 additions & 0 deletions src/libsyntax/parse/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,8 @@ mod tests {
use std::collections::HashSet;
use std::io;
use std::path::PathBuf;
use diagnostics::plugin::ErrorMap;
use rustc_data_structures::sync::Lock;
fn mk_sess(cm: Lrc<CodeMap>) -> ParseSess {
let emitter = errors::emitter::EmitterWriter::new(Box::new(io::sink()),
Some(cm.clone()),
Expand All @@ -1776,6 +1778,7 @@ mod tests {
included_mod_stack: RefCell::new(Vec::new()),
code_map: cm,
missing_fragment_specifiers: RefCell::new(HashSet::new()),
registered_diagnostics: Lock::new(ErrorMap::new()),
non_modrs_mods: RefCell::new(vec![]),
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/libsyntax/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

//! The main parser interface

use rustc_data_structures::sync::Lrc;
use rustc_data_structures::sync::{Lrc, Lock};
use ast::{self, CrateConfig};
use codemap::{CodeMap, FilePathMapping};
use syntax_pos::{self, Span, FileMap, NO_EXPANSION, FileName};
Expand All @@ -21,6 +21,7 @@ use ptr::P;
use str::char_at;
use symbol::Symbol;
use tokenstream::{TokenStream, TokenTree};
use diagnostics::plugin::ErrorMap;

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

0 comments on commit 728c16c

Please sign in to comment.