Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use Arc<Mutex<..> to support internal modifications to CachedScope #909

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading