Skip to content

Commit

Permalink
Fix the definition of parameter variables with duplicate names in adv…
Browse files Browse the repository at this point in the history
…anced resolver

Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa committed Feb 18, 2024
1 parent c421ce1 commit 4a24973
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
9 changes: 6 additions & 3 deletions kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions kclvm/sema/src/core/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ impl GlobalState {
///
/// `module_info`: [Option<&ModuleInfo>]
/// the module import information
/// `local`: [bool]
/// look up in current scope
///
/// # Returns
///
Expand All @@ -75,12 +77,14 @@ impl GlobalState {
name: &str,
scope_ref: ScopeRef,
module_info: Option<&ModuleInfo>,
local: bool,
) -> Option<SymbolRef> {
match self.scopes.get_scope(&scope_ref)?.look_up_def(
name,
&self.scopes,
&self.symbols,
module_info,
local,
) {
None => self
.symbols
Expand Down
12 changes: 10 additions & 2 deletions kclvm/sema/src/core/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub trait Scope {
scope_data: &ScopeData,
symbol_data: &Self::SymbolData,
module_info: Option<&ModuleInfo>,
local: bool,
) -> Option<SymbolRef>;

fn get_all_defs(
Expand Down Expand Up @@ -229,6 +230,7 @@ impl Scope for RootSymbolScope {
_scope_data: &ScopeData,
symbol_data: &Self::SymbolData,
module_info: Option<&ModuleInfo>,
_local: bool,
) -> Option<SymbolRef> {
let package_symbol = symbol_data.get_symbol(self.owner)?;

Expand Down Expand Up @@ -380,6 +382,7 @@ impl Scope for LocalSymbolScope {
scope_data: &ScopeData,
symbol_data: &Self::SymbolData,
module_info: Option<&ModuleInfo>,
local: bool,
) -> Option<SymbolRef> {
match self.defs.get(name) {
Some(symbol_ref) => return Some(*symbol_ref),
Expand All @@ -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)
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions kclvm/tools/src/LSP/src/goto_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
6 changes: 5 additions & 1 deletion kclvm/tools/src/LSP/src/test_data/goto_def_test/goto_def.k
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@ b = True
command: [str] = [
if b:
"a"
]
]

f = lambda a: [str], b: [str], c: [str] -> [str] {
c + a + b
}

0 comments on commit 4a24973

Please sign in to comment.