@@ -53,7 +53,7 @@ pub(crate) use crate::types::narrow::infer_narrowing_constraint;
53
53
use crate :: types:: signatures:: { Parameter , ParameterForm , Parameters } ;
54
54
use crate :: types:: tuple:: { TupleSpec , TupleType } ;
55
55
pub use crate :: util:: diagnostics:: add_inferred_python_version_hint_to_diagnostic;
56
- use crate :: { Db , FxOrderSet , Module , Program } ;
56
+ use crate :: { Db , FxIndexSet , FxOrderSet , Module , Program } ;
57
57
pub ( crate ) use class:: { ClassLiteral , ClassType , GenericAlias , KnownClass } ;
58
58
use instance:: Protocol ;
59
59
pub use instance:: { NominalInstanceType , ProtocolInstanceType } ;
@@ -415,12 +415,18 @@ impl<'db> PropertyInstanceType<'db> {
415
415
)
416
416
}
417
417
418
- fn any_over_type ( self , db : & ' db dyn Db , type_fn : & dyn Fn ( Type < ' db > ) -> bool ) -> bool {
418
+ fn any_over_type_impl (
419
+ self ,
420
+ db : & ' db dyn Db ,
421
+ type_fn : & dyn Fn ( Type < ' db > ) -> bool ,
422
+ recursive_fallback : bool ,
423
+ seen_types : & mut FxIndexSet < Type < ' db > > ,
424
+ ) -> bool {
419
425
self . getter ( db)
420
- . is_some_and ( |ty| ty. any_over_type ( db, type_fn) )
421
- || self
422
- . setter ( db)
423
- . is_some_and ( |ty| ty . any_over_type ( db , type_fn ) )
426
+ . is_some_and ( |ty| ty. any_over_type_impl ( db, type_fn, recursive_fallback , seen_types ) )
427
+ || self . setter ( db ) . is_some_and ( |ty| {
428
+ ty . any_over_type_impl ( db, type_fn , recursive_fallback , seen_types )
429
+ } )
424
430
}
425
431
}
426
432
@@ -755,8 +761,27 @@ impl<'db> Type<'db> {
755
761
}
756
762
}
757
763
764
+ pub ( crate ) fn any_over_type (
765
+ self ,
766
+ db : & ' db dyn Db ,
767
+ type_fn : & dyn Fn ( Type < ' db > ) -> bool ,
768
+ recursive_fallback : bool ,
769
+ ) -> bool {
770
+ self . any_over_type_impl ( db, type_fn, recursive_fallback, & mut FxIndexSet :: default ( ) )
771
+ }
772
+
758
773
/// Return `true` if `self`, or any of the types contained in `self`, match the closure passed in.
759
- pub fn any_over_type ( self , db : & ' db dyn Db , type_fn : & dyn Fn ( Type < ' db > ) -> bool ) -> bool {
774
+ pub ( crate ) fn any_over_type_impl (
775
+ self ,
776
+ db : & ' db dyn Db ,
777
+ type_fn : & dyn Fn ( Type < ' db > ) -> bool ,
778
+ recursive_fallback : bool ,
779
+ seen_types : & mut FxIndexSet < Type < ' db > > ,
780
+ ) -> bool {
781
+ if !seen_types. insert ( self ) {
782
+ return recursive_fallback;
783
+ }
784
+
760
785
if type_fn ( self ) {
761
786
return true ;
762
787
}
@@ -787,64 +812,83 @@ impl<'db> Type<'db> {
787
812
. types ( db)
788
813
. iter ( )
789
814
. copied ( )
790
- . any ( |ty| ty. any_over_type ( db, type_fn) ) ,
791
-
792
- Self :: Callable ( callable) => callable. any_over_type ( db, type_fn) ,
815
+ . any ( |ty| ty. any_over_type_impl ( db, type_fn, recursive_fallback, seen_types) ) ,
793
816
794
- Self :: SubclassOf ( subclass_of ) => {
795
- Type :: from ( subclass_of . subclass_of ( ) ) . any_over_type ( db, type_fn)
817
+ Self :: Callable ( callable ) => {
818
+ callable . any_over_type_impl ( db, type_fn, recursive_fallback , seen_types )
796
819
}
797
820
821
+ Self :: SubclassOf ( subclass_of) => Type :: from ( subclass_of. subclass_of ( ) )
822
+ . any_over_type_impl ( db, type_fn, recursive_fallback, seen_types) ,
823
+
798
824
Self :: TypeVar ( typevar) => match typevar. bound_or_constraints ( db) {
799
825
None => false ,
800
826
Some ( TypeVarBoundOrConstraints :: UpperBound ( bound) ) => {
801
- bound. any_over_type ( db, type_fn)
827
+ bound. any_over_type_impl ( db, type_fn, recursive_fallback, seen_types)
828
+ }
829
+ Some ( TypeVarBoundOrConstraints :: Constraints ( constraints) ) => {
830
+ constraints. elements ( db) . iter ( ) . any ( |constraint| {
831
+ constraint. any_over_type_impl ( db, type_fn, recursive_fallback, seen_types)
832
+ } )
802
833
}
803
- Some ( TypeVarBoundOrConstraints :: Constraints ( constraints) ) => constraints
804
- . elements ( db)
805
- . iter ( )
806
- . any ( |constraint| constraint. any_over_type ( db, type_fn) ) ,
807
834
} ,
808
835
809
836
Self :: BoundSuper ( bound_super) => {
810
- Type :: from ( bound_super. pivot_class ( db) ) . any_over_type ( db, type_fn)
811
- || Type :: from ( bound_super. owner ( db) ) . any_over_type ( db, type_fn)
837
+ Type :: from ( bound_super. pivot_class ( db) ) . any_over_type_impl (
838
+ db,
839
+ type_fn,
840
+ recursive_fallback,
841
+ seen_types,
842
+ ) || Type :: from ( bound_super. owner ( db) ) . any_over_type_impl (
843
+ db,
844
+ type_fn,
845
+ recursive_fallback,
846
+ seen_types,
847
+ )
812
848
}
813
849
814
850
Self :: Tuple ( tuple) => tuple
815
851
. tuple ( db)
816
852
. all_elements ( )
817
- . any ( |ty| ty. any_over_type ( db, type_fn) ) ,
853
+ . any ( |ty| ty. any_over_type_impl ( db, type_fn, recursive_fallback , seen_types ) ) ,
818
854
819
855
Self :: Union ( union) => union
820
856
. elements ( db)
821
857
. iter ( )
822
- . any ( |ty| ty. any_over_type ( db, type_fn) ) ,
858
+ . any ( |ty| ty. any_over_type_impl ( db, type_fn, recursive_fallback , seen_types ) ) ,
823
859
824
860
Self :: Intersection ( intersection) => {
825
861
intersection
826
862
. positive ( db)
827
863
. iter ( )
828
- . any ( |ty| ty. any_over_type ( db, type_fn) )
829
- || intersection
830
- . negative ( db)
831
- . iter ( )
832
- . any ( |ty| ty. any_over_type ( db, type_fn) )
864
+ . any ( |ty| ty. any_over_type_impl ( db, type_fn, recursive_fallback, seen_types) )
865
+ || intersection. negative ( db) . iter ( ) . any ( |ty| {
866
+ ty. any_over_type_impl ( db, type_fn, recursive_fallback, seen_types)
867
+ } )
833
868
}
834
869
835
- Self :: ProtocolInstance ( protocol) => protocol. any_over_type ( db, type_fn) ,
836
- Self :: PropertyInstance ( property) => property. any_over_type ( db, type_fn) ,
870
+ Self :: ProtocolInstance ( protocol) => {
871
+ protocol. any_over_type_impl ( db, type_fn, recursive_fallback, seen_types)
872
+ }
873
+ Self :: PropertyInstance ( property) => {
874
+ property. any_over_type_impl ( db, type_fn, recursive_fallback, seen_types)
875
+ }
837
876
838
877
Self :: NominalInstance ( instance) => match instance. class {
839
878
ClassType :: NonGeneric ( _) => false ,
840
- ClassType :: Generic ( generic) => generic
841
- . specialization ( db)
842
- . types ( db)
843
- . iter ( )
844
- . any ( |ty| ty . any_over_type ( db , type_fn ) ) ,
879
+ ClassType :: Generic ( generic) => {
880
+ generic . specialization ( db) . types ( db ) . iter ( ) . any ( |ty| {
881
+ ty . any_over_type_impl ( db, type_fn , recursive_fallback , seen_types )
882
+ } )
883
+ }
845
884
} ,
846
885
847
- Self :: TypeIs ( type_is) => type_is. return_type ( db) . any_over_type ( db, type_fn) ,
886
+ Self :: TypeIs ( type_is) => type_is. return_type ( db) . any_over_type_impl (
887
+ db,
888
+ type_fn,
889
+ recursive_fallback,
890
+ seen_types,
891
+ ) ,
848
892
}
849
893
}
850
894
@@ -7298,15 +7342,21 @@ impl<'db> CallableType<'db> {
7298
7342
. is_equivalent_to ( db, other. signatures ( db) )
7299
7343
}
7300
7344
7301
- fn any_over_type ( self , db : & ' db dyn Db , type_fn : & dyn Fn ( Type < ' db > ) -> bool ) -> bool {
7345
+ fn any_over_type_impl (
7346
+ self ,
7347
+ db : & ' db dyn Db ,
7348
+ type_fn : & dyn Fn ( Type < ' db > ) -> bool ,
7349
+ recursive_fallback : bool ,
7350
+ seen_types : & mut FxIndexSet < Type < ' db > > ,
7351
+ ) -> bool {
7302
7352
self . signatures ( db) . iter ( ) . any ( |signature| {
7303
7353
signature. parameters ( ) . iter ( ) . any ( |param| {
7304
- param
7305
- . annotated_type ( )
7306
- . is_some_and ( |ty| ty . any_over_type ( db , type_fn ) )
7307
- } ) || signature
7308
- . return_ty
7309
- . is_some_and ( |ty| ty . any_over_type ( db , type_fn ) )
7354
+ param. annotated_type ( ) . is_some_and ( |ty| {
7355
+ ty . any_over_type_impl ( db , type_fn , recursive_fallback , seen_types )
7356
+ } )
7357
+ } ) || signature. return_ty . is_some_and ( |ty| {
7358
+ ty . any_over_type_impl ( db , type_fn , recursive_fallback , seen_types )
7359
+ } )
7310
7360
} )
7311
7361
}
7312
7362
}
0 commit comments