1+ use std:: sync:: Arc ;
2+
13use emmylua_parser:: { LuaAstNode , LuaIndexKey , LuaIndexMemberExpr } ;
24use rowan:: TextRange ;
35use smol_str:: SmolStr ;
@@ -24,11 +26,11 @@ pub struct FindFunctionType {
2426}
2527
2628#[ derive( Debug ) ]
27- struct DeepGuard {
29+ struct DeepLevel {
2830 deep : usize ,
2931}
3032
31- impl DeepGuard {
33+ impl DeepLevel {
3234 pub fn new ( ) -> Self {
3335 Self { deep : 0 }
3436 }
@@ -64,13 +66,13 @@ pub fn find_decl_function_type(
6466 index_member_expr
6567 . get_prefix_expr ( )
6668 . ok_or ( InferFailReason :: None ) ?;
67- let mut deep_guard = DeepGuard :: new ( ) ;
69+ let mut deep_guard = DeepLevel :: new ( ) ;
6870 let reason = match find_function_type_by_member_key (
6971 db,
7072 cache,
7173 prefix_type,
7274 index_member_expr. clone ( ) ,
73- & mut InferGuard :: new ( ) ,
75+ & InferGuard :: new ( ) ,
7476 & mut deep_guard,
7577 ) {
7678 Ok ( member_type) => {
@@ -83,13 +85,13 @@ pub fn find_decl_function_type(
8385 Err ( err) => return Err ( err) ,
8486 } ;
8587
86- let mut deep_guard = DeepGuard :: new ( ) ;
88+ let mut deep_guard = DeepLevel :: new ( ) ;
8789 match find_function_type_by_operator (
8890 db,
8991 cache,
9092 prefix_type,
9193 index_member_expr,
92- & mut InferGuard :: new ( ) ,
94+ & InferGuard :: new ( ) ,
9395 & mut deep_guard,
9496 ) {
9597 Ok ( member_type) => {
@@ -110,8 +112,8 @@ fn find_function_type_by_member_key(
110112 cache : & mut LuaInferCache ,
111113 prefix_type : & LuaType ,
112114 index_expr : LuaIndexMemberExpr ,
113- infer_guard : & mut InferGuard ,
114- deep_guard : & mut DeepGuard ,
115+ infer_guard : & Arc < InferGuard > ,
116+ deep_guard : & mut DeepLevel ,
115117) -> FunctionTypeResult {
116118 match & prefix_type {
117119 LuaType :: Ref ( decl_id) => find_custom_type_function_member (
@@ -135,10 +137,10 @@ fn find_function_type_by_member_key(
135137 find_object_function_member ( db, cache, object_type, index_expr)
136138 }
137139 LuaType :: Union ( union_type) => {
138- find_union_function_member ( db, cache, union_type, index_expr, deep_guard)
140+ find_union_function_member ( db, cache, union_type, index_expr, infer_guard , deep_guard)
139141 }
140142 LuaType :: Generic ( generic_type) => {
141- find_generic_member ( db, cache, generic_type, index_expr, deep_guard)
143+ find_generic_member ( db, cache, generic_type, index_expr, infer_guard , deep_guard)
142144 }
143145 LuaType :: Instance ( inst) => {
144146 find_instance_member_decl_type ( db, cache, inst, index_expr, infer_guard, deep_guard)
@@ -177,8 +179,8 @@ fn find_custom_type_function_member(
177179 cache : & mut LuaInferCache ,
178180 prefix_type_id : LuaTypeDeclId ,
179181 index_expr : LuaIndexMemberExpr ,
180- infer_guard : & mut InferGuard ,
181- deep_guard : & mut DeepGuard ,
182+ infer_guard : & Arc < InferGuard > ,
183+ deep_guard : & mut DeepLevel ,
182184) -> FunctionTypeResult {
183185 infer_guard. check ( & prefix_type_id) ?;
184186 let type_index = db. get_type_index ( ) ;
@@ -321,7 +323,8 @@ fn find_union_function_member(
321323 cache : & mut LuaInferCache ,
322324 union_type : & LuaUnionType ,
323325 index_expr : LuaIndexMemberExpr ,
324- deep_guard : & mut DeepGuard ,
326+ infer_guard : & Arc < InferGuard > ,
327+ deep_guard : & mut DeepLevel ,
325328) -> FunctionTypeResult {
326329 let mut member_types = Vec :: new ( ) ;
327330 for sub_type in union_type. into_vec ( ) {
@@ -330,7 +333,7 @@ fn find_union_function_member(
330333 cache,
331334 & sub_type,
332335 index_expr. clone ( ) ,
333- & mut InferGuard :: new ( ) ,
336+ infer_guard ,
334337 deep_guard,
335338 ) ;
336339 if let Ok ( typ) = result
@@ -349,7 +352,8 @@ fn index_generic_members_from_super_generics(
349352 type_decl_id : & LuaTypeDeclId ,
350353 substitutor : & TypeSubstitutor ,
351354 index_expr : LuaIndexMemberExpr ,
352- deep_guard : & mut DeepGuard ,
355+ infer_guard : & Arc < InferGuard > ,
356+ deep_guard : & mut DeepLevel ,
353357) -> Option < LuaType > {
354358 let type_index = db. get_type_index ( ) ;
355359
@@ -367,7 +371,7 @@ fn index_generic_members_from_super_generics(
367371 cache,
368372 & super_type,
369373 index_expr. clone ( ) ,
370- & mut InferGuard :: new ( ) ,
374+ & infer_guard . fork ( ) ,
371375 deep_guard,
372376 )
373377 . ok ( )
@@ -382,7 +386,8 @@ fn find_generic_member(
382386 cache : & mut LuaInferCache ,
383387 generic_type : & LuaGenericType ,
384388 index_expr : LuaIndexMemberExpr ,
385- deep_guard : & mut DeepGuard ,
389+ infer_guard : & Arc < InferGuard > ,
390+ deep_guard : & mut DeepLevel ,
386391) -> FunctionTypeResult {
387392 let base_type = generic_type. get_base_type ( ) ;
388393
@@ -395,6 +400,7 @@ fn find_generic_member(
395400 base_type_decl_id,
396401 & substitutor,
397402 index_expr. clone ( ) ,
403+ infer_guard,
398404 deep_guard,
399405 ) ;
400406 if let Some ( result) = result {
@@ -407,7 +413,7 @@ fn find_generic_member(
407413 cache,
408414 & base_type,
409415 index_expr,
410- & mut InferGuard :: new ( ) ,
416+ infer_guard ,
411417 deep_guard,
412418 ) ?;
413419
@@ -419,8 +425,8 @@ fn find_instance_member_decl_type(
419425 cache : & mut LuaInferCache ,
420426 inst : & LuaInstanceType ,
421427 index_expr : LuaIndexMemberExpr ,
422- infer_guard : & mut InferGuard ,
423- deep_guard : & mut DeepGuard ,
428+ infer_guard : & Arc < InferGuard > ,
429+ deep_guard : & mut DeepLevel ,
424430) -> FunctionTypeResult {
425431 let origin_type = inst. get_base ( ) ;
426432 find_function_type_by_member_key (
@@ -438,8 +444,8 @@ fn find_function_type_by_operator(
438444 cache : & mut LuaInferCache ,
439445 prefix_type : & LuaType ,
440446 index_expr : LuaIndexMemberExpr ,
441- infer_guard : & mut InferGuard ,
442- deep_guard : & mut DeepGuard ,
447+ infer_guard : & Arc < InferGuard > ,
448+ deep_guard : & mut DeepLevel ,
443449) -> FunctionTypeResult {
444450 match & prefix_type {
445451 LuaType :: TableConst ( in_filed) => {
@@ -467,13 +473,18 @@ fn find_function_type_by_operator(
467473 }
468474 LuaType :: Object ( object) => infer_member_by_index_object ( db, cache, object, index_expr) ,
469475 LuaType :: Union ( union) => {
470- find_member_by_index_union ( db, cache, union, index_expr, deep_guard)
471- }
472- LuaType :: Intersection ( intersection) => {
473- find_member_by_index_intersection ( db, cache, intersection, index_expr, deep_guard)
476+ find_member_by_index_union ( db, cache, union, index_expr, infer_guard, deep_guard)
474477 }
478+ LuaType :: Intersection ( intersection) => find_member_by_index_intersection (
479+ db,
480+ cache,
481+ intersection,
482+ index_expr,
483+ infer_guard,
484+ deep_guard,
485+ ) ,
475486 LuaType :: Generic ( generic) => {
476- find_member_by_index_generic ( db, cache, generic, index_expr, deep_guard)
487+ find_member_by_index_generic ( db, cache, generic, index_expr, infer_guard , deep_guard)
477488 }
478489 LuaType :: TableGeneric ( table_generic) => {
479490 find_member_by_index_table_generic ( db, cache, table_generic, index_expr)
@@ -566,8 +577,8 @@ fn find_member_by_index_custom_type(
566577 cache : & mut LuaInferCache ,
567578 prefix_type_id : & LuaTypeDeclId ,
568579 index_expr : LuaIndexMemberExpr ,
569- infer_guard : & mut InferGuard ,
570- deep_guard : & mut DeepGuard ,
580+ infer_guard : & Arc < InferGuard > ,
581+ deep_guard : & mut DeepLevel ,
571582) -> FunctionTypeResult {
572583 infer_guard. check ( prefix_type_id) ?;
573584 let type_index = db. get_type_index ( ) ;
@@ -680,7 +691,8 @@ fn find_member_by_index_union(
680691 cache : & mut LuaInferCache ,
681692 union : & LuaUnionType ,
682693 index_expr : LuaIndexMemberExpr ,
683- deep_guard : & mut DeepGuard ,
694+ infer_guard : & Arc < InferGuard > ,
695+ deep_guard : & mut DeepLevel ,
684696) -> FunctionTypeResult {
685697 let mut member_type = LuaType :: Unknown ;
686698 for member in union. into_vec ( ) {
@@ -689,7 +701,7 @@ fn find_member_by_index_union(
689701 cache,
690702 & member,
691703 index_expr. clone ( ) ,
692- & mut InferGuard :: new ( ) ,
704+ & infer_guard . fork ( ) ,
693705 deep_guard,
694706 ) ;
695707 match result {
@@ -715,15 +727,16 @@ fn find_member_by_index_intersection(
715727 cache : & mut LuaInferCache ,
716728 intersection : & LuaIntersectionType ,
717729 index_expr : LuaIndexMemberExpr ,
718- deep_guard : & mut DeepGuard ,
730+ infer_guard : & Arc < InferGuard > ,
731+ deep_guard : & mut DeepLevel ,
719732) -> FunctionTypeResult {
720733 for member in intersection. get_types ( ) {
721734 match find_function_type_by_operator (
722735 db,
723736 cache,
724737 member,
725738 index_expr. clone ( ) ,
726- & mut InferGuard :: new ( ) ,
739+ & infer_guard . fork ( ) ,
727740 deep_guard,
728741 ) {
729742 Ok ( ty) => return Ok ( ty) ,
@@ -742,7 +755,8 @@ fn find_member_by_index_generic(
742755 cache : & mut LuaInferCache ,
743756 generic : & LuaGenericType ,
744757 index_expr : LuaIndexMemberExpr ,
745- deep_guard : & mut DeepGuard ,
758+ infer_guard : & Arc < InferGuard > ,
759+ deep_guard : & mut DeepLevel ,
746760) -> FunctionTypeResult {
747761 let base_type = generic. get_base_type ( ) ;
748762 let type_decl_id = if let LuaType :: Ref ( id) = base_type {
@@ -763,7 +777,7 @@ fn find_member_by_index_generic(
763777 cache,
764778 & instantiate_type_generic ( db, & origin_type, & substitutor) ,
765779 index_expr. clone ( ) ,
766- & mut InferGuard :: new ( ) ,
780+ & infer_guard . fork ( ) ,
767781 deep_guard,
768782 ) ;
769783 }
@@ -807,7 +821,7 @@ fn find_member_by_index_generic(
807821 cache,
808822 & instantiate_type_generic ( db, & super_type, & substitutor) ,
809823 index_expr. clone ( ) ,
810- & mut InferGuard :: new ( ) ,
824+ & infer_guard . fork ( ) ,
811825 deep_guard,
812826 ) ;
813827 match result {
0 commit comments