@@ -522,6 +522,29 @@ impl<'a> ModuleData<'a> {
522522 }
523523 }
524524
525+ /// This modifies `self` in place. The traits will be stored in `self.traits`.
526+ fn ensure_traits < R > ( & ' a self , resolver : & mut R )
527+ where
528+ R : AsMut < Resolver < ' a > > ,
529+ {
530+ let mut traits = self . traits . borrow_mut ( ) ;
531+ if traits. is_none ( ) {
532+ let mut collected_traits = Vec :: new ( ) ;
533+ self . for_each_child ( resolver, |_, name, ns, binding| {
534+ if ns != TypeNS {
535+ return ;
536+ }
537+ match binding. res ( ) {
538+ Res :: Def ( DefKind :: Trait | DefKind :: TraitAlias , _) => {
539+ collected_traits. push ( ( name, binding) )
540+ }
541+ _ => ( ) ,
542+ }
543+ } ) ;
544+ * traits = Some ( collected_traits. into_boxed_slice ( ) ) ;
545+ }
546+ }
547+
525548 fn res ( & self ) -> Option < Res > {
526549 match self . kind {
527550 ModuleKind :: Def ( kind, def_id, _) => Some ( Res :: Def ( kind, def_id) ) ,
@@ -1440,22 +1463,8 @@ impl<'a> Resolver<'a> {
14401463 parent_scope : & ParentScope < ' a > ,
14411464 ) {
14421465 assert ! ( ns == TypeNS || ns == ValueNS ) ;
1443- let mut traits = module. traits . borrow_mut ( ) ;
1444- if traits. is_none ( ) {
1445- let mut collected_traits = Vec :: new ( ) ;
1446- module. for_each_child ( self , |_, name, ns, binding| {
1447- if ns != TypeNS {
1448- return ;
1449- }
1450- match binding. res ( ) {
1451- Res :: Def ( DefKind :: Trait | DefKind :: TraitAlias , _) => {
1452- collected_traits. push ( ( name, binding) )
1453- }
1454- _ => ( ) ,
1455- }
1456- } ) ;
1457- * traits = Some ( collected_traits. into_boxed_slice ( ) ) ;
1458- }
1466+ module. ensure_traits ( self ) ;
1467+ let traits = module. traits . borrow ( ) ;
14591468
14601469 for & ( trait_name, binding) in traits. as_ref ( ) . unwrap ( ) . iter ( ) {
14611470 // Traits have pseudo-modules that can be used to search for the given ident.
0 commit comments