Skip to content

Commit

Permalink
fix: fix type infer in assignstmt (#1411)
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa authored Jun 19, 2024
1 parent 71a192e commit edecd59
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
20 changes: 10 additions & 10 deletions kclvm/sema/src/resolver/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ impl<'ctx> Resolver<'ctx> {
if second_var_name.is_some() {
first_var_ty = sup(&[self.int_ty(), first_var_ty.clone()]);
second_var_ty = sup(&[item_ty.clone(), second_var_ty.clone()]);
self.set_type_to_scope(
self.set_infer_type_to_scope(
&first_var_name.unwrap().node,
first_var_ty.clone(),
&first_var_name.unwrap(),
);
self.set_type_to_scope(
self.set_infer_type_to_scope(
&second_var_name.unwrap().node,
second_var_ty.clone(),
&second_var_name.unwrap(),
);
} else {
first_var_ty = sup(&[item_ty.clone(), first_var_ty.clone()]);
self.set_type_to_scope(
self.set_infer_type_to_scope(
&first_var_name.unwrap().node,
first_var_ty.clone(),
&first_var_name.unwrap(),
Expand All @@ -51,14 +51,14 @@ impl<'ctx> Resolver<'ctx> {
}
TypeKind::Dict(DictType { key_ty, val_ty, .. }) => {
first_var_ty = sup(&[key_ty.clone(), first_var_ty.clone()]);
self.set_type_to_scope(
self.set_infer_type_to_scope(
&first_var_name.unwrap().node,
first_var_ty.clone(),
&first_var_name.unwrap(),
);
if second_var_name.is_some() {
second_var_ty = sup(&[val_ty.clone(), second_var_ty.clone()]);
self.set_type_to_scope(
self.set_infer_type_to_scope(
&second_var_name.unwrap().node,
second_var_ty.clone(),
&second_var_name.unwrap(),
Expand All @@ -68,14 +68,14 @@ impl<'ctx> Resolver<'ctx> {
TypeKind::Schema(schema_ty) => {
let (key_ty, val_ty) = (schema_ty.key_ty(), schema_ty.val_ty());
first_var_ty = sup(&[key_ty, first_var_ty.clone()]);
self.set_type_to_scope(
self.set_infer_type_to_scope(
&first_var_name.unwrap().node,
first_var_ty.clone(),
&first_var_name.unwrap(),
);
if second_var_name.is_some() {
second_var_ty = sup(&[val_ty, second_var_ty.clone()]);
self.set_type_to_scope(
self.set_infer_type_to_scope(
&second_var_name.unwrap().node,
second_var_ty.clone(),
&second_var_name.unwrap(),
Expand All @@ -86,19 +86,19 @@ impl<'ctx> Resolver<'ctx> {
if second_var_name.is_some() {
first_var_ty = sup(&[self.int_ty(), first_var_ty.clone()]);
second_var_ty = sup(&[self.str_ty(), second_var_ty.clone()]);
self.set_type_to_scope(
self.set_infer_type_to_scope(
&first_var_name.unwrap().node,
first_var_ty.clone(),
&first_var_name.unwrap(),
);
self.set_type_to_scope(
self.set_infer_type_to_scope(
&second_var_name.unwrap().node,
second_var_ty.clone(),
&second_var_name.unwrap(),
);
} else {
first_var_ty = sup(&[self.str_ty(), first_var_ty.clone()]);
self.set_type_to_scope(
self.set_infer_type_to_scope(
&first_var_name.unwrap().node,
first_var_ty.clone(),
&first_var_name.unwrap(),
Expand Down
4 changes: 2 additions & 2 deletions kclvm/sema/src/resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
None,
);
if !ty.is_any() && expected_ty.is_any() {
self.set_type_to_scope(&names[0].node, ty, &names[0]);
self.set_infer_type_to_scope(&names[0].node, ty, &names[0]);
}
expected_ty
}
Expand Down Expand Up @@ -197,7 +197,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Resolver<'ctx> {
);

if !value_ty.is_any() && expected_ty.is_any() && assign_stmt.ty.is_none() {
self.set_type_to_scope(name, value_ty.clone(), &target.node.names[0]);
self.set_infer_type_to_scope(name, value_ty.clone(), &target.node.names[0]);
if let Some(schema_ty) = &self.ctx.schema {
let mut schema_ty = schema_ty.borrow_mut();
schema_ty.set_type_of_attr(
Expand Down
22 changes: 21 additions & 1 deletion kclvm/sema/src/resolver/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ impl<'ctx> Resolver<'ctx> {
}

/// Set type to the scope exited object, if not found, emit a compile error.
pub fn set_type_to_scope<T>(&mut self, name: &str, ty: TypeRef, node: &ast::Node<T>) {
pub fn set_infer_type_to_scope<T>(&mut self, name: &str, ty: TypeRef, node: &ast::Node<T>) {
let mut scope = self.scope.borrow_mut();
match scope.elems.get_mut(name) {
Some(obj) => {
Expand All @@ -473,6 +473,26 @@ impl<'ctx> Resolver<'ctx> {
}
}

/// Set type to the scope exited object, if not found, emit a compile error.
pub fn set_type_to_scope<T>(&mut self, name: &str, ty: TypeRef, node: &ast::Node<T>) {
let mut scope = self.scope.borrow_mut();
match scope.elems.get_mut(name) {
Some(obj) => {
let mut obj = obj.borrow_mut();
self.node_ty_map
.borrow_mut()
.insert(self.get_node_key(node.id.clone()), ty.clone());
obj.ty = ty;
}
None => {
self.handler.add_compile_error(
&format!("name '{}' is not defined", name.replace('@', "")),
node.get_span_pos(),
);
}
}
}

/// Insert object into the current scope.
#[inline]
pub fn insert_object(&mut self, name: &str, obj: ScopeObject) {
Expand Down

0 comments on commit edecd59

Please sign in to comment.