@@ -65,20 +65,23 @@ fn add_typo_suggestion(
6565 false
6666}
6767
68- fn add_module_candidates (
69- module : Module < ' _ > , names : & mut Vec < TypoSuggestion > , filter_fn : & impl Fn ( Res ) -> bool
70- ) {
71- for ( & ( ident, _) , resolution) in module. resolutions . borrow ( ) . iter ( ) {
72- if let Some ( binding) = resolution. borrow ( ) . binding {
73- let res = binding. res ( ) ;
74- if filter_fn ( res) {
75- names. push ( TypoSuggestion :: from_res ( ident. name , res) ) ;
68+ impl < ' a > Resolver < ' a > {
69+ fn add_module_candidates (
70+ & mut self ,
71+ module : Module < ' a > ,
72+ names : & mut Vec < TypoSuggestion > ,
73+ filter_fn : & impl Fn ( Res ) -> bool ,
74+ ) {
75+ for ( & ( ident, _) , resolution) in self . resolutions ( module) . borrow ( ) . iter ( ) {
76+ if let Some ( binding) = resolution. borrow ( ) . binding {
77+ let res = binding. res ( ) ;
78+ if filter_fn ( res) {
79+ names. push ( TypoSuggestion :: from_res ( ident. name , res) ) ;
80+ }
7681 }
7782 }
7883 }
79- }
8084
81- impl < ' a > Resolver < ' a > {
8285 /// Handles error reporting for `smart_resolve_path_fragment` function.
8386 /// Creates base error and amends it with one short label and possibly some longer helps/notes.
8487 pub ( crate ) fn smart_resolve_report_errors (
@@ -593,10 +596,10 @@ impl<'a> Resolver<'a> {
593596 Scope :: CrateRoot => {
594597 let root_ident = Ident :: new ( kw:: PathRoot , ident. span ) ;
595598 let root_module = this. resolve_crate_root ( root_ident) ;
596- add_module_candidates ( root_module, & mut suggestions, filter_fn) ;
599+ this . add_module_candidates ( root_module, & mut suggestions, filter_fn) ;
597600 }
598601 Scope :: Module ( module) => {
599- add_module_candidates ( module, & mut suggestions, filter_fn) ;
602+ this . add_module_candidates ( module, & mut suggestions, filter_fn) ;
600603 }
601604 Scope :: MacroUsePrelude => {
602605 suggestions. extend ( this. macro_use_prelude . iter ( ) . filter_map ( |( name, binding) | {
@@ -644,7 +647,7 @@ impl<'a> Resolver<'a> {
644647 Scope :: StdLibPrelude => {
645648 if let Some ( prelude) = this. prelude {
646649 let mut tmp_suggestions = Vec :: new ( ) ;
647- add_module_candidates ( prelude, & mut tmp_suggestions, filter_fn) ;
650+ this . add_module_candidates ( prelude, & mut tmp_suggestions, filter_fn) ;
648651 suggestions. extend ( tmp_suggestions. into_iter ( ) . filter ( |s| {
649652 use_prelude || this. is_builtin_macro ( s. res . opt_def_id ( ) )
650653 } ) ) ;
@@ -694,7 +697,9 @@ impl<'a> Resolver<'a> {
694697 if path. len ( ) == 1 {
695698 // Search in lexical scope.
696699 // Walk backwards up the ribs in scope and collect candidates.
697- for rib in self . ribs [ ns] . iter ( ) . rev ( ) {
700+ // Ribs have to be cloned to avoid borrowing the resolver.
701+ let ribs = self . ribs [ ns] . clone ( ) ;
702+ for rib in ribs. iter ( ) . rev ( ) {
698703 // Locals and type parameters
699704 for ( ident, & res) in & rib. bindings {
700705 if filter_fn ( res) {
@@ -704,7 +709,7 @@ impl<'a> Resolver<'a> {
704709 // Items in scope
705710 if let RibKind :: ModuleRibKind ( module) = rib. kind {
706711 // Items from this module
707- add_module_candidates ( module, & mut names, & filter_fn) ;
712+ self . add_module_candidates ( module, & mut names, & filter_fn) ;
708713
709714 if let ModuleKind :: Block ( ..) = module. kind {
710715 // We can see through blocks
@@ -732,7 +737,7 @@ impl<'a> Resolver<'a> {
732737 } ) ) ;
733738
734739 if let Some ( prelude) = self . prelude {
735- add_module_candidates ( prelude, & mut names, & filter_fn) ;
740+ self . add_module_candidates ( prelude, & mut names, & filter_fn) ;
736741 }
737742 }
738743 break ;
@@ -754,7 +759,7 @@ impl<'a> Resolver<'a> {
754759 mod_path, Some ( TypeNS ) , false , span, CrateLint :: No
755760 ) {
756761 if let ModuleOrUniformRoot :: Module ( module) = module {
757- add_module_candidates ( module, & mut names, & filter_fn) ;
762+ self . add_module_candidates ( module, & mut names, & filter_fn) ;
758763 }
759764 }
760765 }
@@ -792,11 +797,9 @@ impl<'a> Resolver<'a> {
792797 while let Some ( ( in_module,
793798 path_segments,
794799 in_module_is_extern) ) = worklist. pop ( ) {
795- self . populate_module_if_necessary ( in_module) ;
796-
797800 // We have to visit module children in deterministic order to avoid
798801 // instabilities in reported imports (#43552).
799- in_module . for_each_child_stable ( | ident, ns, name_binding| {
802+ self . for_each_child_stable ( in_module , |this , ident, ns, name_binding| {
800803 // avoid imports entirely
801804 if name_binding. is_import ( ) && !name_binding. is_extern_crate ( ) { return ; }
802805 // avoid non-importable candidates as well
@@ -830,7 +833,7 @@ impl<'a> Resolver<'a> {
830833 // outside crate private modules => no need to check this)
831834 if !in_module_is_extern || name_binding. vis == ty:: Visibility :: Public {
832835 let did = match res {
833- Res :: Def ( DefKind :: Ctor ( ..) , did) => self . parent ( did) ,
836+ Res :: Def ( DefKind :: Ctor ( ..) , did) => this . parent ( did) ,
834837 _ => res. opt_def_id ( ) ,
835838 } ;
836839 candidates. push ( ImportSuggestion { did, path } ) ;
@@ -890,8 +893,6 @@ impl<'a> Resolver<'a> {
890893 krate : crate_id,
891894 index : CRATE_DEF_INDEX ,
892895 } ) ;
893- self . populate_module_if_necessary ( & crate_root) ;
894-
895896 suggestions. extend ( self . lookup_import_candidates_from_module (
896897 lookup_ident, namespace, crate_root, ident, & filter_fn) ) ;
897898 }
@@ -910,9 +911,7 @@ impl<'a> Resolver<'a> {
910911 // abort if the module is already found
911912 if result. is_some ( ) { break ; }
912913
913- self . populate_module_if_necessary ( in_module) ;
914-
915- in_module. for_each_child_stable ( |ident, _, name_binding| {
914+ self . for_each_child_stable ( in_module, |_, ident, _, name_binding| {
916915 // abort if the module is already found or if name_binding is private external
917916 if result. is_some ( ) || !name_binding. vis . is_visible_locally ( ) {
918917 return
@@ -943,10 +942,8 @@ impl<'a> Resolver<'a> {
943942
944943 fn collect_enum_variants ( & mut self , def_id : DefId ) -> Option < Vec < Path > > {
945944 self . find_module ( def_id) . map ( |( enum_module, enum_import_suggestion) | {
946- self . populate_module_if_necessary ( enum_module) ;
947-
948945 let mut variants = Vec :: new ( ) ;
949- enum_module . for_each_child_stable ( | ident, _, name_binding| {
946+ self . for_each_child_stable ( enum_module , |_ , ident, _, name_binding| {
950947 if let Res :: Def ( DefKind :: Variant , _) = name_binding. res ( ) {
951948 let mut segms = enum_import_suggestion. path . segments . clone ( ) ;
952949 segms. push ( ast:: PathSegment :: from_ident ( ident) ) ;
@@ -1147,7 +1144,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
11471144 /// at the root of the crate instead of the module where it is defined
11481145 /// ```
11491146 pub ( crate ) fn check_for_module_export_macro (
1150- & self ,
1147+ & mut self ,
11511148 directive : & ' b ImportDirective < ' b > ,
11521149 module : ModuleOrUniformRoot < ' b > ,
11531150 ident : Ident ,
@@ -1168,7 +1165,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
11681165 return None ;
11691166 }
11701167
1171- let resolutions = crate_module . resolutions . borrow ( ) ;
1168+ let resolutions = self . resolutions ( crate_module ) . borrow ( ) ;
11721169 let resolution = resolutions. get ( & ( ident, MacroNS ) ) ?;
11731170 let binding = resolution. borrow ( ) . binding ( ) ?;
11741171 if let Res :: Def ( DefKind :: Macro ( MacroKind :: Bang ) , _) = binding. res ( ) {
0 commit comments