Skip to content

Commit

Permalink
feat: use Arc<Mutex<..> to support internal modifications to CachedSc…
Browse files Browse the repository at this point in the history
…ope (#909)

Signed-off-by: never <[email protected]>
  • Loading branch information
NeverRaR authored Nov 23, 2023
1 parent 971555f commit 268837b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
26 changes: 18 additions & 8 deletions kclvm/sema/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod tests;

use indexmap::IndexMap;
use kclvm_error::diagnostic::Range;
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::{cell::RefCell, rc::Rc};

use crate::lint::{CombinedLintPass, Linter};
Expand Down Expand Up @@ -174,18 +174,28 @@ pub fn resolve_program(program: &mut Program) -> ProgramScope {
pub fn resolve_program_with_opts(
program: &mut Program,
opts: Options,
cached_scope: Option<CachedScope>,
cached_scope: Option<Arc<Mutex<CachedScope>>>,
) -> ProgramScope {
pre_process_program(program, &opts);
let mut resolver = Resolver::new(program, opts.clone());
if let Some(mut cached_scope) = cached_scope {
cached_scope.update(program);
resolver.scope_map = cached_scope.scope_map;
resolver.scope_map.remove(kclvm_ast::MAIN_PKG);
resolver.node_ty_map = cached_scope.node_ty_map
}
resolver.resolve_import();
if let Some(cached_scope) = cached_scope.as_ref() {
if let Ok(mut cached_scope) = cached_scope.try_lock() {
cached_scope.update(program);
resolver.scope_map = cached_scope.scope_map.clone();
resolver.scope_map.remove(kclvm_ast::MAIN_PKG);
resolver.node_ty_map = cached_scope.node_ty_map.clone()
}
}
let scope = resolver.check_and_lint(kclvm_ast::MAIN_PKG);
if let Some(cached_scope) = cached_scope.as_ref() {
if let Ok(mut cached_scope) = cached_scope.try_lock() {
cached_scope.update(program);
cached_scope.scope_map = scope.scope_map.clone();
cached_scope.node_ty_map = scope.node_ty_map.clone();
cached_scope.scope_map.remove(kclvm_ast::MAIN_PKG);
}
}

if opts.type_alise {
let type_alias_mapping = resolver.ctx.type_alias_mapping.clone();
Expand Down
3 changes: 2 additions & 1 deletion kclvm/sema/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use kclvm_parser::ParseSession;
use kclvm_parser::{load_program, parse_program};
use std::path::Path;
use std::sync::Arc;
use std::sync::Mutex;

#[test]
fn test_scope() {
Expand Down Expand Up @@ -59,7 +60,7 @@ fn test_resolve_program_with_cache() {
},
None,
);
let cached_scope = CachedScope::new(&scope, &program);
let cached_scope = Arc::new(Mutex::new(CachedScope::new(&scope, &program)));
let scope = resolve_program_with_opts(
&mut program,
Options {
Expand Down

0 comments on commit 268837b

Please sign in to comment.