chore: add TraitImpl::declared_in

This commit is contained in:
Shunsuke Shibayama 2024-02-11 13:08:03 +09:00
parent 2a26cc8a1d
commit 3173db429a
8 changed files with 92 additions and 14 deletions

View file

@ -58,7 +58,7 @@ impl SharedCompilerResource {
self.py_mod_cache.initialize();
self.index.initialize();
self.graph.initialize();
// self.trait_impls.initialize();
self.trait_impls.initialize();
self.promises.initialize();
self.errors.clear();
self.warns.clear();
@ -79,6 +79,7 @@ impl SharedCompilerResource {
}
self.index.remove_path(path);
// self.graph.remove(path);
self.trait_impls.remove_by_path(path);
self.promises.remove(path);
self.errors.remove(path);
self.warns.remove(path);
@ -90,6 +91,7 @@ impl SharedCompilerResource {
self.py_mod_cache.remove(path);
self.index.remove_path(path);
// self.graph.remove(path);
self.trait_impls.remove_by_path(path);
self.promises.remove(path);
self.errors.remove(path);
self.warns.remove(path);
@ -99,6 +101,7 @@ impl SharedCompilerResource {
self.mod_cache.rename_path(old, new.clone());
self.py_mod_cache.rename_path(old, new.clone());
self.index.rename_path(old, new.clone());
self.trait_impls.rename_path(old, new.clone());
self.graph.rename_path(old, new.clone());
self.promises.rename(old, new);
}

View file

@ -3,6 +3,7 @@ use std::fmt;
use std::hash::Hash;
use erg_common::dict::Dict;
use erg_common::pathutil::NormalizedPathBuf;
use erg_common::set::Set;
use erg_common::shared::{
MappedRwLockReadGuard, MappedRwLockWriteGuard, RwLockReadGuard, RwLockWriteGuard, Shared,
@ -61,6 +62,12 @@ impl TraitImpls {
self.cache.remove(path)
}
pub fn remove_by_path(&mut self, path: &NormalizedPathBuf) {
for impls in self.cache.values_mut() {
impls.retain(|impl_| impl_.declared_in.as_ref() != Some(path));
}
}
pub fn rename<Q: Eq + Hash + ?Sized>(&mut self, old: &Q, new: Str)
where
Str: Borrow<Q>,
@ -70,8 +77,24 @@ impl TraitImpls {
}
}
pub fn rename_path(&mut self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
for impls in self.cache.values_mut() {
impls.inplace_map(|mut impl_| {
if impl_.declared_in.as_ref() == Some(old) {
impl_.declared_in = Some(new.clone());
impl_
} else {
impl_
}
});
}
}
pub fn initialize(&mut self) {
self.cache.clear();
for impls in self.cache.values_mut() {
impls.retain(|impl_| impl_.declared_in.is_none());
}
self.cache.retain(|_, impls| !impls.is_empty());
}
}
@ -125,11 +148,15 @@ impl SharedTraitImpls {
self.0.borrow_mut().register(name, impls);
}
pub fn remove<Q: Eq + Hash + ?Sized>(&self, path: &Q) -> Option<Set<TraitImpl>>
pub fn remove<Q: Eq + Hash + ?Sized>(&self, qual_name: &Q) -> Option<Set<TraitImpl>>
where
Str: Borrow<Q>,
{
self.0.borrow_mut().remove(path)
self.0.borrow_mut().remove(qual_name)
}
pub fn remove_by_path(&self, path: &NormalizedPathBuf) {
self.0.borrow_mut().remove_by_path(path);
}
pub fn rename<Q: Eq + Hash + ?Sized>(&self, old: &Q, new: Str)
@ -139,6 +166,10 @@ impl SharedTraitImpls {
self.0.borrow_mut().rename(old, new);
}
pub fn rename_path(&self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
self.0.borrow_mut().rename_path(old, new);
}
pub fn ref_inner(&self) -> MappedRwLockReadGuard<Dict<Str, Set<TraitImpl>>> {
RwLockReadGuard::map(self.0.borrow(), |tis| &tis.cache)
}