@@ -261,33 +261,45 @@ impl InherentImpls {
261
261
let mut impls = Self { map : FxHashMap :: default ( ) } ;
262
262
263
263
let crate_def_map = db. crate_def_map ( krate) ;
264
- collect_def_map ( db, & crate_def_map, & mut impls ) ;
264
+ impls . collect_def_map ( db, & crate_def_map) ;
265
265
266
266
return Arc :: new ( impls) ;
267
+ }
268
+
269
+ pub ( crate ) fn inherent_impls_in_block_query (
270
+ db : & dyn HirDatabase ,
271
+ block : BlockId ,
272
+ ) -> Option < Arc < Self > > {
273
+ let mut impls = Self { map : FxHashMap :: default ( ) } ;
274
+ if let Some ( block_def_map) = db. block_def_map ( block) {
275
+ impls. collect_def_map ( db, & block_def_map) ;
276
+ return Some ( Arc :: new ( impls) ) ;
277
+ }
278
+ return None ;
279
+ }
280
+
281
+ fn collect_def_map ( & mut self , db : & dyn HirDatabase , def_map : & DefMap ) {
282
+ for ( _module_id, module_data) in def_map. modules ( ) {
283
+ for impl_id in module_data. scope . impls ( ) {
284
+ let data = db. impl_data ( impl_id) ;
285
+ if data. target_trait . is_some ( ) {
286
+ continue ;
287
+ }
267
288
268
- fn collect_def_map ( db : & dyn HirDatabase , def_map : & DefMap , impls : & mut InherentImpls ) {
269
- for ( _module_id, module_data) in def_map. modules ( ) {
270
- for impl_id in module_data. scope . impls ( ) {
271
- let data = db. impl_data ( impl_id) ;
272
- if data. target_trait . is_some ( ) {
273
- continue ;
274
- }
275
-
276
- let self_ty = db. impl_self_ty ( impl_id) ;
277
- let fp = TyFingerprint :: for_inherent_impl ( self_ty. skip_binders ( ) ) ;
278
- if let Some ( fp) = fp {
279
- impls. map . entry ( fp) . or_default ( ) . push ( impl_id) ;
280
- }
281
- // `fp` should only be `None` in error cases (either erroneous code or incomplete name resolution)
289
+ let self_ty = db. impl_self_ty ( impl_id) ;
290
+ let fp = TyFingerprint :: for_inherent_impl ( self_ty. skip_binders ( ) ) ;
291
+ if let Some ( fp) = fp {
292
+ self . map . entry ( fp) . or_default ( ) . push ( impl_id) ;
282
293
}
294
+ // `fp` should only be `None` in error cases (either erroneous code or incomplete name resolution)
295
+ }
283
296
284
- // To better support custom derives, collect impls in all unnamed const items.
285
- // const _: () = { ... };
286
- for konst in module_data. scope . unnamed_consts ( ) {
287
- let body = db. body ( konst. into ( ) ) ;
288
- for ( _, block_def_map) in body. blocks ( db. upcast ( ) ) {
289
- collect_def_map ( db, & block_def_map, impls) ;
290
- }
297
+ // To better support custom derives, collect impls in all unnamed const items.
298
+ // const _: () = { ... };
299
+ for konst in module_data. scope . unnamed_consts ( ) {
300
+ let body = db. body ( konst. into ( ) ) ;
301
+ for ( _, block_def_map) in body. blocks ( db. upcast ( ) ) {
302
+ self . collect_def_map ( db, & block_def_map) ;
291
303
}
292
304
}
293
305
}
@@ -744,11 +756,49 @@ fn iterate_inherent_methods(
744
756
None => return ControlFlow :: Continue ( ( ) ) ,
745
757
} ;
746
758
759
+ if let Some ( module_id) = visible_from_module {
760
+ if let Some ( block_id) = module_id. containing_block ( ) {
761
+ if let Some ( impls) = db. inherent_impls_in_block ( block_id) {
762
+ impls_for_self_ty (
763
+ & impls,
764
+ self_ty,
765
+ db,
766
+ env. clone ( ) ,
767
+ name,
768
+ receiver_ty,
769
+ visible_from_module,
770
+ callback,
771
+ ) ?;
772
+ }
773
+ }
774
+ }
775
+
747
776
for krate in def_crates {
748
777
let impls = db. inherent_impls_in_crate ( krate) ;
778
+ impls_for_self_ty (
779
+ & impls,
780
+ self_ty,
781
+ db,
782
+ env. clone ( ) ,
783
+ name,
784
+ receiver_ty,
785
+ visible_from_module,
786
+ callback,
787
+ ) ?;
788
+ }
789
+ return ControlFlow :: Continue ( ( ) ) ;
749
790
750
- let impls_for_self_ty = filter_inherent_impls_for_self_ty ( & impls, & self_ty. value ) ;
751
-
791
+ fn impls_for_self_ty (
792
+ impls : & InherentImpls ,
793
+ self_ty : & Canonical < Ty > ,
794
+ db : & dyn HirDatabase ,
795
+ env : Arc < TraitEnvironment > ,
796
+ name : Option < & Name > ,
797
+ receiver_ty : Option < & Canonical < Ty > > ,
798
+ visible_from_module : Option < ModuleId > ,
799
+ callback : & mut dyn FnMut ( & Canonical < Ty > , AssocItemId ) -> ControlFlow < ( ) > ,
800
+ ) -> ControlFlow < ( ) > {
801
+ let impls_for_self_ty = filter_inherent_impls_for_self_ty ( impls, & self_ty. value ) ;
752
802
for & impl_def in impls_for_self_ty {
753
803
for & item in db. impl_data ( impl_def) . items . iter ( ) {
754
804
if !is_valid_candidate (
@@ -776,8 +826,8 @@ fn iterate_inherent_methods(
776
826
callback ( receiver_ty, item) ?;
777
827
}
778
828
}
829
+ ControlFlow :: Continue ( ( ) )
779
830
}
780
- ControlFlow :: Continue ( ( ) )
781
831
}
782
832
783
833
/// Returns the self type for the index trait call.
0 commit comments