From 4a249732aebd1ad0eb03c0440e5ff1fbd46bfc18 Mon Sep 17 00:00:00 2001 From: he1pa <18012015693@163.com> Date: Sun, 18 Feb 2024 17:16:45 +0800 Subject: [PATCH] Fix the definition of parameter variables with duplicate names in advanced resolver Signed-off-by: he1pa <18012015693@163.com> --- kclvm/sema/src/advanced_resolver/node.rs | 9 ++++++--- kclvm/sema/src/core/global_state.rs | 4 ++++ kclvm/sema/src/core/scope.rs | 12 ++++++++++-- kclvm/tools/src/LSP/src/goto_def.rs | 15 +++++++++++++++ .../LSP/src/test_data/goto_def_test/goto_def.k | 6 +++++- 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/kclvm/sema/src/advanced_resolver/node.rs b/kclvm/sema/src/advanced_resolver/node.rs index 16b084813..28f47413f 100644 --- a/kclvm/sema/src/advanced_resolver/node.rs +++ b/kclvm/sema/src/advanced_resolver/node.rs @@ -810,9 +810,12 @@ impl<'ctx> AdvancedResolver<'ctx> { let first_name = names.get(0)?; let cur_scope = *self.ctx.scopes.last().unwrap(); - let mut first_symbol = - self.gs - .look_up_symbol(&first_name.node, cur_scope, self.get_current_module_info()); + let mut first_symbol = self.gs.look_up_symbol( + &first_name.node, + cur_scope, + self.get_current_module_info(), + self.ctx.maybe_def, + ); if first_symbol.is_none() { //maybe import package symbol let module_info = self.get_current_module_info().unwrap(); diff --git a/kclvm/sema/src/core/global_state.rs b/kclvm/sema/src/core/global_state.rs index 75655f4b9..f7ea3a927 100644 --- a/kclvm/sema/src/core/global_state.rs +++ b/kclvm/sema/src/core/global_state.rs @@ -65,6 +65,8 @@ impl GlobalState { /// /// `module_info`: [Option<&ModuleInfo>] /// the module import information + /// `local`: [bool] + /// look up in current scope /// /// # Returns /// @@ -75,12 +77,14 @@ impl GlobalState { name: &str, scope_ref: ScopeRef, module_info: Option<&ModuleInfo>, + local: bool, ) -> Option { match self.scopes.get_scope(&scope_ref)?.look_up_def( name, &self.scopes, &self.symbols, module_info, + local, ) { None => self .symbols diff --git a/kclvm/sema/src/core/scope.rs b/kclvm/sema/src/core/scope.rs index ac5010d04..c8c0b992b 100644 --- a/kclvm/sema/src/core/scope.rs +++ b/kclvm/sema/src/core/scope.rs @@ -24,6 +24,7 @@ pub trait Scope { scope_data: &ScopeData, symbol_data: &Self::SymbolData, module_info: Option<&ModuleInfo>, + local: bool, ) -> Option; fn get_all_defs( @@ -229,6 +230,7 @@ impl Scope for RootSymbolScope { _scope_data: &ScopeData, symbol_data: &Self::SymbolData, module_info: Option<&ModuleInfo>, + _local: bool, ) -> Option { let package_symbol = symbol_data.get_symbol(self.owner)?; @@ -380,6 +382,7 @@ impl Scope for LocalSymbolScope { scope_data: &ScopeData, symbol_data: &Self::SymbolData, module_info: Option<&ModuleInfo>, + local: bool, ) -> Option { match self.defs.get(name) { Some(symbol_ref) => return Some(*symbol_ref), @@ -392,8 +395,13 @@ impl Scope for LocalSymbolScope { return Some(symbol_ref); } }; - let parent = scope_data.get_scope(&self.parent)?; - parent.look_up_def(name, scope_data, symbol_data, module_info) + + if local { + None + } else { + let parent = scope_data.get_scope(&self.parent)?; + parent.look_up_def(name, scope_data, symbol_data, module_info, false) + } } } } diff --git a/kclvm/tools/src/LSP/src/goto_def.rs b/kclvm/tools/src/LSP/src/goto_def.rs index dcedc7402..ae88a4e76 100644 --- a/kclvm/tools/src/LSP/src/goto_def.rs +++ b/kclvm/tools/src/LSP/src/goto_def.rs @@ -679,4 +679,19 @@ mod tests { let res = goto_definition_with_gs(&program, &pos, &gs); compare_goto_res(res, (&file, 88, 0, 88, 1)); } + + #[test] + #[bench_test] + fn lambda_local_var_test() { + let (file, program, _, _, gs) = compile_test_file("src/test_data/goto_def_test/goto_def.k"); + + let pos = KCLPos { + filename: file.clone(), + line: 96, + column: Some(9), + }; + + let res = goto_definition_with_gs(&program, &pos, &gs); + compare_goto_res(res, (&file, 94, 11, 94, 12)); + } } diff --git a/kclvm/tools/src/LSP/src/test_data/goto_def_test/goto_def.k b/kclvm/tools/src/LSP/src/test_data/goto_def_test/goto_def.k index e40e42fc9..aaa5ba56a 100644 --- a/kclvm/tools/src/LSP/src/test_data/goto_def_test/goto_def.k +++ b/kclvm/tools/src/LSP/src/test_data/goto_def_test/goto_def.k @@ -90,4 +90,8 @@ b = True command: [str] = [ if b: "a" -] \ No newline at end of file +] + +f = lambda a: [str], b: [str], c: [str] -> [str] { + c + a + b +}