From 29a4ec0d434dba190dfe9e4d46f16e4c026dd6cc Mon Sep 17 00:00:00 2001 From: John Kåre Alsaker Date: Thu, 15 Mar 2018 10:03:36 +0100 Subject: Make queries thread safe. Remove the query stack and make queries point to their parents instead. --- src/librustc_errors/lib.rs | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) (limited to 'src/librustc_errors') diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index a25c3668bb1..23a688636e9 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -42,7 +42,6 @@ use rustc_data_structures::stable_hasher::StableHasher; use std::borrow::Cow; use std::cell::{RefCell, Cell}; -use std::mem; use std::{error, fmt}; use std::sync::atomic::AtomicUsize; use std::sync::atomic::Ordering::SeqCst; @@ -269,7 +268,6 @@ pub struct Handler { emitter: RefCell>, continue_after_error: Cell, delayed_span_bug: RefCell>, - tracked_diagnostics: RefCell>>, // This set contains the `DiagnosticId` of all emitted diagnostics to avoid // emitting the same diagnostic with extended help (`--teach`) twice, which @@ -282,6 +280,11 @@ pub struct Handler { emitted_diagnostics: RefCell>, } +fn default_track_diagnostic(_: &Diagnostic) {} + +thread_local!(pub static TRACK_DIAGNOSTICS: Cell = + Cell::new(default_track_diagnostic)); + #[derive(Default)] pub struct HandlerFlags { pub can_emit_warnings: bool, @@ -333,7 +336,6 @@ impl Handler { emitter: RefCell::new(e), continue_after_error: Cell::new(true), delayed_span_bug: RefCell::new(None), - tracked_diagnostics: RefCell::new(None), tracked_diagnostic_codes: RefCell::new(FxHashSet()), emitted_diagnostics: RefCell::new(FxHashSet()), } @@ -629,17 +631,6 @@ impl Handler { } } - pub fn track_diagnostics(&self, f: F) -> (R, Vec) - where F: FnOnce() -> R - { - let prev = mem::replace(&mut *self.tracked_diagnostics.borrow_mut(), - Some(Vec::new())); - let ret = f(); - let diagnostics = mem::replace(&mut *self.tracked_diagnostics.borrow_mut(), prev) - .unwrap(); - (ret, diagnostics) - } - /// `true` if a diagnostic with this code has already been emitted in this handler. /// /// Used to suppress emitting the same error multiple times with extended explanation when @@ -651,9 +642,9 @@ impl Handler { fn emit_db(&self, db: &DiagnosticBuilder) { let diagnostic = &**db; - if let Some(ref mut list) = *self.tracked_diagnostics.borrow_mut() { - list.push(diagnostic.clone()); - } + TRACK_DIAGNOSTICS.with(|track_diagnostics| { + track_diagnostics.get()(diagnostic); + }); if let Some(ref code) = diagnostic.code { self.tracked_diagnostic_codes.borrow_mut().insert(code.clone()); -- cgit 1.4.1-3-g733a5