diff --git a/kclvm/loader/src/lib.rs b/kclvm/loader/src/lib.rs index 079e31380..a79a6dbcf 100644 --- a/kclvm/loader/src/lib.rs +++ b/kclvm/loader/src/lib.rs @@ -150,7 +150,7 @@ pub fn load_packages_with_cache( ); let node_ty_map = prog_scope.node_ty_map; let gs = Namer::find_symbols(&program, gs); - let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map.clone()); + let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map.clone())?; (program, prog_scope.handler.diagnostics.clone(), gs) } else { (parse_result.program, IndexSet::default(), gs) diff --git a/kclvm/sema/src/advanced_resolver/mod.rs b/kclvm/sema/src/advanced_resolver/mod.rs index 0f8dbc354..c09dc0aee 100644 --- a/kclvm/sema/src/advanced_resolver/mod.rs +++ b/kclvm/sema/src/advanced_resolver/mod.rs @@ -95,7 +95,7 @@ impl<'ctx> AdvancedResolver<'ctx> { program: &'ctx Program, gs: GlobalState, node_ty_map: NodeTyMap, - ) -> GlobalState { + ) -> anyhow::Result { let mut advanced_resolver = Self { gs, ctx: Context { @@ -128,14 +128,14 @@ impl<'ctx> AdvancedResolver<'ctx> { ); for module in modules.iter() { advanced_resolver.ctx.current_filename = Some(module.filename.clone()); - advanced_resolver.walk_module(module); + advanced_resolver.walk_module(module)?; } advanced_resolver.leave_scope() } } advanced_resolver.gs.build_sema_db(); - advanced_resolver.gs + Ok(advanced_resolver.gs) } fn enter_root_scope( @@ -303,7 +303,7 @@ mod tests { None, ) .node_ty_map; - let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map); + let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map).unwrap(); let base_path = Path::new(".").canonicalize().unwrap(); // print_symbols_info(&gs); let except_symbols = vec![ @@ -1232,7 +1232,7 @@ mod tests { let gs = GlobalState::default(); let gs = Namer::find_symbols(&program, gs); let node_ty_map = resolver::resolve_program(&mut program).node_ty_map; - let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map); + let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map).unwrap(); let base_path = Path::new(".").canonicalize().unwrap(); let test_cases = vec![ @@ -1309,7 +1309,7 @@ mod tests { let gs = GlobalState::default(); let gs = Namer::find_symbols(&program, gs); let node_ty_map = resolver::resolve_program(&mut program).node_ty_map; - let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map); + let gs = AdvancedResolver::resolve_program(&program, gs, node_ty_map).unwrap(); let base_path = Path::new(".").canonicalize().unwrap(); let scope_test_cases = vec![ diff --git a/kclvm/sema/src/advanced_resolver/node.rs b/kclvm/sema/src/advanced_resolver/node.rs index 99cba7f57..e781ea4a4 100644 --- a/kclvm/sema/src/advanced_resolver/node.rs +++ b/kclvm/sema/src/advanced_resolver/node.rs @@ -1,3 +1,4 @@ +use anyhow::anyhow; use std::sync::Arc; use indexmap::IndexMap; @@ -19,30 +20,30 @@ use crate::{ use super::AdvancedResolver; -type ResolvedResult = Option; +type ResolvedResult = anyhow::Result>; impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { - type Result = Option; + type Result = anyhow::Result>; fn walk_module(&mut self, module: &'ctx ast::Module) -> Self::Result { for stmt in module.body.iter() { - self.stmt(&stmt); + self.stmt(&stmt)?; } for comment in module.comments.iter() { let (start, end) = comment.get_span_pos(); self.ctx.start_pos = start; self.ctx.end_pos = end; self.ctx.cur_node = comment.id.clone(); - self.walk_comment(&comment.node); + self.walk_comment(&comment.node)?; } - None + Ok(None) } fn walk_expr_stmt(&mut self, expr_stmt: &'ctx ast::ExprStmt) -> Self::Result { for expr in expr_stmt.exprs.iter() { - self.expr(&expr); + self.expr(&expr)?; } - None + Ok(None) } fn walk_unification_stmt( @@ -50,18 +51,22 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { unification_stmt: &'ctx ast::UnificationStmt, ) -> Self::Result { self.ctx.maybe_def = true; - self.walk_identifier_expr(&unification_stmt.target); + self.walk_identifier_expr(&unification_stmt.target)?; self.ctx.maybe_def = false; - self.walk_schema_expr(&unification_stmt.value.node); - None + self.walk_schema_expr(&unification_stmt.value.node)?; + Ok(None) } fn walk_type_alias_stmt(&mut self, type_alias_stmt: &'ctx ast::TypeAliasStmt) -> Self::Result { - let alias_symbol = self.gs.get_symbols().get_symbol_by_fully_qualified_name( - &(self.ctx.current_pkgpath.as_ref().unwrap().clone() - + "." - + &type_alias_stmt.type_name.node.get_name()), - )?; + let alias_symbol = self + .gs + .get_symbols() + .get_symbol_by_fully_qualified_name( + &(self.ctx.current_pkgpath.as_ref().unwrap().clone() + + "." + + &type_alias_stmt.type_name.node.get_name()), + ) + .ok_or(anyhow!("alias_symbol not found"))?; if let Some(symbol) = self .gs .get_symbols_mut() @@ -77,8 +82,8 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { doc: None, }; } - self.walk_type_expr(Some(&type_alias_stmt.ty)); - None + self.walk_type_expr(Some(&type_alias_stmt.ty))?; + Ok(None) } fn walk_assign_stmt(&mut self, assign_stmt: &'ctx ast::AssignStmt) -> Self::Result { @@ -87,42 +92,42 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { continue; } self.ctx.maybe_def = true; - self.walk_identifier_expr(target); + self.walk_identifier_expr(target)?; self.ctx.maybe_def = false; } - self.walk_type_expr(assign_stmt.ty.as_ref().map(|ty| ty.as_ref())); - self.expr(&assign_stmt.value); - None + self.walk_type_expr(assign_stmt.ty.as_ref().map(|ty| ty.as_ref()))?; + self.expr(&assign_stmt.value)?; + Ok(None) } fn walk_aug_assign_stmt(&mut self, aug_assign_stmt: &'ctx ast::AugAssignStmt) -> Self::Result { self.ctx.maybe_def = true; - self.walk_identifier_expr(&aug_assign_stmt.target); + self.walk_identifier_expr(&aug_assign_stmt.target)?; self.ctx.maybe_def = false; - self.expr(&aug_assign_stmt.value); - None + self.expr(&aug_assign_stmt.value)?; + Ok(None) } fn walk_assert_stmt(&mut self, assert_stmt: &'ctx ast::AssertStmt) -> Self::Result { - self.expr(&assert_stmt.test); + self.expr(&assert_stmt.test)?; if let Some(if_cond) = &assert_stmt.if_cond { - self.expr(if_cond); + self.expr(if_cond)?; } if let Some(msg) = &assert_stmt.msg { - self.expr(msg); + self.expr(msg)?; } - None + Ok(None) } fn walk_if_stmt(&mut self, if_stmt: &'ctx ast::IfStmt) -> Self::Result { - self.expr(&if_stmt.cond); + self.expr(&if_stmt.cond)?; for stmt in if_stmt.body.iter() { - self.stmt(stmt); + self.stmt(stmt)?; } for stmt in if_stmt.orelse.iter() { - self.stmt(stmt); + self.stmt(stmt)?; } - None + Ok(None) } fn walk_import_stmt(&mut self, import_stmt: &'ctx ast::ImportStmt) -> Self::Result { @@ -135,10 +140,14 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { let mut unresolved = UnresolvedSymbol::new(import_stmt.path.node.clone(), start_pos, end_pos, None); - let package_symbol = self + let package_symbol = match self .gs .get_symbols() - .get_symbol_by_fully_qualified_name(&import_stmt.path.node)?; + .get_symbol_by_fully_qualified_name(&import_stmt.path.node) + { + Some(symbol) => symbol, + None => return Ok(None), + }; unresolved.def = Some(package_symbol); let unresolved_ref = self .gs @@ -153,7 +162,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { self.gs .get_scopes_mut() .add_ref_to_scope(cur_scope, unresolved_ref); - Some(unresolved_ref) + Ok(Some(unresolved_ref)) } fn walk_schema_stmt(&mut self, schema_stmt: &'ctx ast::SchemaStmt) -> Self::Result { @@ -162,14 +171,12 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { .ctx .node_ty_map .get(&self.ctx.get_node_key(&schema_stmt.name.id)) - .unwrap() - .clone(); + .ok_or(anyhow!("schema_ty not found"))?; let schema_symbol = self .gs .get_symbols() .get_type_symbol(&schema_ty, self.get_current_module_info()) - .unwrap(); - + .ok_or(anyhow!("schema_symbol not found"))?; if self .gs .get_symbols() @@ -209,7 +216,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { .get_symbols_mut() .schemas .get_mut(schema_symbol.get_id()) - .unwrap() + .ok_or(anyhow!("schema_symbol not found"))? .sema_info = KCLSymbolSemanticInfo { ty: Some(schema_ty.clone()), doc: schema_stmt.doc.as_ref().map(|doc| doc.node.clone()), @@ -235,20 +242,20 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { .get_symbols_mut() .schemas .get_mut(schema_symbol.get_id()) - .unwrap() - .parent_schema = self.walk_identifier_expr(parent); + .ok_or(anyhow!("schema_symbol not found"))? + .parent_schema = self.walk_identifier_expr(parent)?; } if let Some(for_host) = &schema_stmt.for_host_name { self.gs .get_symbols_mut() .schemas .get_mut(schema_symbol.get_id()) - .unwrap() - .for_host = self.walk_identifier_expr(for_host); + .ok_or(anyhow!("schema_symbol not found"))? + .for_host = self.walk_identifier_expr(for_host)?; } let mut mixins = vec![]; for mixin in schema_stmt.mixins.iter() { - if let Some(mixin) = self.walk_identifier_expr(mixin) { + if let Some(mixin) = self.walk_identifier_expr(mixin)? { mixins.push(mixin); } last_end_pos = mixin.get_end_pos(); @@ -257,11 +264,11 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { .get_symbols_mut() .schemas .get_mut(schema_symbol.get_id()) - .unwrap() + .ok_or(anyhow!("schema_symbol not found"))? .mixins = mixins; if let Some(args) = &schema_stmt.args { - self.walk_arguments(&args.node); + self.walk_arguments(&args.node)?; last_end_pos = args.get_end_pos(); } if let Some(index_signature) = &schema_stmt.index_signature { @@ -286,26 +293,26 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { .get_scopes_mut() .add_def_to_scope(cur_scope, key_name.clone(), value); - self.walk_type_expr(Some(&index_signature.node.value_ty)); + self.walk_type_expr(Some(&index_signature.node.value_ty))?; if let Some(value) = &index_signature.node.value { - self.expr(value); + self.expr(value)?; }; } last_end_pos = index_signature.get_end_pos(); } for stmt in schema_stmt.body.iter() { - if let Some(attribute_symbol) = self.stmt(&stmt) { + if let Some(attribute_symbol) = self.stmt(&stmt)? { let name = self .gs .get_symbols() .get_symbol(attribute_symbol) - .unwrap() + .ok_or(anyhow!("attribute_symbol not found"))? .get_name(); self.gs .get_symbols_mut() .schemas .get_mut(schema_symbol.get_id()) - .unwrap() + .ok_or(anyhow!("schema_symbol not found"))? .attributes .insert(name, attribute_symbol); } @@ -323,7 +330,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { } for check_expr in schema_stmt.checks.iter() { - self.walk_check_expr(&check_expr.node); + self.walk_check_expr(&check_expr.node)?; } if has_check { @@ -332,19 +339,21 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { self.leave_scope(); - Some(schema_symbol) + Ok(Some(schema_symbol)) } fn walk_rule_stmt(&mut self, rule_stmt: &'ctx ast::RuleStmt) -> Self::Result { let rule_ty = self .ctx .node_ty_map - .get(&self.ctx.get_node_key(&rule_stmt.name.id))? + .get(&self.ctx.get_node_key(&rule_stmt.name.id)) + .ok_or(anyhow!("schema_symbol not found"))? .clone(); let rule_symbol = self .gs .get_symbols() - .get_type_symbol(&rule_ty, self.get_current_module_info())?; + .get_type_symbol(&rule_ty, self.get_current_module_info()) + .ok_or(anyhow!("rule_symbol not found"))?; if let Some(symbol) = self .gs .get_symbols_mut() @@ -365,25 +374,30 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { self.gs .get_symbols_mut() .rules - .get_mut(rule_symbol.get_id())? - .for_host = self.walk_identifier_expr(for_host); + .get_mut(rule_symbol.get_id()) + .ok_or(anyhow!("rule_symbol not found"))? + .for_host = self.walk_identifier_expr(for_host)?; } let mut parent_rules = vec![]; for parent_rule in rule_stmt.parent_rules.iter() { - parent_rules.push(self.walk_identifier_expr(parent_rule)?); + parent_rules.push( + self.walk_identifier_expr(parent_rule)? + .ok_or(anyhow!("parent_rule not found"))?, + ); } self.gs .get_symbols_mut() .rules - .get_mut(rule_symbol.get_id())? + .get_mut(rule_symbol.get_id()) + .ok_or(anyhow!("rule_symbol not found"))? .parent_rules = parent_rules; self.resolve_decorator(&rule_stmt.decorators); - Some(rule_symbol) + Ok(Some(rule_symbol)) } fn walk_quant_expr(&mut self, quant_expr: &'ctx ast::QuantExpr) -> Self::Result { let (start, end) = (self.ctx.start_pos.clone(), self.ctx.end_pos.clone()); - self.expr(&quant_expr.target); + self.expr(&quant_expr.target)?; self.enter_local_scope( &self.ctx.current_filename.as_ref().unwrap().clone(), start, @@ -422,25 +436,33 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { } if let Some(if_cond) = &quant_expr.if_cond { - self.expr(&if_cond); + self.expr(&if_cond)?; } - self.expr(&quant_expr.test); + self.expr(&quant_expr.test)?; self.leave_scope(); - None + Ok(None) } fn walk_schema_attr(&mut self, schema_attr: &'ctx ast::SchemaAttr) -> Self::Result { - let attr_symbol = *self + let attr_symbol = match self .gs .get_symbols() .symbols_info .node_symbol_map - .get(&self.ctx.get_node_key(&schema_attr.name.id))?; + .get(&self.ctx.get_node_key(&schema_attr.name.id)) + { + Some(symbol) => *symbol, + None => return Err(anyhow!("attr_symbol not found")), + }; let parent_scope = *self.ctx.scopes.last().unwrap(); let parent_scope = self.gs.get_scopes().get_scope(&parent_scope).unwrap(); let mut doc = None; if let Some(schema_symbol) = parent_scope.get_owner() { - let schema_symbol = self.gs.get_symbols().get_symbol(schema_symbol).unwrap(); + let schema_symbol = self + .gs + .get_symbols() + .get_symbol(schema_symbol) + .ok_or(anyhow!("schema_symbol not found"))?; if let Some(schema_ty) = schema_symbol.get_sema_info().ty.clone() { let schema_ty = schema_ty.into_schema_type(); if let Some(attr) = schema_ty.attrs.get(&schema_attr.name.node) { @@ -465,47 +487,51 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { }; }; - self.walk_type_expr(Some(&schema_attr.ty)); + self.walk_type_expr(Some(&schema_attr.ty))?; if let Some(value) = &schema_attr.value { - self.expr(value); + self.expr(value)?; } self.resolve_decorator(&schema_attr.decorators); - Some(attr_symbol) + Ok(Some(attr_symbol)) } /// if else -> sup([body, orelse]) fn walk_if_expr(&mut self, if_expr: &'ctx ast::IfExpr) -> Self::Result { - self.expr(&if_expr.cond); - self.expr(&if_expr.body); - self.expr(&if_expr.orelse); - None + self.expr(&if_expr.cond)?; + self.expr(&if_expr.body)?; + self.expr(&if_expr.orelse)?; + Ok(None) } fn walk_unary_expr(&mut self, unary_expr: &'ctx ast::UnaryExpr) -> Self::Result { - self.expr(&unary_expr.operand); - None + self.expr(&unary_expr.operand)?; + Ok(None) } fn walk_binary_expr(&mut self, binary_expr: &'ctx ast::BinaryExpr) -> Self::Result { - self.expr(&binary_expr.left); - self.expr(&binary_expr.right); - None + self.expr(&binary_expr.left)?; + self.expr(&binary_expr.right)?; + Ok(None) } fn walk_selector_expr(&mut self, selector_expr: &'ctx ast::SelectorExpr) -> Self::Result { - self.expr(&selector_expr.value); + self.expr(&selector_expr.value)?; let mut parent_ty = self .ctx .node_ty_map - .get(&self.ctx.get_node_key(&selector_expr.value.id))? + .get(&self.ctx.get_node_key(&selector_expr.value.id)) + .ok_or(anyhow!("parent_ty not found"))? .clone(); for name in &selector_expr.attr.node.names { - let def_symbol_ref = self.gs.get_symbols().get_type_attribute( + let def_symbol_ref = match self.gs.get_symbols().get_type_attribute( &parent_ty, &name.node, self.get_current_module_info(), - )?; + ) { + Some(symbol) => symbol, + None => return Ok(None), + }; let (start_pos, end_pos): Range = name.get_span_pos(); let ast_id = name.id.clone(); @@ -520,47 +546,46 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { .get_scopes_mut() .add_ref_to_scope(cur_scope, unresolved_ref); - parent_ty = self - .ctx - .node_ty_map - .get(&self.ctx.get_node_key(&name.id))? - .clone(); + parent_ty = match self.ctx.node_ty_map.get(&self.ctx.get_node_key(&name.id)) { + Some(ty) => ty.clone(), + None => return Ok(None), + }; } - None + Ok(None) } fn walk_call_expr(&mut self, call_expr: &'ctx ast::CallExpr) -> Self::Result { - self.expr(&call_expr.func); - self.do_arguments_symbol_resolve(&call_expr.args, &call_expr.keywords); - None + self.expr(&call_expr.func)?; + self.do_arguments_symbol_resolve(&call_expr.args, &call_expr.keywords)?; + Ok(None) } fn walk_subscript(&mut self, subscript: &'ctx ast::Subscript) -> Self::Result { - self.expr(&subscript.value); + self.expr(&subscript.value)?; if let Some(index) = &subscript.index { - self.expr(index); + self.expr(index)?; } else { for expr in [&subscript.lower, &subscript.upper, &subscript.step] .iter() .copied() .flatten() { - self.expr(expr); + self.expr(expr)?; } } - None + Ok(None) } fn walk_paren_expr(&mut self, paren_expr: &'ctx ast::ParenExpr) -> Self::Result { - self.expr(&paren_expr.expr); - None + self.expr(&paren_expr.expr)?; + Ok(None) } fn walk_list_expr(&mut self, list_expr: &'ctx ast::ListExpr) -> Self::Result { for expr in list_expr.elts.iter() { - self.expr(expr); + self.expr(expr)?; } - None + Ok(None) } fn walk_list_comp(&mut self, list_comp: &'ctx ast::ListComp) -> Self::Result { @@ -576,11 +601,11 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { LocalSymbolScopeKind::List, ); for comp_clause in &list_comp.generators { - self.walk_comp_clause(&comp_clause.node); + self.walk_comp_clause(&comp_clause.node)?; } - self.expr(&list_comp.elt); + self.expr(&list_comp.elt)?; self.leave_scope(); - None + Ok(None) } fn walk_dict_comp(&mut self, dict_comp: &'ctx ast::DictComp) -> Self::Result { @@ -600,14 +625,14 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { LocalSymbolScopeKind::Dict, ); for comp_clause in &dict_comp.generators { - self.walk_comp_clause(&comp_clause.node); + self.walk_comp_clause(&comp_clause.node)?; } if let Some(key) = key { - self.expr(key); + self.expr(key)?; } - self.expr(&dict_comp.entry.value); + self.expr(&dict_comp.entry.value)?; self.leave_scope(); - None + Ok(None) } fn walk_list_if_item_expr( @@ -616,42 +641,42 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { ) -> Self::Result { self.expr(&list_if_item_expr.if_cond)?; if let Some(orelse) = &list_if_item_expr.orelse { - self.expr(orelse); + self.expr(orelse)?; } for expr in list_if_item_expr.exprs.iter() { - self.expr(expr); + self.expr(expr)?; } - None + Ok(None) } fn walk_starred_expr(&mut self, starred_expr: &'ctx ast::StarredExpr) -> Self::Result { - self.expr(&starred_expr.value); - None + self.expr(&starred_expr.value)?; + Ok(None) } fn walk_config_if_entry_expr( &mut self, config_if_entry_expr: &'ctx ast::ConfigIfEntryExpr, ) -> Self::Result { - self.expr(&config_if_entry_expr.if_cond); - self.walk_config_entries(&config_if_entry_expr.items); + self.expr(&config_if_entry_expr.if_cond)?; + self.walk_config_entries(&config_if_entry_expr.items)?; if let Some(expr) = config_if_entry_expr.orelse.as_ref() { - self.expr(expr); + self.expr(expr)?; } - None + Ok(None) } fn walk_comp_clause(&mut self, comp_clause: &'ctx ast::CompClause) -> Self::Result { - self.expr(&comp_clause.iter); + self.expr(&comp_clause.iter)?; for target in comp_clause.targets.iter() { self.ctx.maybe_def = true; - self.walk_identifier_expr(target); + self.walk_identifier_expr(target)?; self.ctx.maybe_def = false; } for if_expr in comp_clause.ifs.iter() { - self.expr(if_expr); + self.expr(if_expr)?; } - None + Ok(None) } fn walk_schema_expr(&mut self, schema_expr: &'ctx ast::SchemaExpr) -> Self::Result { @@ -659,32 +684,34 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { let schema_ty = self .ctx .node_ty_map - .get(&self.ctx.get_node_key(&schema_expr.name.id))? + .get(&self.ctx.get_node_key(&schema_expr.name.id)) + .ok_or(anyhow!("schema_ty not found"))? .clone(); let schema_symbol = self .gs .get_symbols() - .get_type_symbol(&schema_ty, self.get_current_module_info())?; + .get_type_symbol(&schema_ty, self.get_current_module_info()) + .ok_or(anyhow!("schema_symbol not found"))?; self.ctx.current_schema_symbol = Some(schema_symbol); - self.expr(&schema_expr.config); - self.do_arguments_symbol_resolve(&schema_expr.args, &schema_expr.kwargs); - None + self.expr(&schema_expr.config)?; + self.do_arguments_symbol_resolve(&schema_expr.args, &schema_expr.kwargs)?; + Ok(None) } fn walk_config_expr(&mut self, config_expr: &'ctx ast::ConfigExpr) -> Self::Result { - self.walk_config_entries(&config_expr.items); - None + self.walk_config_entries(&config_expr.items)?; + Ok(None) } fn walk_check_expr(&mut self, check_expr: &'ctx ast::CheckExpr) -> Self::Result { if let Some(msg) = &check_expr.msg { - self.expr(msg); + self.expr(msg)?; } if let Some(if_cond) = &check_expr.if_cond { - self.expr(if_cond); + self.expr(if_cond)?; } - self.expr(&check_expr.test); - None + self.expr(&check_expr.test)?; + Ok(None) } fn walk_lambda_expr(&mut self, lambda_expr: &'ctx ast::LambdaExpr) -> Self::Result { @@ -696,94 +723,95 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { LocalSymbolScopeKind::Lambda, ); if let Some(args) = &lambda_expr.args { - self.walk_arguments(&args.node); + self.walk_arguments(&args.node)?; } if let Some(ret_annotation_ty) = &lambda_expr.return_ty { - self.walk_type_expr(Some(&ret_annotation_ty)); + self.walk_type_expr(Some(&ret_annotation_ty))?; } for stmt in lambda_expr.body.iter() { - self.stmt(&stmt); + self.stmt(&stmt)?; } self.leave_scope(); - None + Ok(None) } fn walk_keyword(&mut self, keyword: &'ctx ast::Keyword) -> Self::Result { self.ctx.maybe_def = true; - self.walk_identifier_expr(&keyword.arg); + self.walk_identifier_expr(&keyword.arg)?; self.ctx.maybe_def = false; if let Some(value) = &keyword.value { - self.expr(&value); + self.expr(&value)?; } - None + Ok(None) } fn walk_arguments(&mut self, arguments: &'ctx ast::Arguments) -> Self::Result { for (i, arg) in arguments.args.iter().enumerate() { let ty = arguments.get_arg_type_node(i); - self.walk_type_expr(ty); + self.walk_type_expr(ty)?; self.ctx.maybe_def = true; - self.walk_identifier_expr(arg); + self.walk_identifier_expr(arg)?; self.ctx.maybe_def = false; if let Some(val) = &arguments.defaults[i] { - self.expr(val); + self.expr(val)?; } } - None + Ok(None) } fn walk_compare(&mut self, compare: &'ctx ast::Compare) -> Self::Result { - self.expr(&compare.left); + self.expr(&compare.left)?; for comparator in compare.comparators.iter() { - self.expr(&comparator); + self.expr(&comparator)?; } - None + Ok(None) } fn walk_identifier(&mut self, identifier: &'ctx ast::Identifier) -> Self::Result { let symbol_ref = self.resolve_names(&identifier.names, self.ctx.maybe_def)?; - Some(symbol_ref) + Ok(symbol_ref) } fn walk_number_lit(&mut self, _number_lit: &'ctx ast::NumberLit) -> Self::Result { - None + Ok(None) } fn walk_string_lit(&mut self, _string_lit: &'ctx ast::StringLit) -> Self::Result { - None + Ok(None) } fn walk_name_constant_lit( &mut self, _name_constant_lit: &'ctx ast::NameConstantLit, ) -> Self::Result { - None + Ok(None) } fn walk_joined_string(&mut self, joined_string: &'ctx ast::JoinedString) -> Self::Result { self.ctx.maybe_def = false; for expr in joined_string.values.iter() { - self.expr(expr); + self.expr(expr)?; } - None + Ok(None) } fn walk_formatted_value(&mut self, formatted_value: &'ctx ast::FormattedValue) -> Self::Result { - self.expr(&formatted_value.value); - None + self.expr(&formatted_value.value)?; + Ok(None) } fn walk_comment(&mut self, comment: &'ctx ast::Comment) -> Self::Result { let (start, end) = (self.ctx.start_pos.clone(), self.ctx.end_pos.clone()); let comment_symbol = CommentSymbol::new(start, end, comment.text.clone()); - self.gs + Ok(self + .gs .get_symbols_mut() - .alloc_comment_symbol(comment_symbol, self.ctx.get_node_key(&self.ctx.cur_node)) + .alloc_comment_symbol(comment_symbol, self.ctx.get_node_key(&self.ctx.cur_node))) } fn walk_missing_expr(&mut self, _missing_expr: &'ctx ast::MissingExpr) -> Self::Result { - None + Ok(None) } } @@ -804,10 +832,10 @@ impl<'ctx> AdvancedResolver<'ctx> { } self.ctx.cur_node = expr.id.clone(); match self.walk_expr(&expr.node) { - None => match self.ctx.node_ty_map.get(&self.ctx.get_node_key(&expr.id)) { + Ok(None) => match self.ctx.node_ty_map.get(&self.ctx.get_node_key(&expr.id)) { Some(ty) => { if let ast::Expr::Missing(_) = expr.node { - return None; + return Ok(None); } let (_, end) = expr.get_span_pos(); let mut expr_symbol = ExpressionSymbol::new( @@ -817,11 +845,12 @@ impl<'ctx> AdvancedResolver<'ctx> { None, ); expr_symbol.sema_info.ty = Some(ty.clone()); - self.gs + Ok(self + .gs .get_symbols_mut() - .alloc_expression_symbol(expr_symbol, self.ctx.get_node_key(&expr.id)) + .alloc_expression_symbol(expr_symbol, self.ctx.get_node_key(&expr.id))) } - None => None, + None => Ok(None), }, some => some, } @@ -838,7 +867,7 @@ impl<'ctx> AdvancedResolver<'ctx> { } fn resolve_names(&mut self, names: &[ast::Node], maybe_def: bool) -> ResolvedResult { - let first_name = names.get(0)?; + let first_name = names.get(0).unwrap(); let cur_scope = *self.ctx.scopes.last().unwrap(); let mut first_symbol = self.gs.look_up_symbol( @@ -864,7 +893,7 @@ impl<'ctx> AdvancedResolver<'ctx> { .gs .get_symbols() .get_symbol(first_symbol) - .unwrap() + .ok_or(anyhow!("first name symbol not found"))? .get_sema_info() .ty .is_none() @@ -884,8 +913,12 @@ impl<'ctx> AdvancedResolver<'ctx> { match first_symbol { Some(symbol_ref) => { let (start_pos, end_pos): Range = first_name.get_span_pos(); - let (def_start_pos, def_end_pos) = - self.gs.get_symbols().get_symbol(symbol_ref)?.get_range(); + let (def_start_pos, def_end_pos) = self + .gs + .get_symbols() + .get_symbol(symbol_ref) + .ok_or(anyhow!("first name symbol not found"))? + .get_range(); // get an unresolved symbol if def_start_pos != start_pos || def_end_pos != end_pos { @@ -906,15 +939,20 @@ impl<'ctx> AdvancedResolver<'ctx> { let mut parent_ty = self .ctx .node_ty_map - .get(&self.ctx.get_node_key(&first_name.id))?; + .get(&self.ctx.get_node_key(&first_name.id)) + .ok_or(anyhow!("parent_ty not found"))? + .clone(); for index in 1..names.len() { let name = names.get(index).unwrap(); - let def_symbol_ref = self.gs.get_symbols().get_type_attribute( + let def_symbol_ref = match self.gs.get_symbols().get_type_attribute( &parent_ty, &name.node, self.get_current_module_info(), - )?; + ) { + Some(symbol) => symbol, + None => return Ok(None), + }; let (start_pos, end_pos): Range = name.get_span_pos(); let ast_id = name.id.clone(); @@ -931,13 +969,18 @@ impl<'ctx> AdvancedResolver<'ctx> { .get_scopes_mut() .add_ref_to_scope(cur_scope, unresolved_ref); - parent_ty = self.ctx.node_ty_map.get(&self.ctx.get_node_key(&name.id))?; + parent_ty = self + .ctx + .node_ty_map + .get(&self.ctx.get_node_key(&name.id)) + .ok_or(anyhow!("parent_ty not found"))? + .clone(); if index == names.len() - 1 { - return Some(unresolved_ref); + return Ok(Some(unresolved_ref)); } } } - Some(symbol_ref) + Ok(Some(symbol_ref)) } None => { if maybe_def { @@ -970,7 +1013,7 @@ impl<'ctx> AdvancedResolver<'ctx> { } for index in 1..names.len() { - let name = names.get(index)?; + let name = names.get(index).unwrap(); let (start_pos, end_pos): Range = name.get_span_pos(); let ast_id = name.id.clone(); let value = self.gs.get_symbols_mut().alloc_value_symbol( @@ -997,11 +1040,11 @@ impl<'ctx> AdvancedResolver<'ctx> { }; } if index == names.len() - 1 { - return Some(value); + return Ok(Some(value)); } } } - None + Ok(None) } } } @@ -1055,10 +1098,13 @@ impl<'ctx> AdvancedResolver<'ctx> { } identifier_symbol } else { - self.resolve_names(&identifier.node.names, self.ctx.maybe_def)? + match self.resolve_names(&identifier.node.names, self.ctx.maybe_def)? { + Some(symbol) => symbol, + None => return Ok(None), + } }; - Some(symbol_ref) + Ok(Some(symbol_ref)) } pub fn walk_type_expr( @@ -1069,48 +1115,48 @@ impl<'ctx> AdvancedResolver<'ctx> { match &ty_node.node { ast::Type::Any => {} ast::Type::Named(identifier) => { - self.walk_identifier(identifier); + self.walk_identifier(identifier)?; } ast::Type::Basic(_) => {} ast::Type::List(list_type) => { - self.walk_type_expr(list_type.inner_type.as_ref().map(|ty| ty.as_ref())); + self.walk_type_expr(list_type.inner_type.as_ref().map(|ty| ty.as_ref()))?; } ast::Type::Dict(dict_type) => { - self.walk_type_expr(dict_type.key_type.as_ref().map(|ty| ty.as_ref())); - self.walk_type_expr(dict_type.value_type.as_ref().map(|ty| ty.as_ref())); + self.walk_type_expr(dict_type.key_type.as_ref().map(|ty| ty.as_ref()))?; + self.walk_type_expr(dict_type.value_type.as_ref().map(|ty| ty.as_ref()))?; } ast::Type::Union(union_type) => { for elem_ty in union_type.type_elements.iter() { - self.walk_type_expr(Some(elem_ty)); + self.walk_type_expr(Some(elem_ty))?; } } ast::Type::Literal(_) => {} ast::Type::Function(func_type) => { if let Some(params_ty) = &func_type.params_ty { for param_ty in params_ty.iter() { - self.walk_type_expr(Some(param_ty)); + self.walk_type_expr(Some(param_ty))?; } } if let Some(ret_ty) = &func_type.ret_ty { - self.walk_type_expr(Some(&ret_ty)); + self.walk_type_expr(Some(&ret_ty))?; } } } } - None + Ok(None) } pub fn do_arguments_symbol_resolve( &mut self, args: &'ctx [ast::NodeRef], kwargs: &'ctx [ast::NodeRef], - ) { + ) -> anyhow::Result<()> { for arg in args.iter() { - self.expr(arg); + self.expr(arg)?; } for kw in kwargs.iter() { if let Some(value) = &kw.node.value { - self.expr(value); + self.expr(value)?; } let (start_pos, end_pos): Range = kw.get_span_pos(); let value = self.gs.get_symbols_mut().alloc_value_symbol( @@ -1129,9 +1175,13 @@ impl<'ctx> AdvancedResolver<'ctx> { }; } } + Ok(()) } - pub(crate) fn walk_config_entries(&mut self, entries: &'ctx [ast::NodeRef]) { + pub(crate) fn walk_config_entries( + &mut self, + entries: &'ctx [ast::NodeRef], + ) -> anyhow::Result<()> { let (start, end) = (self.ctx.start_pos.clone(), self.ctx.end_pos.clone()); let schema_symbol = self.ctx.current_schema_symbol.take(); @@ -1157,8 +1207,8 @@ impl<'ctx> AdvancedResolver<'ctx> { for entry in entries.iter() { if let Some(key) = &entry.node.key { self.ctx.maybe_def = true; - if let Some(key_symbol_ref) = self.expr(key) { - self.set_current_schema_symbol(key_symbol_ref); + if let Some(key_symbol_ref) = self.expr(key)? { + self.set_current_schema_symbol(key_symbol_ref)?; } self.ctx.maybe_def = false; } @@ -1172,16 +1222,24 @@ impl<'ctx> AdvancedResolver<'ctx> { LocalSymbolScopeKind::Value, ); - self.expr(&entry.node.value); + self.expr(&entry.node.value)?; self.leave_scope(); } - self.leave_scope() + self.leave_scope(); + Ok(()) } - pub(crate) fn set_current_schema_symbol(&mut self, key_symbol_ref: SymbolRef) { + pub(crate) fn set_current_schema_symbol( + &mut self, + key_symbol_ref: SymbolRef, + ) -> anyhow::Result<()> { let symbols = self.gs.get_symbols(); - if let Some(def_symbol_ref) = symbols.get_symbol(key_symbol_ref).unwrap().get_definition() { + if let Some(def_symbol_ref) = symbols + .get_symbol(key_symbol_ref) + .ok_or(anyhow!("def_symbol_ref not found"))? + .get_definition() + { if let Some(node_key) = symbols.symbols_info.symbol_node_map.get(&def_symbol_ref) { if let Some(def_ty) = self.ctx.node_ty_map.get(node_key) { if let Some(ty) = get_possible_schema_ty(def_ty.clone()) { @@ -1205,6 +1263,7 @@ impl<'ctx> AdvancedResolver<'ctx> { _ => None, } } + Ok(()) } pub(crate) fn resolve_decorator(&mut self, decorators: &'ctx [ast::NodeRef]) { diff --git a/kclvm/tools/src/LSP/src/rename.rs b/kclvm/tools/src/LSP/src/rename.rs index b52614fd9..366d522de 100644 --- a/kclvm/tools/src/LSP/src/rename.rs +++ b/kclvm/tools/src/LSP/src/rename.rs @@ -192,7 +192,7 @@ where let gs = GlobalState::default(); let gs = Namer::find_symbols(&program, gs); let node_ty_map = prog_scope.node_ty_map.clone(); - let global_state = AdvancedResolver::resolve_program(&program, gs, node_ty_map); + let global_state = AdvancedResolver::resolve_program(&program, gs, node_ty_map)?; Ok((program, global_state)) } diff --git a/kclvm/tools/src/LSP/src/util.rs b/kclvm/tools/src/LSP/src/util.rs index ba8901403..ea0dba533 100644 --- a/kclvm/tools/src/LSP/src/util.rs +++ b/kclvm/tools/src/LSP/src/util.rs @@ -123,7 +123,7 @@ pub(crate) fn compile_with_params( // Please note that there is no global state cache at this stage. let gs = GlobalState::default(); let gs = Namer::find_symbols(&program, gs); - let gs = AdvancedResolver::resolve_program(&program, gs, prog_scope.node_ty_map.clone()); + let gs = AdvancedResolver::resolve_program(&program, gs, prog_scope.node_ty_map.clone())?; // Merge parse diagnostic and resolve diagnostic sess.append_diagnostic(prog_scope.handler.diagnostics.clone()); let diags = sess.1.borrow().diagnostics.clone();