Skip to content

Commit

Permalink
fix: fix ty error in assignment (#900)
Browse files Browse the repository at this point in the history
fix: fix ty infer error in assignment

Signed-off-by: zongz <zongzhe1024@163.com>
  • Loading branch information
zong-zhe authored Nov 21, 2023
1 parent 0f7bf2d commit 1d34307
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
8 changes: 0 additions & 8 deletions kclvm/sema/src/resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,6 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
value_ty = self.expr(&assign_stmt.value);
self.clear_config_expr_context(init_stack_depth as usize, false)
}
TypeKind::Dict(_) => {
value_ty = self.expr(&assign_stmt.value);
if !assign_stmt.type_annotation.is_none() {
// Check type annotation if exists.
self.check_assignment_type_annotation(assign_stmt, value_ty.clone());
}
self.set_type_to_scope(name, value_ty.clone(), &target.node.names[0]);
}
_ => {
value_ty = self.expr(&assign_stmt.value);
// Check type annotation if exists.
Expand Down
9 changes: 9 additions & 0 deletions kclvm/sema/src/resolver/test_data/ty_in_lambda.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
_b = True
a = lambda item: {str:}, c: {str:} -> {str:str} {
result = {"aaa": "bbb"}
if _b :
result = {}
result
}

result = {"ccc": "ddd"}
25 changes: 25 additions & 0 deletions kclvm/sema/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,3 +775,28 @@ fn test_schema_params_count() {
"expected 1 positional argument, found 0"
);
}

#[test]
fn test_set_ty_in_lambda() {
let sess = Arc::new(ParseSession::default());
let mut program = load_program(
sess.clone(),
&["./src/resolver/test_data/ty_in_lambda.k"],
None,
None,
)
.unwrap();
assert_eq!(
resolve_program(&mut program)
.main_scope()
.unwrap()
.borrow()
.lookup("result")
.unwrap()
.borrow()
.ty
.clone()
.ty_str(),
"{str:str}"
);
}
21 changes: 21 additions & 0 deletions kclvm/tools/src/LSP/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,27 @@ mod tests {
}
}

#[test]
#[bench_test]
fn assignment_ty_in_lambda_hover() {
let (file, program, prog_scope, _, gs) =
compile_test_file("src/test_data/hover_test/ty_in_lambda.k");
let pos = KCLPos {
filename: file.clone(),
line: 3,
column: Some(8),
};
let got = hover(&program, &pos, &prog_scope, &gs).unwrap();
match got.contents {
lsp_types::HoverContents::Scalar(marked_string) => {
if let MarkedString::String(s) = marked_string {
assert_eq!(s, "result: {str:str}");
}
}
_ => unreachable!("test error"),
}
}

#[test]
#[bench_test]
fn str_var_func_hover() {
Expand Down
9 changes: 9 additions & 0 deletions kclvm/tools/src/LSP/src/test_data/hover_test/ty_in_lambda.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
_b = True
a = lambda item: {str:}, c: {str:} -> {str:str} {
result = {"aaa": "bbb"}
if _b :
result = {}
result
}

result = {"ccc": "ddd"}

0 comments on commit 1d34307

Please sign in to comment.