fix: errors in other modules are ignored

This commit is contained in:
Shunsuke Shibayama 2023-06-22 16:32:10 +09:00
parent 7ba874a85c
commit eaeb659a7d
13 changed files with 214 additions and 75 deletions

View file

@ -18,4 +18,10 @@ impl SharedCompileErrors {
pub fn take(&self) -> CompileErrors {
self.0.borrow_mut().take_all().into()
}
pub fn clear(&self) {
self.0.borrow_mut().clear();
}
}
pub type SharedCompileWarnings = SharedCompileErrors;

View file

@ -1,8 +1,11 @@
use std::path::Path;
use erg_common::config::ErgConfig;
use crate::context::Context;
use super::cache::SharedModuleCache;
use super::errors::{SharedCompileErrors, SharedCompileWarnings};
use super::graph::SharedModuleGraph;
use super::impls::SharedTraitImpls;
use super::index::SharedModuleIndex;
@ -19,6 +22,8 @@ pub struct SharedCompilerResource {
/// e.g. { "Named": [(Type, Named), (Func, Named), ...], "Add": [(Nat, Add(Nat)), (Int, Add(Int)), ...], ... }
pub trait_impls: SharedTraitImpls,
pub promises: SharedPromises,
pub errors: SharedCompileErrors,
pub warns: SharedCompileWarnings,
}
impl SharedCompilerResource {
@ -32,6 +37,8 @@ impl SharedCompilerResource {
graph: SharedModuleGraph::new(),
trait_impls: SharedTraitImpls::new(),
promises: SharedPromises::new(),
errors: SharedCompileErrors::new(),
warns: SharedCompileWarnings::new(),
};
Context::init_builtins(cfg, self_.clone());
self_
@ -43,5 +50,14 @@ impl SharedCompilerResource {
self.index.initialize();
self.graph.initialize();
self.trait_impls.initialize();
self.errors.clear();
self.warns.clear();
}
pub fn clear(&self, path: &Path) {
self.mod_cache.remove(path);
self.py_mod_cache.remove(path);
self.index.remove_path(path);
self.graph.remove(path);
}
}

View file

@ -6,6 +6,7 @@ use erg_common::dict::Dict;
use erg_common::set;
use erg_common::set::Set;
use erg_common::shared::{MappedRwLockReadGuard, RwLockReadGuard, Shared};
use erg_common::Str;
use crate::varinfo::{AbsLocation, VarInfo};
@ -25,8 +26,9 @@ impl<'a> Members<'a> {
}
}
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct ModuleIndexValue {
pub name: Str,
pub vi: VarInfo,
pub referrers: Set<AbsLocation>,
}
@ -38,8 +40,12 @@ impl fmt::Display for ModuleIndexValue {
}
impl ModuleIndexValue {
pub const fn new(vi: VarInfo, referrers: Set<AbsLocation>) -> Self {
Self { vi, referrers }
pub const fn new(name: Str, vi: VarInfo, referrers: Set<AbsLocation>) -> Self {
Self {
name,
vi,
referrers,
}
}
pub fn push_ref(&mut self, referrer: AbsLocation) {
@ -65,19 +71,19 @@ impl ModuleIndex {
}
}
pub fn inc_ref(&mut self, vi: &VarInfo, referrer: AbsLocation) {
pub fn inc_ref(&mut self, name: &Str, vi: &VarInfo, referrer: AbsLocation) {
let referee = vi.def_loc.clone();
if let Some(referrers) = self.members.get_mut(&referee) {
referrers.push_ref(referrer);
} else {
let value = ModuleIndexValue::new(vi.clone(), set! {referrer});
let value = ModuleIndexValue::new(name.clone(), vi.clone(), set! {referrer});
self.members.insert(referee, value);
}
}
pub fn register(&mut self, vi: &VarInfo) {
pub fn register(&mut self, name: Str, vi: &VarInfo) {
let referee = vi.def_loc.clone();
let value = ModuleIndexValue::new(vi.clone(), set! {});
let value = ModuleIndexValue::new(name, vi.clone(), set! {});
self.members.insert(referee, value);
}
@ -90,8 +96,12 @@ impl ModuleIndex {
}
pub fn remove_path(&mut self, path: &Path) {
self.members
.retain(|loc, _| loc.module.as_deref() != Some(path));
self.members.retain(|loc, value| {
value
.referrers
.retain(|ref_loc| ref_loc.module.as_deref() != Some(path));
loc.module.as_deref() != Some(path)
});
}
}
@ -109,12 +119,12 @@ impl SharedModuleIndex {
Self(Shared::new(ModuleIndex::new()))
}
pub fn inc_ref(&self, vi: &VarInfo, referrer: AbsLocation) {
self.0.borrow_mut().inc_ref(vi, referrer);
pub fn inc_ref(&self, name: &Str, vi: &VarInfo, referrer: AbsLocation) {
self.0.borrow_mut().inc_ref(name, vi, referrer);
}
pub fn register(&self, vi: &VarInfo) {
self.0.borrow_mut().register(vi);
pub fn register(&self, name: Str, vi: &VarInfo) {
self.0.borrow_mut().register(name, vi);
}
pub fn get_refs(

View file

@ -1,4 +1,5 @@
pub mod cache;
pub mod errors;
pub mod global;
pub mod graph;
pub mod impls;
@ -6,6 +7,7 @@ pub mod index;
pub mod promise;
pub use cache::*;
pub use errors::*;
pub use global::*;
pub use graph::*;
pub use impls::*;

View file

@ -1,22 +1,44 @@
use std::path::{Path, PathBuf};
use std::thread::JoinHandle;
use std::thread::{current, JoinHandle, ThreadId};
use erg_common::dict::Dict;
use erg_common::shared::Shared;
use crate::artifact::ErrorArtifact;
#[derive(Debug)]
pub enum Promise {
Running(JoinHandle<Result<(), ErrorArtifact>>),
Running {
parent: ThreadId,
handle: JoinHandle<()>,
},
Finished,
}
impl Promise {
pub fn running(handle: JoinHandle<()>) -> Self {
Self::Running {
parent: current().id(),
handle,
}
}
pub fn is_finished(&self) -> bool {
match self {
Self::Finished => true,
Self::Running(handle) => handle.is_finished(),
Self::Running { handle, .. } => handle.is_finished(),
}
}
pub fn thread_id(&self) -> Option<ThreadId> {
match self {
Self::Finished => None,
Self::Running { handle, .. } => Some(handle.thread().id()),
}
}
pub fn parent_thread_id(&self) -> Option<ThreadId> {
match self {
Self::Finished => None,
Self::Running { parent, .. } => Some(*parent),
}
}
@ -33,24 +55,52 @@ impl SharedPromises {
Self::default()
}
pub fn insert(&self, path: PathBuf, handle: JoinHandle<Result<(), ErrorArtifact>>) {
self.0.borrow_mut().insert(path, Promise::Running(handle));
pub fn insert(&self, path: PathBuf, handle: JoinHandle<()>) {
self.0.borrow_mut().insert(path, Promise::running(handle));
}
pub fn is_registered(&self, path: &Path) -> bool {
self.0.borrow_mut().get(path).is_some()
self.0.borrow().get(path).is_some()
}
pub fn is_finished(&self, path: &Path) -> bool {
self.0
.borrow_mut()
.borrow()
.get(path)
.is_some_and(|promise| promise.is_finished())
}
pub fn join(&self, path: &Path) -> Result<(), ErrorArtifact> {
pub fn join(&self, path: &Path) -> std::thread::Result<()> {
let promise = self.0.borrow_mut().get_mut(path).unwrap().take();
let Promise::Running(handle) = promise else { todo!() };
handle.join().unwrap()
let Promise::Running{ handle, .. } = promise else { todo!() };
handle.join()
}
pub fn join_children(&self) {
let cur_id = std::thread::current().id();
let mut handles = vec![];
for (_path, promise) in self.0.borrow_mut().iter_mut() {
if promise.parent_thread_id() != Some(cur_id) {
continue;
}
if let Promise::Running { handle, .. } = promise.take() {
handles.push(handle);
}
}
for handle in handles {
let _result = handle.join();
}
}
pub fn join_all(&self) {
let mut handles = vec![];
for (_path, promise) in self.0.borrow_mut().iter_mut() {
if let Promise::Running { handle, .. } = promise.take() {
handles.push(handle);
}
}
for handle in handles {
let _result = handle.join();
}
}
}