mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 11:48:30 +00:00
Rollup merge of #48808 - Zoxc:reg-diag, r=michaelwoerister
Move REGISTERED_DIAGNOSTICS to a ParseSess field r? @michaelwoerister
This commit is contained in:
commit
457975369b
@ -62,7 +62,7 @@ cfg_if! {
|
|||||||
pub use std::cell::RefMut as WriteGuard;
|
pub use std::cell::RefMut as WriteGuard;
|
||||||
pub use std::cell::RefMut as LockGuard;
|
pub use std::cell::RefMut as LockGuard;
|
||||||
|
|
||||||
pub use std::cell::RefCell as RwLock;
|
use std::cell::RefCell as InnerRwLock;
|
||||||
use std::cell::RefCell as InnerLock;
|
use std::cell::RefCell as InnerLock;
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
@ -159,13 +159,12 @@ cfg_if! {
|
|||||||
|
|
||||||
pub use parking_lot::MutexGuard as LockGuard;
|
pub use parking_lot::MutexGuard as LockGuard;
|
||||||
|
|
||||||
use parking_lot;
|
|
||||||
|
|
||||||
pub use std::sync::Arc as Lrc;
|
pub use std::sync::Arc as Lrc;
|
||||||
|
|
||||||
pub use self::Lock as MTLock;
|
pub use self::Lock as MTLock;
|
||||||
|
|
||||||
use parking_lot::Mutex as InnerLock;
|
use parking_lot::Mutex as InnerLock;
|
||||||
|
use parking_lot::RwLock as InnerRwLock;
|
||||||
|
|
||||||
pub type MetadataRef = OwningRef<Box<Erased + Send + Sync>, [u8]>;
|
pub type MetadataRef = OwningRef<Box<Erased + Send + Sync>, [u8]>;
|
||||||
|
|
||||||
@ -222,42 +221,6 @@ cfg_if! {
|
|||||||
self.0.lock().take()
|
self.0.lock().take()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct RwLock<T>(parking_lot::RwLock<T>);
|
|
||||||
|
|
||||||
impl<T> RwLock<T> {
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn new(inner: T) -> Self {
|
|
||||||
RwLock(parking_lot::RwLock::new(inner))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn borrow(&self) -> ReadGuard<T> {
|
|
||||||
if ERROR_CHECKING {
|
|
||||||
self.0.try_read().expect("lock was already held")
|
|
||||||
} else {
|
|
||||||
self.0.read()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn borrow_mut(&self) -> WriteGuard<T> {
|
|
||||||
if ERROR_CHECKING {
|
|
||||||
self.0.try_write().expect("lock was already held")
|
|
||||||
} else {
|
|
||||||
self.0.write()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: Probably a bad idea
|
|
||||||
impl<T: Clone> Clone for RwLock<T> {
|
|
||||||
#[inline]
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
RwLock::new(self.borrow().clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -384,6 +347,11 @@ impl<T> Lock<T> {
|
|||||||
self.0.borrow_mut()
|
self.0.borrow_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn with_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
|
||||||
|
f(&mut *self.lock())
|
||||||
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn borrow(&self) -> LockGuard<T> {
|
pub fn borrow(&self) -> LockGuard<T> {
|
||||||
self.lock()
|
self.lock()
|
||||||
@ -402,3 +370,83 @@ impl<T: Clone> Clone for Lock<T> {
|
|||||||
Lock::new(self.borrow().clone())
|
Lock::new(self.borrow().clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct RwLock<T>(InnerRwLock<T>);
|
||||||
|
|
||||||
|
impl<T> RwLock<T> {
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn new(inner: T) -> Self {
|
||||||
|
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_queries))]
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn read(&self) -> ReadGuard<T> {
|
||||||
|
self.0.borrow()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(parallel_queries)]
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn read(&self) -> ReadGuard<T> {
|
||||||
|
if ERROR_CHECKING {
|
||||||
|
self.0.try_read().expect("lock was already held")
|
||||||
|
} else {
|
||||||
|
self.0.read()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn with_read_lock<F: FnOnce(&T) -> R, R>(&self, f: F) -> R {
|
||||||
|
f(&*self.read())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(parallel_queries))]
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn write(&self) -> WriteGuard<T> {
|
||||||
|
self.0.borrow_mut()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(parallel_queries)]
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn write(&self) -> WriteGuard<T> {
|
||||||
|
if ERROR_CHECKING {
|
||||||
|
self.0.try_write().expect("lock was already held")
|
||||||
|
} else {
|
||||||
|
self.0.write()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn with_write_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
|
||||||
|
f(&mut *self.write())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn borrow(&self) -> ReadGuard<T> {
|
||||||
|
self.read()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn borrow_mut(&self) -> WriteGuard<T> {
|
||||||
|
self.write()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: Probably a bad idea
|
||||||
|
impl<T: Clone> Clone for RwLock<T> {
|
||||||
|
#[inline]
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
RwLock::new(self.borrow().clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
@ -31,12 +30,6 @@ pub use errors::*;
|
|||||||
// Maximum width of any line in an extended error description (inclusive).
|
// Maximum width of any line in an extended error description (inclusive).
|
||||||
const MAX_DESCRIPTION_WIDTH: usize = 80;
|
const MAX_DESCRIPTION_WIDTH: usize = 80;
|
||||||
|
|
||||||
thread_local! {
|
|
||||||
static REGISTERED_DIAGNOSTICS: RefCell<ErrorMap> = {
|
|
||||||
RefCell::new(BTreeMap::new())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Error information type.
|
/// Error information type.
|
||||||
pub struct ErrorInfo {
|
pub struct ErrorInfo {
|
||||||
pub description: Option<Name>,
|
pub description: Option<Name>,
|
||||||
@ -46,14 +39,6 @@ pub struct ErrorInfo {
|
|||||||
/// Mapping from error codes to metadata.
|
/// Mapping from error codes to metadata.
|
||||||
pub type ErrorMap = BTreeMap<Name, ErrorInfo>;
|
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,
|
pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt,
|
||||||
span: Span,
|
span: Span,
|
||||||
token_tree: &[TokenTree])
|
token_tree: &[TokenTree])
|
||||||
@ -63,7 +48,7 @@ pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt,
|
|||||||
_ => unreachable!()
|
_ => unreachable!()
|
||||||
};
|
};
|
||||||
|
|
||||||
with_registered_diagnostics(|diagnostics| {
|
ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| {
|
||||||
match diagnostics.get_mut(&code.name) {
|
match diagnostics.get_mut(&code.name) {
|
||||||
// Previously used errors.
|
// Previously used errors.
|
||||||
Some(&mut ErrorInfo { description: _, use_site: Some(previous_span) }) => {
|
Some(&mut ErrorInfo { description: _, use_site: Some(previous_span) }) => {
|
||||||
@ -132,7 +117,7 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt,
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
// Add the error to the map.
|
// Add the error to the map.
|
||||||
with_registered_diagnostics(|diagnostics| {
|
ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| {
|
||||||
let info = ErrorInfo {
|
let info = ErrorInfo {
|
||||||
description,
|
description,
|
||||||
use_site: None
|
use_site: None
|
||||||
@ -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`
|
// Output error metadata to `tmp/extended-errors/<target arch>/<crate name>.json`
|
||||||
if let Ok(target_triple) = env::var("CFG_COMPILER_HOST_TRIPLE") {
|
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,
|
if let Err(e) = output_metadata(ecx,
|
||||||
&target_triple,
|
&target_triple,
|
||||||
&crate_name.name.as_str(),
|
&crate_name.name.as_str(),
|
||||||
@ -194,7 +179,7 @@ pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
|
|||||||
|
|
||||||
// Construct the output expression.
|
// Construct the output expression.
|
||||||
let (count, expr) =
|
let (count, expr) =
|
||||||
with_registered_diagnostics(|diagnostics| {
|
ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| {
|
||||||
let descriptions: Vec<P<ast::Expr>> =
|
let descriptions: Vec<P<ast::Expr>> =
|
||||||
diagnostics.iter().filter_map(|(&code, info)| {
|
diagnostics.iter().filter_map(|(&code, info)| {
|
||||||
info.description.map(|description| {
|
info.description.map(|description| {
|
||||||
|
@ -1764,6 +1764,8 @@ mod tests {
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use diagnostics::plugin::ErrorMap;
|
||||||
|
use rustc_data_structures::sync::Lock;
|
||||||
fn mk_sess(cm: Lrc<CodeMap>) -> ParseSess {
|
fn mk_sess(cm: Lrc<CodeMap>) -> ParseSess {
|
||||||
let emitter = errors::emitter::EmitterWriter::new(Box::new(io::sink()),
|
let emitter = errors::emitter::EmitterWriter::new(Box::new(io::sink()),
|
||||||
Some(cm.clone()),
|
Some(cm.clone()),
|
||||||
@ -1776,6 +1778,7 @@ mod tests {
|
|||||||
included_mod_stack: RefCell::new(Vec::new()),
|
included_mod_stack: RefCell::new(Vec::new()),
|
||||||
code_map: cm,
|
code_map: cm,
|
||||||
missing_fragment_specifiers: RefCell::new(HashSet::new()),
|
missing_fragment_specifiers: RefCell::new(HashSet::new()),
|
||||||
|
registered_diagnostics: Lock::new(ErrorMap::new()),
|
||||||
non_modrs_mods: RefCell::new(vec![]),
|
non_modrs_mods: RefCell::new(vec![]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
//! The main parser interface
|
//! The main parser interface
|
||||||
|
|
||||||
use rustc_data_structures::sync::Lrc;
|
use rustc_data_structures::sync::{Lrc, Lock};
|
||||||
use ast::{self, CrateConfig};
|
use ast::{self, CrateConfig};
|
||||||
use codemap::{CodeMap, FilePathMapping};
|
use codemap::{CodeMap, FilePathMapping};
|
||||||
use syntax_pos::{self, Span, FileMap, NO_EXPANSION, FileName};
|
use syntax_pos::{self, Span, FileMap, NO_EXPANSION, FileName};
|
||||||
@ -21,6 +21,7 @@ use ptr::P;
|
|||||||
use str::char_at;
|
use str::char_at;
|
||||||
use symbol::Symbol;
|
use symbol::Symbol;
|
||||||
use tokenstream::{TokenStream, TokenTree};
|
use tokenstream::{TokenStream, TokenTree};
|
||||||
|
use diagnostics::plugin::ErrorMap;
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
@ -47,6 +48,8 @@ pub struct ParseSess {
|
|||||||
pub unstable_features: UnstableFeatures,
|
pub unstable_features: UnstableFeatures,
|
||||||
pub config: CrateConfig,
|
pub config: CrateConfig,
|
||||||
pub missing_fragment_specifiers: RefCell<HashSet<Span>>,
|
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.
|
// 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.
|
// These are used to issue errors if the non_modrs_mods feature is not enabled.
|
||||||
pub non_modrs_mods: RefCell<Vec<(ast::Ident, Span)>>,
|
pub non_modrs_mods: RefCell<Vec<(ast::Ident, Span)>>,
|
||||||
@ -71,6 +74,7 @@ impl ParseSess {
|
|||||||
unstable_features: UnstableFeatures::from_environment(),
|
unstable_features: UnstableFeatures::from_environment(),
|
||||||
config: HashSet::new(),
|
config: HashSet::new(),
|
||||||
missing_fragment_specifiers: RefCell::new(HashSet::new()),
|
missing_fragment_specifiers: RefCell::new(HashSet::new()),
|
||||||
|
registered_diagnostics: Lock::new(ErrorMap::new()),
|
||||||
included_mod_stack: RefCell::new(vec![]),
|
included_mod_stack: RefCell::new(vec![]),
|
||||||
code_map,
|
code_map,
|
||||||
non_modrs_mods: RefCell::new(vec![]),
|
non_modrs_mods: RefCell::new(vec![]),
|
||||||
|
Loading…
Reference in New Issue
Block a user