Skip to content

Commit 11ccc4c

Browse files
committed
Make LazyTokenStream thread-safe
1 parent ab8b961 commit 11ccc4c

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

src/libsyntax/parse/token.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ use syntax_pos::{self, Span, FileName};
2525
use tokenstream::{TokenStream, TokenTree};
2626
use tokenstream;
2727

28-
use std::cell::Cell;
2928
use std::{cmp, fmt};
30-
use rustc_data_structures::sync::Lrc;
29+
use rustc_data_structures::sync::{Lrc, Lock};
3130

3231
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug, Copy)]
3332
pub enum BinOpToken {
@@ -614,15 +613,8 @@ pub fn is_op(tok: &Token) -> bool {
614613
}
615614
}
616615

617-
pub struct LazyTokenStream(Cell<Option<TokenStream>>);
618-
619-
impl Clone for LazyTokenStream {
620-
fn clone(&self) -> Self {
621-
let opt_stream = self.0.take();
622-
self.0.set(opt_stream.clone());
623-
LazyTokenStream(Cell::new(opt_stream))
624-
}
625-
}
616+
#[derive(Clone)]
617+
pub struct LazyTokenStream(Lock<Option<TokenStream>>);
626618

627619
impl cmp::Eq for LazyTokenStream {}
628620
impl PartialEq for LazyTokenStream {
@@ -639,15 +631,14 @@ impl fmt::Debug for LazyTokenStream {
639631

640632
impl LazyTokenStream {
641633
pub fn new() -> Self {
642-
LazyTokenStream(Cell::new(None))
634+
LazyTokenStream(Lock::new(None))
643635
}
644636

645637
pub fn force<F: FnOnce() -> TokenStream>(&self, f: F) -> TokenStream {
646-
let mut opt_stream = self.0.take();
638+
let mut opt_stream = self.0.lock();
647639
if opt_stream.is_none() {
648-
opt_stream = Some(f());
640+
*opt_stream = Some(f());
649641
}
650-
self.0.set(opt_stream.clone());
651642
opt_stream.clone().unwrap()
652643
}
653644
}

0 commit comments

Comments
 (0)