Skip to content

Commit

Permalink
fix: add debug assertions to PackageBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Sep 19, 2024
1 parent ed6ad90 commit 58b22c4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
8 changes: 6 additions & 2 deletions crates/els/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{mpsc, Arc};
use std::time::Duration;

use erg_common::config::ErgConfig;
use erg_common::config::{ErgConfig, ErgMode};
use erg_common::consts::PYTHON_MODE;
use erg_common::dict::Dict;
use erg_common::env::erg_path;
Expand Down Expand Up @@ -213,7 +213,11 @@ impl Server {
#[allow(unused)]
pub fn bind_fake_client() -> FakeClient<Server> {
let (sender, receiver) = std::sync::mpsc::channel();
FakeClient::new(Server::new(ErgConfig::default(), Some(sender)), receiver)
let cfg = ErgConfig {
mode: ErgMode::LanguageServer,
..Default::default()
};
FakeClient::new(Server::new(cfg, Some(sender)), receiver)
}
}

Expand Down
22 changes: 19 additions & 3 deletions crates/erg_compiler/build_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,11 +819,19 @@ impl<ASTBuilder: ASTBuildable, HIRBuilder: Buildable>
/// Launch the analysis processes in order according to the dependency graph.
fn execute(&mut self, ast: AST, mode: &str) -> Result<CompleteArtifact, IncompleteArtifact> {
log!(info "Start to spawn dependencies processes");
let path = NormalizedPathBuf::from(self.cfg.input.path());
let root = NormalizedPathBuf::from(self.cfg.input.path());
let mut graph = self.shared.graph.clone_inner();
let deps = self.build_deps_and_module(&path, &mut graph);
let deps = self.build_deps_and_module(&root, &mut graph);
log!(info "All dependencies have started to analyze");
debug_power_assert!(self.asts.len(), ==, 0);
if self.cfg.mode != ErgMode::LanguageServer {
for path in self.shared.graph.ancestors(&root) {
assert!(
self.shared.promises.is_registered(&path),
"{path} is not registered"
);
}
}
self.finalize();
match self.main_builder.build_from_ast(ast, mode) {
Ok(artifact) => Ok(CompleteArtifact::new(
Expand Down Expand Up @@ -895,6 +903,7 @@ impl<ASTBuilder: ASTBuildable, HIRBuilder: Buildable>
self.shared.promises.wait_until_finished(path);
} else if let Some(inliner) = self.inlines.get(path).cloned() {
self.build_deps_and_module(&inliner, graph);
self.shared.promises.mark_as_joined(path.clone());
} else {
unreachable!("{path} is not found in self.inlines and self.asts");
}
Expand Down Expand Up @@ -998,10 +1007,17 @@ impl<ASTBuilder: ASTBuildable, HIRBuilder: Buildable>
}
Err(artifact) => {
let ctx = builder.pop_context().unwrap();
py_mod_cache.register(path, raw_ast, artifact.object, ctx, CheckStatus::Failed);
py_mod_cache.register(
path.clone(),
raw_ast,
artifact.object,
ctx,
CheckStatus::Failed,
);
self.shared.warns.extend(artifact.warns);
self.shared.errors.extend(artifact.errors);
}
}
self.shared.promises.mark_as_joined(path);
}
}
9 changes: 7 additions & 2 deletions crates/erg_compiler/module/promise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ impl Promise {
matches!(self, Self::Joined)
}

pub const fn is_joining(&self) -> bool {
matches!(self, Self::Joining)
}

pub fn thread_id(&self) -> Option<ThreadId> {
match self {
Self::Joined | Self::Joining => None,
Expand Down Expand Up @@ -172,8 +176,8 @@ impl SharedPromises {
if !self.graph.entries().contains(path) {
return Err(Box::new(format!("not registered: {path}")));
}
if self.graph.ancestors(path).contains(&self.root) {
// cycle detected, `self.path` must not in the dependencies
if self.graph.ancestors(path).contains(&self.root) || path == &self.root {
// cycle detected, `self.root` must not in the dependencies
// Erg analysis processes never join ancestor threads (although joining ancestors itself is allowed in Rust)
// self.wait_until_finished(path);
return Ok(());
Expand Down Expand Up @@ -206,6 +210,7 @@ impl SharedPromises {
return Ok(());
};
if handle.thread().id() == current().id() {
*self.promises.borrow_mut().get_mut(path).unwrap() = Promise::Joined;
return Ok(());
}
let res = handle.join();
Expand Down

0 comments on commit 58b22c4

Please sign in to comment.