diff --git a/kclvm/sema/src/advanced_resolver/mod.rs b/kclvm/sema/src/advanced_resolver/mod.rs index ad596b66a..d59addef8 100644 --- a/kclvm/sema/src/advanced_resolver/mod.rs +++ b/kclvm/sema/src/advanced_resolver/mod.rs @@ -396,47 +396,12 @@ mod tests { ], ), ( - "src/advanced_resolver/test_data/pkg/pkg.k" + "src/advanced_resolver/test_data/import_test/d.k" .to_string() .replace("/", &std::path::MAIN_SEPARATOR.to_string()), vec![ - (1, 7, 1, 11, "Name".to_string(), SymbolKind::Schema), - (2, 4, 2, 8, "name".to_string(), SymbolKind::Attribute), - (4, 7, 4, 13, "Person".to_string(), SymbolKind::Schema), - (5, 4, 5, 8, "name".to_string(), SymbolKind::Attribute), - (5, 10, 5, 14, "Name".to_string(), SymbolKind::Unresolved), - ( - 1, - 7, - 1, - 11, - "src/advanced_resolver/test_data/pkg/pkg.k" - .to_string() - .replace("/", &std::path::MAIN_SEPARATOR.to_string()), - SymbolKind::Schema, - ), - (5, 17, 5, 21, "Name".to_string(), SymbolKind::Unresolved), - ( - 1, - 7, - 1, - 11, - "src/advanced_resolver/test_data/pkg/pkg.k" - .to_string() - .replace("/", &std::path::MAIN_SEPARATOR.to_string()), - SymbolKind::Schema, - ), - (5, 23, 5, 27, "name".to_string(), SymbolKind::Unresolved), - ( - 2, - 4, - 2, - 8, - "src/advanced_resolver/test_data/pkg/pkg.k" - .to_string() - .replace("/", &std::path::MAIN_SEPARATOR.to_string()), - SymbolKind::Attribute, - ), + (1, 7, 1, 13, "Parent".to_string(), SymbolKind::Schema), + (2, 4, 2, 8, "age1".to_string(), SymbolKind::Attribute), ], ), ( @@ -1068,6 +1033,40 @@ mod tests { .replace("/", &std::path::MAIN_SEPARATOR.to_string()), SymbolKind::Schema, ), + (34, 4, 34, 8, "name".to_string(), SymbolKind::Unresolved), + ( + 5, + 4, + 5, + 8, + "src/advanced_resolver/test_data/pkg/pkg.k" + .to_string() + .replace("/", &std::path::MAIN_SEPARATOR.to_string()), + SymbolKind::Attribute, + ), + (34, 9, 34, 13, "name".to_string(), SymbolKind::Unresolved), + ( + 2, + 4, + 2, + 8, + "src/advanced_resolver/test_data/pkg/pkg.k" + .to_string() + .replace("/", &std::path::MAIN_SEPARATOR.to_string()), + SymbolKind::Attribute, + ), + (37, 0, 37, 1, "x".to_string(), SymbolKind::Value), + (38, 16, 38, 17, "x".to_string(), SymbolKind::Unresolved), + ( + 37, + 0, + 37, + 1, + "src/advanced_resolver/test_data/schema_symbols.k" + .to_string() + .replace("/", &std::path::MAIN_SEPARATOR.to_string()), + SymbolKind::Value, + ), ], ), ( @@ -1089,12 +1088,47 @@ mod tests { ], ), ( - "src/advanced_resolver/test_data/import_test/d.k" + "src/advanced_resolver/test_data/pkg/pkg.k" .to_string() .replace("/", &std::path::MAIN_SEPARATOR.to_string()), vec![ - (1, 7, 1, 13, "Parent".to_string(), SymbolKind::Schema), - (2, 4, 2, 8, "age1".to_string(), SymbolKind::Attribute), + (1, 7, 1, 11, "Name".to_string(), SymbolKind::Schema), + (2, 4, 2, 8, "name".to_string(), SymbolKind::Attribute), + (4, 7, 4, 13, "Person".to_string(), SymbolKind::Schema), + (5, 4, 5, 8, "name".to_string(), SymbolKind::Attribute), + (5, 10, 5, 14, "Name".to_string(), SymbolKind::Unresolved), + ( + 1, + 7, + 1, + 11, + "src/advanced_resolver/test_data/pkg/pkg.k" + .to_string() + .replace("/", &std::path::MAIN_SEPARATOR.to_string()), + SymbolKind::Schema, + ), + (5, 17, 5, 21, "Name".to_string(), SymbolKind::Unresolved), + ( + 1, + 7, + 1, + 11, + "src/advanced_resolver/test_data/pkg/pkg.k" + .to_string() + .replace("/", &std::path::MAIN_SEPARATOR.to_string()), + SymbolKind::Schema, + ), + (5, 23, 5, 27, "name".to_string(), SymbolKind::Unresolved), + ( + 2, + 4, + 2, + 8, + "src/advanced_resolver/test_data/pkg/pkg.k" + .to_string() + .replace("/", &std::path::MAIN_SEPARATOR.to_string()), + SymbolKind::Attribute, + ), ], ), ( @@ -1202,7 +1236,7 @@ mod tests { "src/advanced_resolver/test_data/schema_symbols.k" .to_string() .replace("/", &std::path::MAIN_SEPARATOR.to_string()), - 34_u64, + 35_u64, 5_u64, Some((33, 13, 33, 19, "Person".to_string(), SymbolKind::Unresolved)), ), @@ -1264,7 +1298,7 @@ mod tests { .replace("/", &std::path::MAIN_SEPARATOR.to_string()), 17_u64, 26_u64, - 9_usize, + 10_usize, ), // __main__.Main schema expr scope ( @@ -1273,7 +1307,7 @@ mod tests { .replace("/", &std::path::MAIN_SEPARATOR.to_string()), 30, 41, - 9, + 10, ), // pkg.Person schema expr scope ( @@ -1282,16 +1316,16 @@ mod tests { .replace("/", &std::path::MAIN_SEPARATOR.to_string()), 33, 21, - 5, + 6, ), // __main__ package scope ( "src/advanced_resolver/test_data/schema_symbols.k" .to_string() .replace("/", &std::path::MAIN_SEPARATOR.to_string()), - 34, + 36, 31, - 4, + 5, ), // import_test.a.Person expr scope ( diff --git a/kclvm/sema/src/advanced_resolver/node.rs b/kclvm/sema/src/advanced_resolver/node.rs index 5d445d58c..8875f9e08 100644 --- a/kclvm/sema/src/advanced_resolver/node.rs +++ b/kclvm/sema/src/advanced_resolver/node.rs @@ -95,6 +95,9 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { if let Some(if_cond) = &assert_stmt.if_cond { self.expr(if_cond); } + if let Some(msg) = &assert_stmt.msg { + self.expr(msg); + } None } @@ -762,38 +765,38 @@ impl<'ctx> AdvancedResolver<'ctx> { .get_scopes_mut() .add_ref_to_scope(cur_scope, first_unresolved_ref); } + if names.len() > 1 { + let mut parent_ty = self.ctx.node_ty_map.get(&first_name.id)?; - let mut parent_ty = self.ctx.node_ty_map.get(&first_name.id)?; - - for index in 1..names.len() { - let name = names.get(index).unwrap(); - let def_symbol_ref = self.gs.get_symbols().get_type_attribute( - &parent_ty, - &name.node, - self.get_current_module_info(), - )?; - - let (start_pos, end_pos): Range = name.get_span_pos(); - let ast_id = name.id.clone(); - let mut unresolved = - UnresolvedSymbol::new(name.node.clone(), start_pos, end_pos, None); - unresolved.def = Some(def_symbol_ref); - let unresolved_ref = self - .gs - .get_symbols_mut() - .alloc_unresolved_symbol(unresolved, &ast_id); - - let cur_scope = *self.ctx.scopes.last().unwrap(); - self.gs - .get_scopes_mut() - .add_ref_to_scope(cur_scope, unresolved_ref); + for index in 1..names.len() { + let name = names.get(index).unwrap(); + let def_symbol_ref = self.gs.get_symbols().get_type_attribute( + &parent_ty, + &name.node, + self.get_current_module_info(), + )?; - parent_ty = self.ctx.node_ty_map.get(&name.id)?; - if index == names.len() - 1 { - return Some(unresolved_ref); + let (start_pos, end_pos): Range = name.get_span_pos(); + let ast_id = name.id.clone(); + let mut unresolved = + UnresolvedSymbol::new(name.node.clone(), start_pos, end_pos, None); + unresolved.def = Some(def_symbol_ref); + let unresolved_ref = self + .gs + .get_symbols_mut() + .alloc_unresolved_symbol(unresolved, &ast_id); + + let cur_scope = *self.ctx.scopes.last().unwrap(); + self.gs + .get_scopes_mut() + .add_ref_to_scope(cur_scope, unresolved_ref); + + parent_ty = self.ctx.node_ty_map.get(&name.id)?; + if index == names.len() - 1 { + return Some(unresolved_ref); + } } } - Some(symbol_ref) } None => { @@ -975,17 +978,34 @@ impl<'ctx> AdvancedResolver<'ctx> { let cur_scope = self.ctx.scopes.last().unwrap(); self.gs .get_scopes_mut() - .set_owner_to_scope(*cur_scope, owner) + .set_owner_to_scope(*cur_scope, owner); } for entry in entries.iter() { if let Some(key) = &entry.node.key { self.ctx.maybe_def = true; - self.expr(key); + if let Some(key_symbol_ref) = self.expr(key) { + self.set_config_scope_owner(key_symbol_ref); + } self.ctx.maybe_def = false; } self.expr(&entry.node.value); } self.leave_scope() } + + pub(crate) fn set_config_scope_owner(&mut self, key_symbol_ref: SymbolRef) { + 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_ast_id) = symbols.symbols_info.symbol_ref_map.get(&def_symbol_ref) { + if let Some(def_ty) = self.ctx.node_ty_map.get(def_ast_id) { + if def_ty.is_schema() { + self.ctx.current_schema_symbol = + self.gs.get_symbols().get_type_symbol(&def_ty, None); + } + } + } + } + } } diff --git a/kclvm/sema/src/advanced_resolver/test_data/schema_symbols.k b/kclvm/sema/src/advanced_resolver/test_data/schema_symbols.k index f353ef2c4..2a2139846 100644 --- a/kclvm/sema/src/advanced_resolver/test_data/schema_symbols.k +++ b/kclvm/sema/src/advanced_resolver/test_data/schema_symbols.k @@ -30,4 +30,9 @@ p = Main{ age = b._b + a._person?.age } -person = pkg.Person {} +person = pkg.Person { + name.name = "" +} + +x = "123" +assert True, "${x}456" diff --git a/kclvm/sema/src/core/symbol.rs b/kclvm/sema/src/core/symbol.rs index 549554927..a15855dea 100644 --- a/kclvm/sema/src/core/symbol.rs +++ b/kclvm/sema/src/core/symbol.rs @@ -71,7 +71,7 @@ pub struct SymbolDB { pub(crate) fully_qualified_name_map: IndexMap, pub(crate) schema_builtin_symbols: IndexMap>, pub(crate) ast_id_map: IndexMap, - pub(crate) symbol_ty_map: IndexMap>, + pub(crate) symbol_ref_map: IndexMap, } impl KCLSymbolData { @@ -319,11 +319,6 @@ impl KCLSymbolData { } } - pub fn add_symbol_info(&mut self, symbol: SymbolRef, ty: Arc, ast_id: AstIndex) { - self.symbols_info.ast_id_map.insert(ast_id, symbol); - self.symbols_info.symbol_ty_map.insert(symbol, ty); - } - pub fn get_symbol_by_ast_index(&self, id: &AstIndex) -> Option { self.symbols_info.ast_id_map.get(id).cloned() } @@ -434,6 +429,9 @@ impl KCLSymbolData { self.symbols_info .ast_id_map .insert(ast_id.clone(), symbol_ref); + self.symbols_info + .symbol_ref_map + .insert(symbol_ref, ast_id.clone()); self.schemas.get_mut(symbol_id).unwrap().id = Some(symbol_ref); symbol_ref } @@ -451,6 +449,9 @@ impl KCLSymbolData { self.symbols_info .ast_id_map .insert(ast_id.clone(), symbol_ref); + self.symbols_info + .symbol_ref_map + .insert(symbol_ref, ast_id.clone()); self.unresolved.get_mut(symbol_id).unwrap().id = Some(symbol_ref); symbol_ref } @@ -468,6 +469,9 @@ impl KCLSymbolData { self.symbols_info .ast_id_map .insert(ast_id.clone(), symbol_ref); + self.symbols_info + .symbol_ref_map + .insert(symbol_ref, ast_id.clone()); self.type_aliases.get_mut(symbol_id).unwrap().id = Some(symbol_ref); symbol_ref } @@ -481,6 +485,9 @@ impl KCLSymbolData { self.symbols_info .ast_id_map .insert(ast_id.clone(), symbol_ref); + self.symbols_info + .symbol_ref_map + .insert(symbol_ref, ast_id.clone()); self.rules.get_mut(symbol_id).unwrap().id = Some(symbol_ref); symbol_ref } @@ -498,6 +505,9 @@ impl KCLSymbolData { self.symbols_info .ast_id_map .insert(ast_id.clone(), symbol_ref); + self.symbols_info + .symbol_ref_map + .insert(symbol_ref, ast_id.clone()); self.attributes.get_mut(symbol_id).unwrap().id = Some(symbol_ref); symbol_ref } @@ -511,6 +521,9 @@ impl KCLSymbolData { self.symbols_info .ast_id_map .insert(ast_id.clone(), symbol_ref); + self.symbols_info + .symbol_ref_map + .insert(symbol_ref, ast_id.clone()); self.values.get_mut(symbol_id).unwrap().id = Some(symbol_ref); symbol_ref } diff --git a/kclvm/tools/src/LSP/src/goto_def.rs b/kclvm/tools/src/LSP/src/goto_def.rs index b7addcd85..dc34f715d 100644 --- a/kclvm/tools/src/LSP/src/goto_def.rs +++ b/kclvm/tools/src/LSP/src/goto_def.rs @@ -657,7 +657,7 @@ mod tests { }; let res = goto_definition_with_gs(&program, &pos, &prog_scope, &gs); - compare_goto_res(res, (&file, 81, 6, 81, 10)); + compare_goto_res(res, (&file, 18, 4, 18, 8)); } #[test]