11use std:: collections:: HashSet ;
22
33use emmylua_code_analysis:: {
4- LuaCompilation , LuaDeclId , LuaMemberId , LuaSemanticDeclId , LuaType , SemanticDeclLevel ,
5- SemanticModel ,
4+ LuaCompilation , LuaDeclId , LuaMemberId , LuaSemanticDeclId , LuaType , LuaUnionType ,
5+ SemanticDeclLevel , SemanticModel ,
66} ;
77use emmylua_parser:: { LuaAssignStat , LuaAstNode , LuaSyntaxKind , LuaTableExpr , LuaTableField } ;
88
@@ -240,6 +240,21 @@ fn resolve_member_owner(
240240 }
241241}
242242
243+ // 判断`table`是否为类
244+ fn table_is_class ( table_type : & LuaType , depth : usize ) -> bool {
245+ if depth > 10 {
246+ return false ;
247+ }
248+ match table_type {
249+ LuaType :: Ref ( _) | LuaType :: Def ( _) | LuaType :: Generic ( _) => true ,
250+ LuaType :: Union ( union) => match union. as_ref ( ) {
251+ LuaUnionType :: Nullable ( t) => table_is_class ( t, depth + 1 ) ,
252+ LuaUnionType :: Multi ( ts) => ts. iter ( ) . any ( |t| table_is_class ( t, depth + 1 ) ) ,
253+ } ,
254+ _ => false ,
255+ }
256+ }
257+
243258fn resolve_table_field_through_type_inference (
244259 semantic_model : & SemanticModel ,
245260 table_field : & LuaTableField ,
@@ -248,10 +263,8 @@ fn resolve_table_field_through_type_inference(
248263 let table_expr = LuaTableExpr :: cast ( parent) ?;
249264 let table_type = semantic_model. infer_table_should_be ( table_expr) ?;
250265
251- if !matches ! (
252- table_type,
253- LuaType :: Ref ( _) | LuaType :: Def ( _) | LuaType :: Generic ( _)
254- ) {
266+ // 必须为类我们才搜索其成员
267+ if !table_is_class ( & table_type, 0 ) {
255268 return None ;
256269 }
257270
0 commit comments