Skip to content

Commit

Permalink
fix: fix dict to schema type upgrade for schema calling expressions (#…
Browse files Browse the repository at this point in the history
…1577)

Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa authored Aug 15, 2024
1 parent 26cc807 commit 10f0506
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 19 deletions.
61 changes: 42 additions & 19 deletions kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,28 +670,51 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
.map(|ty| ty.clone());

if let Some(ty) = call_ty {
if let TypeKind::Function(func_ty) = &ty.kind {
self.enter_local_scope(
&self.ctx.current_filename.as_ref().unwrap().clone(),
start,
end,
LocalSymbolScopeKind::Callable,
);
match &ty.kind {
TypeKind::Schema(schema_ty) => {
if !schema_ty.is_instance {
self.enter_local_scope(
&self.ctx.current_filename.as_ref().unwrap().clone(),
start,
end,
LocalSymbolScopeKind::Config,
);

if let Some(owner) = func_symbol {
let cur_scope = self.ctx.scopes.last().unwrap();
self.gs
.get_scopes_mut()
.set_owner_to_scope(*cur_scope, owner);
if let Some(owner) = func_symbol {
let cur_scope = self.ctx.scopes.last().unwrap();
self.gs
.get_scopes_mut()
.set_owner_to_scope(*cur_scope, owner);
}
self.do_arguments_symbol_resolve(&call_expr.args, &call_expr.keywords)?;

self.leave_scope();
}
}
TypeKind::Function(func_ty) => {
self.enter_local_scope(
&self.ctx.current_filename.as_ref().unwrap().clone(),
start,
end,
LocalSymbolScopeKind::Callable,
);

self.do_arguments_symbol_resolve_with_hint(
&call_expr.args,
&call_expr.keywords,
true,
&func_ty,
)?;
self.leave_scope();
if let Some(owner) = func_symbol {
let cur_scope = self.ctx.scopes.last().unwrap();
self.gs
.get_scopes_mut()
.set_owner_to_scope(*cur_scope, owner);
}

self.do_arguments_symbol_resolve_with_hint(
&call_expr.args,
&call_expr.keywords,
true,
&func_ty,
)?;
self.leave_scope();
}
_ => {}
}
}

Expand Down
21 changes: 21 additions & 0 deletions kclvm/tools/src/LSP/src/goto_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,27 @@ mod tests {
11
);

goto_def_test_snapshot!(
goto_dict_to_schema_attr_test6,
"src/test_data/goto_def_test/dict_to_schema/dict_to_schema.k",
52,
7
);

goto_def_test_snapshot!(
goto_dict_to_schema_attr_test7,
"src/test_data/goto_def_test/dict_to_schema/dict_to_schema.k",
55,
7
);

goto_def_test_snapshot!(
goto_dict_to_schema_attr_test8,
"src/test_data/goto_def_test/dict_to_schema/dict_to_schema.k",
58,
7
);

goto_def_test_snapshot!(
list_if_expr_test,
"src/test_data/goto_def_test/list_if_expr_test/list_if_expr_test.k",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: tools/src/LSP/src/goto_def.rs
expression: "format!(\"{:?}\", { fmt_resp(& res) })"
---
"path: \"src/test_data/goto_def_test/dict_to_schema/dict_to_schema.k\", range: Range { start: Position { line: 39, character: 4 }, end: Position { line: 39, character: 8 } }"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: tools/src/LSP/src/goto_def.rs
expression: "format!(\"{:?}\", { fmt_resp(& res) })"
---
"path: \"src/test_data/goto_def_test/dict_to_schema/dict_to_schema.k\", range: Range { start: Position { line: 39, character: 4 }, end: Position { line: 39, character: 8 } }"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: tools/src/LSP/src/goto_def.rs
expression: "format!(\"{:?}\", { fmt_resp(& res) })"
---
"path: \"src/test_data/goto_def_test/dict_to_schema/dict_to_schema.k\", range: Range { start: Position { line: 39, character: 4 }, end: Position { line: 39, character: 8 } }"
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,25 @@ e: Name1 | Name2 | {str:Name1} | {str:Name2} | {str: Name3} = {
"c": "c"
}
}

schema Config:
name: str
age: int = 1

schema Name[c: Config]:
name: str = "Bob"
cc: any = c

ff = lambda c: Config {
c
}

n1 = Name({
name = "Alice"
}) {}
n2 = Name({
name = "Alice"
})
n3 = ff({
name = "Alice"
})

0 comments on commit 10f0506

Please sign in to comment.