From 0f48061573dfc8adc90fdd273ed6356fb3534ddd Mon Sep 17 00:00:00 2001 From: never Date: Tue, 28 Nov 2023 13:59:50 +0800 Subject: [PATCH] fix: resolve assert_stmt and config_entry correctly --- kclvm/sema/src/advanced_resolver/mod.rs | 128 +++++++++++------- kclvm/sema/src/advanced_resolver/node.rs | 86 ++++++++---- .../test_data/schema_symbols.k | 7 +- 3 files changed, 143 insertions(+), 78 deletions(-) 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..16646fb62 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,13 +978,36 @@ 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) { + if let Some(def_symbol_ref) = self + .gs + .get_symbols() + .get_symbol(key_symbol_ref) + .unwrap() + .get_definition() + { + if let Some(def_ty) = &self + .gs + .get_symbols() + .get_symbol(def_symbol_ref) + .unwrap() + .get_sema_info() + .ty + { + if def_ty.is_schema() { + self.ctx.current_schema_symbol = + self.gs.get_symbols().get_type_symbol(&def_ty, None); + } + } + } + } + self.ctx.maybe_def = false; } self.expr(&entry.node.value); 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"