Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhance upgrade dict to schema. #1381

Merged
merged 4 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions kclvm/sema/src/resolver/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<'ctx> Resolver<'ctx> {
return;
}
};
self.must_assignable_to(ty.clone(), expected_ty, args[i].get_span_pos(), None, true)
self.must_assignable_to(ty.clone(), expected_ty, args[i].get_span_pos(), None)
}
// Do keyword argument type check
for (i, (arg_name, kwarg_ty)) in kwarg_types.iter().enumerate() {
Expand Down Expand Up @@ -144,7 +144,6 @@ impl<'ctx> Resolver<'ctx> {
expected_types[0].clone(),
kwargs[i].get_span_pos(),
None,
true,
);
};
}
Expand Down
70 changes: 56 additions & 14 deletions kclvm/sema/src/resolver/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,38 @@ impl<'ctx> Resolver<'ctx> {
},
}
}
TypeKind::Union(types) => {
let mut possible_types = vec![];
for ty in types {
match &ty.kind {
TypeKind::Schema(schema_ty) => {
match schema_ty.get_obj_of_attr(key_name) {
Some(attr_ty_obj) => {
possible_types.push(attr_ty_obj.ty.clone());
}
None => match &schema_ty.index_signature {
Some(index_signature) => {
possible_types
.push(index_signature.val_ty.clone());
}
None => continue,
},
}
}
TypeKind::Dict(DictType { val_ty, .. }) => {
possible_types.push(val_ty.clone());
}
_ => continue,
}
}

Some(self.new_config_expr_context_item(
key_name,
crate::ty::sup(&possible_types).into(),
obj.start.clone(),
obj.end.clone(),
))
}
_ => None,
},
None => None,
Expand Down Expand Up @@ -320,7 +352,6 @@ impl<'ctx> Resolver<'ctx> {
ty,
key.get_span_pos(),
Some(obj_last.get_span_pos()),
true,
);
}
self.clear_config_expr_context(stack_depth, false);
Expand Down Expand Up @@ -350,19 +381,23 @@ impl<'ctx> Resolver<'ctx> {
let mut schema_names = vec![];
let mut total_suggs = vec![];
for ty in types {
if let TypeKind::Schema(schema_ty) = &ty.kind {
if schema_ty.get_obj_of_attr(attr).is_none()
&& !schema_ty.is_mixin
&& schema_ty.index_signature.is_none()
{
let mut suggs =
suggestions::provide_suggestions(attr, schema_ty.attrs.keys());
total_suggs.append(&mut suggs);
schema_names.push(schema_ty.name.clone());
} else {
// If there is a schema attribute that meets the condition, the type check passes
return;
match &ty.kind {
TypeKind::Schema(schema_ty) => {
if schema_ty.get_obj_of_attr(attr).is_none()
&& !schema_ty.is_mixin
&& schema_ty.index_signature.is_none()
{
let mut suggs =
suggestions::provide_suggestions(attr, schema_ty.attrs.keys());
total_suggs.append(&mut suggs);
schema_names.push(schema_ty.name.clone());
} else {
// If there is a schema attribute that meets the condition, the type check passes
return;
}
}
TypeKind::Dict(..) => return,
_ => continue,
}
}
if !schema_names.is_empty() {
Expand Down Expand Up @@ -497,7 +532,11 @@ impl<'ctx> Resolver<'ctx> {
let key_ty = if identifier.names.len() == 1 {
let name = &identifier.names[0].node;
let key_ty = if self.ctx.local_vars.contains(name) {
self.expr(key)
// set key context expected schema as None
self.ctx.config_expr_context.push(None);
let key_ty = self.expr(key);
self.ctx.config_expr_context.pop();
key_ty
} else {
Arc::new(Type::str_lit(name))
};
Expand Down Expand Up @@ -543,7 +582,10 @@ impl<'ctx> Resolver<'ctx> {
val_ty
}
_ => {
// set key context expected schema as None
self.ctx.config_expr_context.push(None);
let key_ty = self.expr(key);
self.ctx.config_expr_context.pop();
let val_ty = self.expr(value);
self.check_attr_ty(&key_ty, key.get_span_pos());
if let ast::Expr::StringLit(string_lit) = &key.node {
Expand Down
14 changes: 1 addition & 13 deletions kclvm/sema/src/resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
expected_ty.clone(),
unification_stmt.target.get_span_pos(),
None,
true,
);
if !ty.is_any() && expected_ty.is_any() {
self.set_type_to_scope(&names[0].node, ty, &names[0]);
Expand Down Expand Up @@ -185,7 +184,6 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
expected_ty.clone(),
target.get_span_pos(),
None,
true,
);

let upgrade_schema_type = self.upgrade_dict_to_schema(
Expand Down Expand Up @@ -221,7 +219,6 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
expected_ty.clone(),
target.get_span_pos(),
None,
true,
);

let upgrade_schema_type = self.upgrade_dict_to_schema(
Expand Down Expand Up @@ -290,7 +287,6 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
expected_ty,
aug_assign_stmt.target.get_span_pos(),
None,
true,
);
self.ctx.l_value = false;
new_target_ty
Expand Down Expand Up @@ -457,7 +453,6 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
expected_ty,
schema_attr.name.get_span_pos(),
None,
true,
);
}
// Assign
Expand All @@ -466,7 +461,6 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
expected_ty,
schema_attr.name.get_span_pos(),
None,
true,
),
},
None => bug!("invalid ast schema attr op kind"),
Expand Down Expand Up @@ -1117,13 +1111,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
let real_ret_ty = self.stmts(&lambda_expr.body);
self.leave_scope();
self.ctx.in_lambda_expr.pop();
self.must_assignable_to(
real_ret_ty.clone(),
ret_ty.clone(),
(start, end),
None,
true,
);
self.must_assignable_to(real_ret_ty.clone(), ret_ty.clone(), (start, end), None);
if !real_ret_ty.is_any() && ret_ty.is_any() && lambda_expr.return_ty.is_none() {
ret_ty = real_ret_ty;
}
Expand Down
1 change: 0 additions & 1 deletion kclvm/sema/src/resolver/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ impl<'ctx> Resolver<'ctx> {
expected_ty,
index_signature_node.get_span_pos(),
None,
true,
);
}
}
Expand Down
Loading
Loading