@@ -65,20 +65,23 @@ fn add_typo_suggestion(
65
65
false
66
66
}
67
67
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
+ }
76
81
}
77
82
}
78
83
}
79
- }
80
84
81
- impl < ' a > Resolver < ' a > {
82
85
/// Handles error reporting for `smart_resolve_path_fragment` function.
83
86
/// Creates base error and amends it with one short label and possibly some longer helps/notes.
84
87
pub ( crate ) fn smart_resolve_report_errors (
@@ -593,10 +596,10 @@ impl<'a> Resolver<'a> {
593
596
Scope :: CrateRoot => {
594
597
let root_ident = Ident :: new ( kw:: PathRoot , ident. span ) ;
595
598
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) ;
597
600
}
598
601
Scope :: Module ( module) => {
599
- add_module_candidates ( module, & mut suggestions, filter_fn) ;
602
+ this . add_module_candidates ( module, & mut suggestions, filter_fn) ;
600
603
}
601
604
Scope :: MacroUsePrelude => {
602
605
suggestions. extend ( this. macro_use_prelude . iter ( ) . filter_map ( |( name, binding) | {
@@ -644,7 +647,7 @@ impl<'a> Resolver<'a> {
644
647
Scope :: StdLibPrelude => {
645
648
if let Some ( prelude) = this. prelude {
646
649
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) ;
648
651
suggestions. extend ( tmp_suggestions. into_iter ( ) . filter ( |s| {
649
652
use_prelude || this. is_builtin_macro ( s. res . opt_def_id ( ) )
650
653
} ) ) ;
@@ -694,7 +697,9 @@ impl<'a> Resolver<'a> {
694
697
if path. len ( ) == 1 {
695
698
// Search in lexical scope.
696
699
// 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 ( ) {
698
703
// Locals and type parameters
699
704
for ( ident, & res) in & rib. bindings {
700
705
if filter_fn ( res) {
@@ -704,7 +709,7 @@ impl<'a> Resolver<'a> {
704
709
// Items in scope
705
710
if let RibKind :: ModuleRibKind ( module) = rib. kind {
706
711
// Items from this module
707
- add_module_candidates ( module, & mut names, & filter_fn) ;
712
+ self . add_module_candidates ( module, & mut names, & filter_fn) ;
708
713
709
714
if let ModuleKind :: Block ( ..) = module. kind {
710
715
// We can see through blocks
@@ -732,7 +737,7 @@ impl<'a> Resolver<'a> {
732
737
} ) ) ;
733
738
734
739
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) ;
736
741
}
737
742
}
738
743
break ;
@@ -754,7 +759,7 @@ impl<'a> Resolver<'a> {
754
759
mod_path, Some ( TypeNS ) , false , span, CrateLint :: No
755
760
) {
756
761
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) ;
758
763
}
759
764
}
760
765
}
@@ -792,11 +797,9 @@ impl<'a> Resolver<'a> {
792
797
while let Some ( ( in_module,
793
798
path_segments,
794
799
in_module_is_extern) ) = worklist. pop ( ) {
795
- self . populate_module_if_necessary ( in_module) ;
796
-
797
800
// We have to visit module children in deterministic order to avoid
798
801
// 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| {
800
803
// avoid imports entirely
801
804
if name_binding. is_import ( ) && !name_binding. is_extern_crate ( ) { return ; }
802
805
// avoid non-importable candidates as well
@@ -830,7 +833,7 @@ impl<'a> Resolver<'a> {
830
833
// outside crate private modules => no need to check this)
831
834
if !in_module_is_extern || name_binding. vis == ty:: Visibility :: Public {
832
835
let did = match res {
833
- Res :: Def ( DefKind :: Ctor ( ..) , did) => self . parent ( did) ,
836
+ Res :: Def ( DefKind :: Ctor ( ..) , did) => this . parent ( did) ,
834
837
_ => res. opt_def_id ( ) ,
835
838
} ;
836
839
candidates. push ( ImportSuggestion { did, path } ) ;
@@ -890,8 +893,6 @@ impl<'a> Resolver<'a> {
890
893
krate : crate_id,
891
894
index : CRATE_DEF_INDEX ,
892
895
} ) ;
893
- self . populate_module_if_necessary ( & crate_root) ;
894
-
895
896
suggestions. extend ( self . lookup_import_candidates_from_module (
896
897
lookup_ident, namespace, crate_root, ident, & filter_fn) ) ;
897
898
}
@@ -910,9 +911,7 @@ impl<'a> Resolver<'a> {
910
911
// abort if the module is already found
911
912
if result. is_some ( ) { break ; }
912
913
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| {
916
915
// abort if the module is already found or if name_binding is private external
917
916
if result. is_some ( ) || !name_binding. vis . is_visible_locally ( ) {
918
917
return
@@ -943,10 +942,8 @@ impl<'a> Resolver<'a> {
943
942
944
943
fn collect_enum_variants ( & mut self , def_id : DefId ) -> Option < Vec < Path > > {
945
944
self . find_module ( def_id) . map ( |( enum_module, enum_import_suggestion) | {
946
- self . populate_module_if_necessary ( enum_module) ;
947
-
948
945
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| {
950
947
if let Res :: Def ( DefKind :: Variant , _) = name_binding. res ( ) {
951
948
let mut segms = enum_import_suggestion. path . segments . clone ( ) ;
952
949
segms. push ( ast:: PathSegment :: from_ident ( ident) ) ;
@@ -1147,7 +1144,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
1147
1144
/// at the root of the crate instead of the module where it is defined
1148
1145
/// ```
1149
1146
pub ( crate ) fn check_for_module_export_macro (
1150
- & self ,
1147
+ & mut self ,
1151
1148
directive : & ' b ImportDirective < ' b > ,
1152
1149
module : ModuleOrUniformRoot < ' b > ,
1153
1150
ident : Ident ,
@@ -1168,7 +1165,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
1168
1165
return None ;
1169
1166
}
1170
1167
1171
- let resolutions = crate_module . resolutions . borrow ( ) ;
1168
+ let resolutions = self . resolutions ( crate_module ) . borrow ( ) ;
1172
1169
let resolution = resolutions. get ( & ( ident, MacroNS ) ) ?;
1173
1170
let binding = resolution. borrow ( ) . binding ( ) ?;
1174
1171
if let Res :: Def ( DefKind :: Macro ( MacroKind :: Bang ) , _) = binding. res ( ) {
0 commit comments