Skip to content

Commit

Permalink
fix: normal func param default value
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Nov 7, 2023
1 parent b24a298 commit 094a8ed
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
5 changes: 2 additions & 3 deletions kclvm/sema/src/resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,13 +972,12 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
} else {
ty.clone()
};

let value = &args.node.defaults[i];
params.push(Parameter {
name,
ty: ty.clone(),
has_default: false,
has_default: value.is_some(),
});
let value = &args.node.defaults[i];
self.expr_or_any_type(value);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
is_alpha = lambda s: str, locale: str = 'en-US', options: {str:} = {} -> bool {
False
}
a = is_alpha("c")
21 changes: 21 additions & 0 deletions kclvm/sema/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,27 @@ fn test_resolve_assignment_in_lambda() {
assert_eq!(images_scope_obj.borrow().ty.ty_str(), "[str]");
}

#[test]
fn test_resolve_function_with_default_values() {
let sess = Arc::new(ParseSession::default());
let mut program = load_program(
sess.clone(),
&["./src/resolver/test_data/function_with_default_values.k"],
None,
)
.unwrap();
let scope = resolve_program(&mut program);
assert!(!scope.handler.has_errors());
let main_scope = scope.main_scope().unwrap();
let func = main_scope.borrow().lookup("is_alpha").unwrap();
assert!(func.borrow().ty.is_func());
let func_ty = func.borrow().ty.into_function_ty();
assert_eq!(func_ty.params.len(), 3);
assert_eq!(func_ty.params[0].has_default, false);
assert_eq!(func_ty.params[1].has_default, true);
assert_eq!(func_ty.params[2].has_default, true);
}

#[test]
fn test_assignment_type_annotation_check_in_lambda() {
let sess = Arc::new(ParseSession::default());
Expand Down

0 comments on commit 094a8ed

Please sign in to comment.