Skip to content

Commit

Permalink
Merge pull request trishume#391 from rhysd/issue-334
Browse files Browse the repository at this point in the history
Fix crash on lazy initialization
  • Loading branch information
trishume committed Nov 8, 2021
2 parents b4b5f52 + 43d72bf commit 54de0d1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ fancy-regex = { version = "0.7", optional = true }
walkdir = "2.0"
regex-syntax = { version = "0.6", optional = true }
lazy_static = "1.0"
lazycell = "1.0"
bitflags = "1.0.4"
plist = { version = "1", optional = true }
bincode = { version = "1.0", optional = true }
Expand All @@ -33,6 +32,7 @@ fnv = { version = "1.0", optional = true }
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
once_cell = "1.8"

[dev-dependencies]
criterion = { version = "0.3", features = [ "html_reports" ] }
Expand Down
23 changes: 9 additions & 14 deletions src/parsing/regex.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use lazycell::AtomicLazyCell;
use once_cell::sync::OnceCell;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::error::Error;

Expand All @@ -10,7 +10,7 @@ use std::error::Error;
#[derive(Debug)]
pub struct Regex {
regex_str: String,
regex: AtomicLazyCell<regex_impl::Regex>,
regex: OnceCell<regex_impl::Regex>,
}

/// A region contains text positions for capture groups in a match result.
Expand All @@ -27,7 +27,7 @@ impl Regex {
pub fn new(regex_str: String) -> Self {
Self {
regex_str,
regex: AtomicLazyCell::new(),
regex: OnceCell::new(),
}
}

Expand Down Expand Up @@ -65,22 +65,17 @@ impl Regex {
}

fn regex(&self) -> &regex_impl::Regex {
if let Some(regex) = self.regex.borrow() {
regex
} else {
let regex =
regex_impl::Regex::new(&self.regex_str).expect("regex string should be pre-tested");
self.regex.fill(regex).ok();
self.regex.borrow().unwrap()
}
self.regex.get_or_init(|| {
regex_impl::Regex::new(&self.regex_str).expect("regex string should be pre-tested")
})
}
}

impl Clone for Regex {
fn clone(&self) -> Self {
Regex {
regex_str: self.regex_str.clone(),
regex: AtomicLazyCell::new(),
regex: OnceCell::new(),
}
}
}
Expand Down Expand Up @@ -275,9 +270,9 @@ mod tests {
fn caches_compiled_regex() {
let regex = Regex::new(String::from(r"\w+"));

assert!(!regex.regex.filled());
assert!(regex.regex.get().is_none());
assert!(regex.is_match("test"));
assert!(regex.regex.filled());
assert!(regex.regex.get().is_some());
}

#[test]
Expand Down
22 changes: 8 additions & 14 deletions src/parsing/syntax_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use std::io::{self, BufRead, BufReader};
use std::fs::File;
use std::mem;

use lazycell::AtomicLazyCell;
use super::regex::Regex;
use crate::parsing::syntax_definition::ContextId;
use once_cell::sync::OnceCell;

/// A syntax set holds multiple syntaxes that have been linked together.
///
Expand All @@ -37,8 +37,8 @@ pub struct SyntaxSet {
/// Stores the syntax index for every path that was loaded
path_syntaxes: Vec<(String, usize)>,

#[serde(skip_serializing, skip_deserializing, default = "AtomicLazyCell::new")]
first_line_cache: AtomicLazyCell<FirstLineCache>,
#[serde(skip_serializing, skip_deserializing, default = "OnceCell::new")]
first_line_cache: OnceCell<FirstLineCache>,
/// Metadata, e.g. indent and commenting information.
///
/// NOTE: if serializing, you should handle metadata manually; that is, you should serialize and
Expand Down Expand Up @@ -110,7 +110,7 @@ impl Clone for SyntaxSet {
contexts: self.contexts.clone(),
path_syntaxes: self.path_syntaxes.clone(),
// Will need to be re-initialized
first_line_cache: AtomicLazyCell::new(),
first_line_cache: OnceCell::new(),
#[cfg(feature = "metadata")]
metadata: self.metadata.clone(),
}
Expand All @@ -123,14 +123,13 @@ impl Default for SyntaxSet {
syntaxes: Vec::new(),
contexts: Vec::new(),
path_syntaxes: Vec::new(),
first_line_cache: AtomicLazyCell::new(),
first_line_cache: OnceCell::new(),
#[cfg(feature = "metadata")]
metadata: Metadata::default(),
}
}
}


impl SyntaxSet {
pub fn new() -> SyntaxSet {
SyntaxSet::default()
Expand Down Expand Up @@ -353,13 +352,8 @@ impl SyntaxSet {
}

fn first_line_cache(&self) -> &FirstLineCache {
if let Some(cache) = self.first_line_cache.borrow() {
cache
} else {
let cache = FirstLineCache::new(self.syntaxes());
self.first_line_cache.fill(cache).ok();
self.first_line_cache.borrow().unwrap()
}
self.first_line_cache
.get_or_init(|| FirstLineCache::new(self.syntaxes()))
}

pub fn find_unlinked_contexts(&self) -> BTreeSet<String> {
Expand Down Expand Up @@ -624,7 +618,7 @@ impl SyntaxSetBuilder {
syntaxes,
contexts: all_contexts,
path_syntaxes,
first_line_cache: AtomicLazyCell::new(),
first_line_cache: OnceCell::new(),
#[cfg(feature = "metadata")]
metadata,
}
Expand Down

0 comments on commit 54de0d1

Please sign in to comment.