@@ -850,6 +850,9 @@ pub struct ModuleS<'a> {
850
850
glob_importers : RefCell < Vec < ( Module < ' a > , & ' a ImportDirective < ' a > ) > > ,
851
851
globs : RefCell < Vec < & ' a ImportDirective < ' a > > > ,
852
852
853
+ // Used to memoize the traits in this module for faster searches through all traits in scope.
854
+ traits : RefCell < Option < Box < [ & ' a NameBinding < ' a > ] > > > ,
855
+
853
856
// Whether this module is populated. If not populated, any attempt to
854
857
// access the children must be preceded with a
855
858
// `populate_module_if_necessary` call.
@@ -877,6 +880,7 @@ impl<'a> ModuleS<'a> {
877
880
prelude : RefCell :: new ( None ) ,
878
881
glob_importers : RefCell :: new ( Vec :: new ( ) ) ,
879
882
globs : RefCell :: new ( ( Vec :: new ( ) ) ) ,
883
+ traits : RefCell :: new ( None ) ,
880
884
populated : Cell :: new ( !external) ,
881
885
arenas : arenas
882
886
}
@@ -3233,18 +3237,28 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
3233
3237
let mut search_module = self . current_module ;
3234
3238
loop {
3235
3239
// Look for trait children.
3236
- let mut search_in_module = |module : Module < ' a > | module. for_each_child ( |_, ns, binding| {
3237
- if ns != TypeNS { return }
3238
- let trait_def_id = match binding. def ( ) {
3239
- Some ( Def :: Trait ( trait_def_id) ) => trait_def_id,
3240
- Some ( ..) | None => return ,
3241
- } ;
3242
- if self . trait_item_map . contains_key ( & ( name, trait_def_id) ) {
3243
- add_trait_info ( & mut found_traits, trait_def_id, name) ;
3244
- let trait_name = self . get_trait_name ( trait_def_id) ;
3245
- self . record_use ( trait_name, TypeNS , binding) ;
3240
+ let mut search_in_module = |module : Module < ' a > | {
3241
+ let mut traits = module. traits . borrow_mut ( ) ;
3242
+ if traits. is_none ( ) {
3243
+ let mut collected_traits = Vec :: new ( ) ;
3244
+ module. for_each_child ( |_, ns, binding| {
3245
+ if ns != TypeNS { return }
3246
+ if let Some ( Def :: Trait ( _) ) = binding. def ( ) {
3247
+ collected_traits. push ( binding) ;
3248
+ }
3249
+ } ) ;
3250
+ * traits = Some ( collected_traits. into_boxed_slice ( ) ) ;
3246
3251
}
3247
- } ) ;
3252
+
3253
+ for binding in traits. as_ref ( ) . unwrap ( ) . iter ( ) {
3254
+ let trait_def_id = binding. def ( ) . unwrap ( ) . def_id ( ) ;
3255
+ if self . trait_item_map . contains_key ( & ( name, trait_def_id) ) {
3256
+ add_trait_info ( & mut found_traits, trait_def_id, name) ;
3257
+ let trait_name = self . get_trait_name ( trait_def_id) ;
3258
+ self . record_use ( trait_name, TypeNS , binding) ;
3259
+ }
3260
+ }
3261
+ } ;
3248
3262
search_in_module ( search_module) ;
3249
3263
3250
3264
match search_module. parent_link {
0 commit comments