chore: use NormalizedPathBuf

This commit is contained in:
Shunsuke Shibayama 2023-10-08 18:16:31 +09:00
parent 23b696ca1e
commit e6a75e80df
10 changed files with 78 additions and 89 deletions

View file

@ -324,8 +324,8 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
continue; continue;
}; };
self.shared.rename_path( self.shared.rename_path(
&old_uri.to_file_path().unwrap(), &old_uri.to_file_path().unwrap().into(),
new_uri.to_file_path().unwrap(), new_uri.to_file_path().unwrap().into(),
); );
self.restore_entry(new_uri, entry); self.restore_entry(new_uri, entry);
} }

View file

@ -758,7 +758,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
/// the cache is deleted after each analysis. /// the cache is deleted after each analysis.
pub(crate) fn get_checker(&self, path: PathBuf) -> Checker { pub(crate) fn get_checker(&self, path: PathBuf) -> Checker {
let shared = self.shared.clone(); let shared = self.shared.clone();
shared.clear(&path); shared.clear(&NormalizedPathBuf::from(&path));
Checker::inherit(self.cfg.inherit(path), shared) Checker::inherit(self.cfg.inherit(path), shared)
} }
@ -914,7 +914,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
} }
pub(crate) fn clear_cache(&mut self, uri: &NormalizedUrl) { pub(crate) fn clear_cache(&mut self, uri: &NormalizedUrl) {
let path = util::uri_to_path(uri); let path = NormalizedPathBuf::from(util::uri_to_path(uri));
self.shared.clear(&path); self.shared.clear(&path);
} }

View file

@ -265,7 +265,7 @@ impl PackageBuilder {
if self if self
.shared .shared
.graph .graph
.inc_ref(&from_path, import_path.to_path_buf()) .inc_ref(&from_path, import_path.clone())
.is_err() .is_err()
{ {
self.submodules.push(from_path.clone()); self.submodules.push(from_path.clone());
@ -299,7 +299,7 @@ impl PackageBuilder {
/// Launch the analysis processes in order according to the dependency graph. /// Launch the analysis processes in order according to the dependency graph.
fn execute(&mut self, ast: AST, mode: &str) -> Result<CompleteArtifact, IncompleteArtifact> { fn execute(&mut self, ast: AST, mode: &str) -> Result<CompleteArtifact, IncompleteArtifact> {
log!(info "Start to spawn dependencies processes"); log!(info "Start to spawn dependencies processes");
let path = self.cfg.input.path().to_path_buf(); let path = NormalizedPathBuf::from(self.cfg.input.path());
let mut graph = self.shared.graph.clone_inner(); let mut graph = self.shared.graph.clone_inner();
let mut ancestors = graph.ancestors(&path).into_vec(); let mut ancestors = graph.ancestors(&path).into_vec();
while let Some(ancestor) = ancestors.pop() { while let Some(ancestor) = ancestors.pop() {

View file

@ -50,7 +50,7 @@ pub enum SubstituteResult {
} }
impl Context { impl Context {
pub(crate) fn mod_registered(&self, path: &Path) -> bool { pub(crate) fn mod_registered(&self, path: &NormalizedPathBuf) -> bool {
(self.shared.is_some() && self.promises().is_registered(path)) || self.mod_cached(path) (self.shared.is_some() && self.promises().is_registered(path)) || self.mod_cached(path)
} }
@ -63,15 +63,18 @@ impl Context {
pub(crate) fn get_mod_with_path(&self, path: &Path) -> Option<&Context> { pub(crate) fn get_mod_with_path(&self, path: &Path) -> Option<&Context> {
if self.module_path() == path { if self.module_path() == path {
return self.get_module(); return self.get_module();
} else if let Some(ctx) = self.get_module_from_stack(&NormalizedPathBuf::from(path)) { }
let path = NormalizedPathBuf::from(path);
if let Some(ctx) = self.get_module_from_stack(&path) {
return Some(ctx); return Some(ctx);
} }
if self.shared.is_some() && self.promises().is_registered(path) && !self.mod_cached(path) { if self.shared.is_some() && self.promises().is_registered(&path) && !self.mod_cached(&path)
let _result = self.promises().join(path); {
let _result = self.promises().join(&path);
} }
self.opt_mod_cache()? self.opt_mod_cache()?
.raw_ref_ctx(path) .raw_ref_ctx(&path)
.or_else(|| self.opt_py_mod_cache()?.raw_ref_ctx(path)) .or_else(|| self.opt_py_mod_cache()?.raw_ref_ctx(&path))
.map(|mod_ctx| &mod_ctx.context) .map(|mod_ctx| &mod_ctx.context)
} }

View file

@ -1,7 +1,6 @@
use std::borrow::Borrow; use std::borrow::Borrow;
use std::fmt; use std::fmt;
use std::hash::Hash; use std::hash::Hash;
use std::path::Path;
use erg_common::config::ErgConfig; use erg_common::config::ErgConfig;
use erg_common::dict::Dict; use erg_common::dict::Dict;
@ -166,7 +165,7 @@ impl ModuleCache {
get_similar_name(self.cache.iter().map(|(v, _)| v.to_str().unwrap()), name).map(Str::rc) get_similar_name(self.cache.iter().map(|(v, _)| v.to_str().unwrap()), name).map(Str::rc)
} }
pub fn rename_path(&mut self, old: &Path, new: NormalizedPathBuf) { pub fn rename_path(&mut self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
if let Some(entry) = self.cache.remove(old) { if let Some(entry) = self.cache.remove(old) {
self.cache.insert(new, entry); self.cache.insert(new, entry);
} }
@ -302,7 +301,7 @@ impl SharedModuleCache {
self.register(builtin_path, None, None, builtin.module); self.register(builtin_path, None, None, builtin.module);
} }
pub fn rename_path<P: Into<NormalizedPathBuf>>(&self, path: &Path, new: P) { pub fn rename_path<P: Into<NormalizedPathBuf>>(&self, path: &NormalizedPathBuf, new: P) {
self.0.borrow_mut().rename_path(path, new.into()); self.0.borrow_mut().rename_path(path, new.into());
} }

View file

@ -1,5 +1,3 @@
use std::path::Path;
use erg_common::pathutil::NormalizedPathBuf; use erg_common::pathutil::NormalizedPathBuf;
use erg_common::set::Set; use erg_common::set::Set;
use erg_common::shared::Shared; use erg_common::shared::Shared;
@ -38,11 +36,10 @@ impl SharedCompileErrors {
self.0.borrow_mut().clear(); self.0.borrow_mut().clear();
} }
pub fn remove(&self, path: &Path) { pub fn remove(&self, path: &NormalizedPathBuf) {
let path = NormalizedPathBuf::from(path);
self.0 self.0
.borrow_mut() .borrow_mut()
.retain(|e| NormalizedPathBuf::from(e.input.path()) != path); .retain(|e| &NormalizedPathBuf::from(e.input.path()) != path);
} }
pub fn raw_iter(&self) -> impl Iterator<Item = &CompileError> { pub fn raw_iter(&self) -> impl Iterator<Item = &CompileError> {

View file

@ -1,5 +1,3 @@
use std::path::{Path, PathBuf};
use erg_common::config::ErgConfig; use erg_common::config::ErgConfig;
use erg_common::pathutil::NormalizedPathBuf; use erg_common::pathutil::NormalizedPathBuf;
@ -38,10 +36,7 @@ impl SharedCompilerResource {
index: SharedModuleIndex::new(), index: SharedModuleIndex::new(),
graph: graph.clone(), graph: graph.clone(),
trait_impls: SharedTraitImpls::new(), trait_impls: SharedTraitImpls::new(),
promises: SharedPromises::new( promises: SharedPromises::new(graph, NormalizedPathBuf::from(cfg.input.path())),
graph,
cfg.input.path().canonicalize().unwrap_or_default(),
),
errors: SharedCompileErrors::new(), errors: SharedCompileErrors::new(),
warns: SharedCompileWarnings::new(), warns: SharedCompileWarnings::new(),
}; };
@ -69,7 +64,7 @@ impl SharedCompilerResource {
/// Clear all information about the module. /// Clear all information about the module.
/// Graph information is not cleared (due to ELS). /// Graph information is not cleared (due to ELS).
pub fn clear(&self, path: &Path) { pub fn clear(&self, path: &NormalizedPathBuf) {
for child in self.graph.children(path) { for child in self.graph.children(path) {
self.clear(&child); self.clear(&child);
} }
@ -82,7 +77,7 @@ impl SharedCompilerResource {
self.warns.remove(path); self.warns.remove(path);
} }
pub fn rename_path(&self, old: &Path, new: PathBuf) { pub fn rename_path(&self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
self.mod_cache.rename_path(old, new.clone()); self.mod_cache.rename_path(old, new.clone());
self.py_mod_cache.rename_path(old, new.clone()); self.py_mod_cache.rename_path(old, new.clone());
self.index.rename_path(old, new.clone()); self.index.rename_path(old, new.clone());

View file

@ -1,5 +1,4 @@
use std::fmt; use std::fmt;
use std::path::{Path, PathBuf};
use erg_common::pathutil::NormalizedPathBuf; use erg_common::pathutil::NormalizedPathBuf;
use erg_common::set; use erg_common::set;
@ -49,14 +48,13 @@ impl ModuleGraph {
Self(Graph::new()) Self(Graph::new())
} }
pub fn get_node(&self, path: &Path) -> Option<&Node<NormalizedPathBuf, ()>> { pub fn get_node(&self, path: &NormalizedPathBuf) -> Option<&Node<NormalizedPathBuf, ()>> {
let path = NormalizedPathBuf::new(path.to_path_buf()); self.0.iter().find(|n| &n.id == path)
self.0.iter().find(|n| n.id == path)
} }
/// if `path` depends on `target`, returns `true`, else `false`. /// if `path` depends on `target`, returns `true`, else `false`.
/// if `path` not found, returns `false` /// if `path` not found, returns `false`
pub fn depends_on(&self, path: &Path, target: &Path) -> bool { pub fn depends_on(&self, path: &NormalizedPathBuf, target: &NormalizedPathBuf) -> bool {
let path = NormalizedPathBuf::new(path.to_path_buf()); let path = NormalizedPathBuf::new(path.to_path_buf());
let target = NormalizedPathBuf::new(target.to_path_buf()); let target = NormalizedPathBuf::new(target.to_path_buf());
self.0 self.0
@ -66,18 +64,16 @@ impl ModuleGraph {
.unwrap_or(false) .unwrap_or(false)
} }
pub fn children(&self, path: &Path) -> Set<NormalizedPathBuf> { pub fn children(&self, path: &NormalizedPathBuf) -> Set<NormalizedPathBuf> {
let path = NormalizedPathBuf::new(path.to_path_buf());
self.0 self.0
.iter() .iter()
.filter(|n| n.depends_on.contains(&path)) .filter(|n| n.depends_on.contains(path))
.map(|n| n.id.clone()) .map(|n| n.id.clone())
.collect() .collect()
} }
fn parents(&self, path: &Path) -> Option<&Set<NormalizedPathBuf>> { fn parents(&self, path: &NormalizedPathBuf) -> Option<&Set<NormalizedPathBuf>> {
let path = NormalizedPathBuf::new(path.to_path_buf()); self.0.iter().find(|n| &n.id == path).map(|n| &n.depends_on)
self.0.iter().find(|n| n.id == path).map(|n| &n.depends_on)
} }
/// ```erg /// ```erg
@ -85,7 +81,7 @@ impl ModuleGraph {
/// b = import "b" /// b = import "b"
/// ``` /// ```
/// -> a: child, b: parent /// -> a: child, b: parent
pub fn ancestors(&self, path: &Path) -> Set<NormalizedPathBuf> { pub fn ancestors(&self, path: &NormalizedPathBuf) -> Set<NormalizedPathBuf> {
let mut ancestors = set! {}; let mut ancestors = set! {};
if let Some(parents) = self.parents(path) { if let Some(parents) = self.parents(path) {
for parent in parents.iter() { for parent in parents.iter() {
@ -96,24 +92,25 @@ impl ModuleGraph {
ancestors ancestors
} }
pub fn add_node_if_none(&mut self, path: &Path) { pub fn add_node_if_none(&mut self, path: &NormalizedPathBuf) {
let path = NormalizedPathBuf::new(path.to_path_buf()); if self.0.iter().all(|n| &n.id != path) {
if self.0.iter().all(|n| n.id != path) { let node = Node::new(path.clone(), (), set! {});
let node = Node::new(path, (), set! {});
self.0.push(node); self.0.push(node);
} }
} }
/// returns Err (and do nothing) if this operation makes a cycle /// returns Err (and do nothing) if this operation makes a cycle
pub fn inc_ref(&mut self, referrer: &Path, depends_on: PathBuf) -> Result<(), IncRefError> { pub fn inc_ref(
&mut self,
referrer: &NormalizedPathBuf,
depends_on: NormalizedPathBuf,
) -> Result<(), IncRefError> {
self.add_node_if_none(referrer); self.add_node_if_none(referrer);
let referrer = NormalizedPathBuf::new(referrer.to_path_buf()); if self.ancestors(&depends_on).contains(referrer) && referrer != &depends_on {
let depends_on = NormalizedPathBuf::new(depends_on);
if self.ancestors(&depends_on).contains(&referrer) && referrer != depends_on {
return Err(IncRefError::CycleDetected); return Err(IncRefError::CycleDetected);
} }
if let Some(node) = self.0.iter_mut().find(|n| n.id == referrer) { if let Some(node) = self.0.iter_mut().find(|n| &n.id == referrer) {
if referrer == depends_on { if referrer == &depends_on {
return Ok(()); return Ok(());
} }
node.push_dep(depends_on); node.push_dep(depends_on);
@ -139,25 +136,22 @@ impl ModuleGraph {
} }
/// Do not erase relationships with modules that depend on `path` /// Do not erase relationships with modules that depend on `path`
pub fn remove(&mut self, path: &Path) { pub fn remove(&mut self, path: &NormalizedPathBuf) {
let path = NormalizedPathBuf::new(path.to_path_buf()); self.0.retain(|n| &n.id != path);
self.0.retain(|n| n.id != path);
for node in self.0.iter_mut() { for node in self.0.iter_mut() {
node.depends_on.retain(|p| *p != path); node.depends_on.retain(|p| p != path);
} }
} }
pub fn rename_path(&mut self, old: &Path, new: PathBuf) { pub fn rename_path(&mut self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
let old = NormalizedPathBuf::new(old.to_path_buf());
let new = NormalizedPathBuf::new(new);
for node in self.0.iter_mut() { for node in self.0.iter_mut() {
if node.id == old { if &node.id == old {
node.id = new.clone(); node.id = new.clone();
} }
if node.depends_on.contains(&old) { if node.depends_on.contains(old) {
node.depends_on.insert(new.clone()); node.depends_on.insert(new.clone());
} }
node.depends_on.retain(|p| *p != old); node.depends_on.retain(|p| p != old);
} }
} }
@ -191,7 +185,7 @@ impl SharedModuleGraph {
pub fn get_node( pub fn get_node(
&self, &self,
path: &Path, path: &NormalizedPathBuf,
) -> Option<MappedRwLockReadGuard<Node<NormalizedPathBuf, ()>>> { ) -> Option<MappedRwLockReadGuard<Node<NormalizedPathBuf, ()>>> {
if self.0.borrow().get_node(path).is_some() { if self.0.borrow().get_node(path).is_some() {
Some(RwLockReadGuard::map(self.0.borrow(), |graph| { Some(RwLockReadGuard::map(self.0.borrow(), |graph| {
@ -202,23 +196,27 @@ impl SharedModuleGraph {
} }
} }
pub fn depends_on(&self, path: &Path, target: &Path) -> bool { pub fn depends_on(&self, path: &NormalizedPathBuf, target: &NormalizedPathBuf) -> bool {
self.0.borrow().depends_on(path, target) self.0.borrow().depends_on(path, target)
} }
pub fn children(&self, path: &Path) -> Set<NormalizedPathBuf> { pub fn children(&self, path: &NormalizedPathBuf) -> Set<NormalizedPathBuf> {
self.0.borrow().children(path) self.0.borrow().children(path)
} }
pub fn ancestors(&self, path: &Path) -> Set<NormalizedPathBuf> { pub fn ancestors(&self, path: &NormalizedPathBuf) -> Set<NormalizedPathBuf> {
self.0.borrow().ancestors(path) self.0.borrow().ancestors(path)
} }
pub fn add_node_if_none(&self, path: &Path) { pub fn add_node_if_none(&self, path: &NormalizedPathBuf) {
self.0.borrow_mut().add_node_if_none(path); self.0.borrow_mut().add_node_if_none(path);
} }
pub fn inc_ref(&self, referrer: &Path, depends_on: PathBuf) -> Result<(), IncRefError> { pub fn inc_ref(
&self,
referrer: &NormalizedPathBuf,
depends_on: NormalizedPathBuf,
) -> Result<(), IncRefError> {
self.0.borrow_mut().inc_ref(referrer, depends_on) self.0.borrow_mut().inc_ref(referrer, depends_on)
} }
@ -226,11 +224,11 @@ impl SharedModuleGraph {
self.0.borrow() self.0.borrow()
} }
pub fn remove(&self, path: &Path) { pub fn remove(&self, path: &NormalizedPathBuf) {
self.0.borrow_mut().remove(path); self.0.borrow_mut().remove(path);
} }
pub fn rename_path(&self, old: &Path, new: PathBuf) { pub fn rename_path(&self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
self.0.borrow_mut().rename_path(old, new); self.0.borrow_mut().rename_path(old, new);
} }

View file

@ -1,6 +1,5 @@
use std::collections::hash_map::{Iter, Keys, Values}; use std::collections::hash_map::{Iter, Keys, Values};
use std::fmt; use std::fmt;
use std::path::{Path, PathBuf};
use erg_common::dict::Dict; use erg_common::dict::Dict;
use erg_common::pathutil::NormalizedPathBuf; use erg_common::pathutil::NormalizedPathBuf;
@ -103,7 +102,7 @@ impl ModuleIndex {
self.members.clear(); self.members.clear();
} }
pub fn remove_path(&mut self, path: &Path) { pub fn remove_path(&mut self, path: &NormalizedPathBuf) {
self.members.retain(|loc, value| { self.members.retain(|loc, value| {
value value
.referrers .referrers
@ -112,8 +111,7 @@ impl ModuleIndex {
}); });
} }
pub fn rename_path(&mut self, old: &Path, new: PathBuf) { pub fn rename_path(&mut self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
let new = NormalizedPathBuf::new(new);
let mut new_members = Dict::new(); let mut new_members = Dict::new();
for (loc, mut value) in std::mem::take(&mut self.members) { for (loc, mut value) in std::mem::take(&mut self.members) {
if value.vi.def_loc.module.as_deref() == Some(old) { if value.vi.def_loc.module.as_deref() == Some(old) {
@ -190,11 +188,11 @@ impl SharedModuleIndex {
self.0.borrow_mut().initialize(); self.0.borrow_mut().initialize();
} }
pub fn remove_path(&self, path: &Path) { pub fn remove_path(&self, path: &NormalizedPathBuf) {
self.0.borrow_mut().remove_path(path); self.0.borrow_mut().remove_path(path);
} }
pub fn rename_path(&self, old: &Path, new: PathBuf) { pub fn rename_path(&self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
self.0.borrow_mut().rename_path(old, new); self.0.borrow_mut().rename_path(old, new);
} }
} }

View file

@ -1,5 +1,4 @@
use std::fmt; use std::fmt;
use std::path::{Path, PathBuf};
use std::thread::{current, JoinHandle, ThreadId}; use std::thread::{current, JoinHandle, ThreadId};
use erg_common::consts::DEBUG_MODE; use erg_common::consts::DEBUG_MODE;
@ -92,17 +91,17 @@ impl fmt::Display for SharedPromises {
} }
impl SharedPromises { impl SharedPromises {
pub fn new(graph: SharedModuleGraph, path: PathBuf) -> Self { pub fn new(graph: SharedModuleGraph, path: NormalizedPathBuf) -> Self {
Self { Self {
graph, graph,
path: NormalizedPathBuf::new(path), path,
promises: Shared::new(Dict::new()), promises: Shared::new(Dict::new()),
} }
} }
pub fn insert<P: Into<NormalizedPathBuf>>(&self, path: P, handle: JoinHandle<()>) { pub fn insert(&self, path: impl Into<NormalizedPathBuf>, handle: JoinHandle<()>) {
let path = path.into(); let path = path.into();
if self.promises.borrow().get(&path).is_some() { if self.is_registered(&path) {
if DEBUG_MODE { if DEBUG_MODE {
panic!("already registered: {}", path.display()); panic!("already registered: {}", path.display());
} }
@ -113,40 +112,40 @@ impl SharedPromises {
.insert(path, Promise::running(handle)); .insert(path, Promise::running(handle));
} }
pub fn remove(&self, path: &Path) { pub fn remove(&self, path: &NormalizedPathBuf) -> Option<Promise> {
self.promises.borrow_mut().remove(path); self.promises.borrow_mut().remove(path)
} }
pub fn initialize(&self) { pub fn initialize(&self) {
self.promises.borrow_mut().clear(); self.promises.borrow_mut().clear();
} }
pub fn rename(&self, old: &Path, new: PathBuf) { pub fn rename(&self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
let Some(promise) = self.promises.borrow_mut().remove(old) else { let Some(promise) = self.remove(old) else {
return; return;
}; };
self.promises.borrow_mut().insert(new.into(), promise); self.promises.borrow_mut().insert(new, promise);
} }
pub fn is_registered(&self, path: &Path) -> bool { pub fn is_registered(&self, path: &NormalizedPathBuf) -> bool {
self.promises.borrow().get(path).is_some() self.promises.borrow().get(path).is_some()
} }
pub fn is_joined(&self, path: &Path) -> bool { pub fn is_joined(&self, path: &NormalizedPathBuf) -> bool {
self.promises self.promises
.borrow() .borrow()
.get(path) .get(path)
.is_some_and(|promise| promise.is_joined()) .is_some_and(|promise| promise.is_joined())
} }
pub fn can_be_joined(&self, path: &Path) -> bool { pub fn can_be_joined(&self, path: &NormalizedPathBuf) -> bool {
self.promises self.promises
.borrow() .borrow()
.get(path) .get(path)
.is_some_and(|promise| promise.is_finished()) .is_some_and(|promise| promise.is_finished())
} }
fn join_checked(&self, path: &Path, promise: Promise) -> std::thread::Result<()> { fn join_checked(&self, path: &NormalizedPathBuf, promise: Promise) -> std::thread::Result<()> {
let Promise::Running { handle, parent } = promise else { let Promise::Running { handle, parent } = promise else {
*self.promises.borrow_mut().get_mut(path).unwrap() = promise; *self.promises.borrow_mut().get_mut(path).unwrap() = promise;
return Ok(()); return Ok(());
@ -184,7 +183,7 @@ impl SharedPromises {
res res
} }
pub fn join(&self, path: &Path) -> std::thread::Result<()> { pub fn join(&self, path: &NormalizedPathBuf) -> std::thread::Result<()> {
while let Some(Promise::Joining) | None = self.promises.borrow().get(path) { while let Some(Promise::Joining) | None = self.promises.borrow().get(path) {
safe_yield(); safe_yield();
} }