diff --git a/kclvm/sema/src/resolver/loop.rs b/kclvm/sema/src/resolver/loop.rs index d251314f2..51cca4e8d 100644 --- a/kclvm/sema/src/resolver/loop.rs +++ b/kclvm/sema/src/resolver/loop.rs @@ -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(), @@ -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(), @@ -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(), @@ -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(), diff --git a/kclvm/sema/src/resolver/node.rs b/kclvm/sema/src/resolver/node.rs index 7f9ba1801..05e88c365 100644 --- a/kclvm/sema/src/resolver/node.rs +++ b/kclvm/sema/src/resolver/node.rs @@ -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 } @@ -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( diff --git a/kclvm/sema/src/resolver/scope.rs b/kclvm/sema/src/resolver/scope.rs index 4f486d3f5..f9d4ec455 100644 --- a/kclvm/sema/src/resolver/scope.rs +++ b/kclvm/sema/src/resolver/scope.rs @@ -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(&mut self, name: &str, ty: TypeRef, node: &ast::Node) { + pub fn set_infer_type_to_scope(&mut self, name: &str, ty: TypeRef, node: &ast::Node) { let mut scope = self.scope.borrow_mut(); match scope.elems.get_mut(name) { Some(obj) => { @@ -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(&mut self, name: &str, ty: TypeRef, node: &ast::Node) { + 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) {