From 11ccc4cac6d1c8fbe27d55714a78e8118c07379c Mon Sep 17 00:00:00 2001 From: John Kåre Alsaker Date: Mon, 26 Mar 2018 20:10:05 +0200 Subject: Make LazyTokenStream thread-safe --- src/libsyntax/parse/token.rs | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 7798a7a77ee..2c30ac9ba35 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -25,9 +25,8 @@ use syntax_pos::{self, Span, FileName}; use tokenstream::{TokenStream, TokenTree}; use tokenstream; -use std::cell::Cell; use std::{cmp, fmt}; -use rustc_data_structures::sync::Lrc; +use rustc_data_structures::sync::{Lrc, Lock}; #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug, Copy)] pub enum BinOpToken { @@ -614,15 +613,8 @@ pub fn is_op(tok: &Token) -> bool { } } -pub struct LazyTokenStream(Cell>); - -impl Clone for LazyTokenStream { - fn clone(&self) -> Self { - let opt_stream = self.0.take(); - self.0.set(opt_stream.clone()); - LazyTokenStream(Cell::new(opt_stream)) - } -} +#[derive(Clone)] +pub struct LazyTokenStream(Lock>); impl cmp::Eq for LazyTokenStream {} impl PartialEq for LazyTokenStream { @@ -639,15 +631,14 @@ impl fmt::Debug for LazyTokenStream { impl LazyTokenStream { pub fn new() -> Self { - LazyTokenStream(Cell::new(None)) + LazyTokenStream(Lock::new(None)) } pub fn force TokenStream>(&self, f: F) -> TokenStream { - let mut opt_stream = self.0.take(); + let mut opt_stream = self.0.lock(); if opt_stream.is_none() { - opt_stream = Some(f()); + *opt_stream = Some(f()); } - self.0.set(opt_stream.clone()); opt_stream.clone().unwrap() } } -- cgit 1.4.1-3-g733a5 From c979189867d4af957a50cd4ad39c230dc06344f9 Mon Sep 17 00:00:00 2001 From: John Kåre Alsaker Date: Thu, 15 Feb 2018 10:52:26 +0100 Subject: Make ParseSess thread-safe --- src/libsyntax/parse/lexer/mod.rs | 9 ++++----- src/libsyntax/parse/mod.rs | 17 ++++++++--------- 2 files changed, 12 insertions(+), 14 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs index 068929c8948..5db279c7681 100644 --- a/src/libsyntax/parse/lexer/mod.rs +++ b/src/libsyntax/parse/lexer/mod.rs @@ -1781,7 +1781,6 @@ mod tests { use errors; use feature_gate::UnstableFeatures; use parse::token; - use std::cell::RefCell; use std::collections::HashSet; use std::io; use std::path::PathBuf; @@ -1797,12 +1796,12 @@ mod tests { span_diagnostic: errors::Handler::with_emitter(true, false, Box::new(emitter)), unstable_features: UnstableFeatures::from_environment(), config: CrateConfig::new(), - included_mod_stack: RefCell::new(Vec::new()), + included_mod_stack: Lock::new(Vec::new()), code_map: cm, - missing_fragment_specifiers: RefCell::new(HashSet::new()), - raw_identifier_spans: RefCell::new(Vec::new()), + missing_fragment_specifiers: Lock::new(HashSet::new()), + raw_identifier_spans: Lock::new(Vec::new()), registered_diagnostics: Lock::new(ErrorMap::new()), - non_modrs_mods: RefCell::new(vec![]), + non_modrs_mods: Lock::new(vec![]), } } diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 7b39db16ac2..d5103eb1833 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -23,7 +23,6 @@ use symbol::Symbol; use tokenstream::{TokenStream, TokenTree}; use diagnostics::plugin::ErrorMap; -use std::cell::RefCell; use std::collections::HashSet; use std::iter; use std::path::{Path, PathBuf}; @@ -47,17 +46,17 @@ pub struct ParseSess { pub span_diagnostic: Handler, pub unstable_features: UnstableFeatures, pub config: CrateConfig, - pub missing_fragment_specifiers: RefCell>, + pub missing_fragment_specifiers: Lock>, /// Places where raw identifiers were used. This is used for feature gating /// raw identifiers - pub raw_identifier_spans: RefCell>, + pub raw_identifier_spans: Lock>, /// The registered diagnostics codes pub registered_diagnostics: Lock, // 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>, + pub non_modrs_mods: Lock>, /// Used to determine and report recursive mod inclusions - included_mod_stack: RefCell>, + included_mod_stack: Lock>, code_map: Lrc, } @@ -76,12 +75,12 @@ impl ParseSess { span_diagnostic: handler, unstable_features: UnstableFeatures::from_environment(), config: HashSet::new(), - missing_fragment_specifiers: RefCell::new(HashSet::new()), - raw_identifier_spans: RefCell::new(Vec::new()), + missing_fragment_specifiers: Lock::new(HashSet::new()), + raw_identifier_spans: Lock::new(Vec::new()), registered_diagnostics: Lock::new(ErrorMap::new()), - included_mod_stack: RefCell::new(vec![]), + included_mod_stack: Lock::new(vec![]), code_map, - non_modrs_mods: RefCell::new(vec![]), + non_modrs_mods: Lock::new(vec![]), } } -- cgit 1.4.1-3-g733a5