From 268837bcdaa8d083826ff3bc774f53fba94996b1 Mon Sep 17 00:00:00 2001 From: NeverRaR <44917563+NeverRaR@users.noreply.github.com> Date: Thu, 23 Nov 2023 12:41:59 +0800 Subject: [PATCH] feat: use Arc to support internal modifications to CachedScope (#909) Signed-off-by: never --- kclvm/sema/src/resolver/mod.rs | 26 ++++++++++++++++++-------- kclvm/sema/src/resolver/tests.rs | 3 ++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/kclvm/sema/src/resolver/mod.rs b/kclvm/sema/src/resolver/mod.rs index 554f489c0..3da10e5ad 100644 --- a/kclvm/sema/src/resolver/mod.rs +++ b/kclvm/sema/src/resolver/mod.rs @@ -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}; @@ -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, + cached_scope: Option>>, ) -> 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(); diff --git a/kclvm/sema/src/resolver/tests.rs b/kclvm/sema/src/resolver/tests.rs index 19fae70b5..b0030bb9c 100644 --- a/kclvm/sema/src/resolver/tests.rs +++ b/kclvm/sema/src/resolver/tests.rs @@ -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() { @@ -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 {