11//! Look up accessible paths for items.
22use hir:: {
33 AsAssocItem , AssocItem , AssocItemContainer , Crate , ItemInNs , MacroDef , ModPath , Module ,
4- ModuleDef , Name , PathResolution , PrefixKind , ScopeDef , Semantics , SemanticsScope , Type ,
4+ ModuleDef , Name , PathResolution , PrefixKind , ScopeDef , Semantics , Type ,
55} ;
66use itertools:: Itertools ;
77use rustc_hash:: FxHashSet ;
8- use syntax:: { ast, AstNode } ;
8+ use syntax:: { ast, AstNode , SyntaxNode } ;
99
1010use crate :: {
1111 items_locator:: { self , AssocItemSearch , DEFAULT_QUERY_SEARCH_LIMIT } ,
@@ -62,38 +62,37 @@ impl NameToImport {
6262}
6363
6464#[ derive( Debug ) ]
65- pub struct ImportAssets < ' a > {
65+ pub struct ImportAssets {
6666 import_candidate : ImportCandidate ,
67+ candidate_node : SyntaxNode ,
6768 module_with_candidate : Module ,
68- scope : SemanticsScope < ' a > ,
6969}
7070
71- impl < ' a > ImportAssets < ' a > {
71+ impl ImportAssets {
7272 pub fn for_method_call (
7373 method_call : & ast:: MethodCallExpr ,
74- sema : & ' a Semantics < RootDatabase > ,
74+ sema : & Semantics < RootDatabase > ,
7575 ) -> Option < Self > {
76- let scope = sema . scope ( method_call. syntax ( ) ) ;
76+ let candidate_node = method_call. syntax ( ) . clone ( ) ;
7777 Some ( Self {
7878 import_candidate : ImportCandidate :: for_method_call ( sema, method_call) ?,
79- module_with_candidate : scope. module ( ) ?,
80- scope ,
79+ module_with_candidate : sema . scope ( & candidate_node ) . module ( ) ?,
80+ candidate_node ,
8181 } )
8282 }
8383
8484 pub fn for_exact_path (
8585 fully_qualified_path : & ast:: Path ,
86- sema : & ' a Semantics < RootDatabase > ,
86+ sema : & Semantics < RootDatabase > ,
8787 ) -> Option < Self > {
88- let syntax_under_caret = fully_qualified_path. syntax ( ) ;
89- if syntax_under_caret . ancestors ( ) . find_map ( ast:: Use :: cast) . is_some ( ) {
88+ let candidate_node = fully_qualified_path. syntax ( ) . clone ( ) ;
89+ if candidate_node . ancestors ( ) . find_map ( ast:: Use :: cast) . is_some ( ) {
9090 return None ;
9191 }
92- let scope = sema. scope ( syntax_under_caret) ;
9392 Some ( Self {
9493 import_candidate : ImportCandidate :: for_regular_path ( sema, fully_qualified_path) ?,
95- module_with_candidate : scope. module ( ) ?,
96- scope ,
94+ module_with_candidate : sema . scope ( & candidate_node ) . module ( ) ?,
95+ candidate_node ,
9796 } )
9897 }
9998
@@ -102,28 +101,28 @@ impl<'a> ImportAssets<'a> {
102101 qualifier : Option < ast:: Path > ,
103102 fuzzy_name : String ,
104103 sema : & Semantics < RootDatabase > ,
105- scope : SemanticsScope < ' a > ,
104+ candidate_node : SyntaxNode ,
106105 ) -> Option < Self > {
107106 Some ( Self {
108107 import_candidate : ImportCandidate :: for_fuzzy_path ( qualifier, fuzzy_name, sema) ?,
109108 module_with_candidate,
110- scope ,
109+ candidate_node ,
111110 } )
112111 }
113112
114113 pub fn for_fuzzy_method_call (
115114 module_with_method_call : Module ,
116115 receiver_ty : Type ,
117116 fuzzy_method_name : String ,
118- scope : SemanticsScope < ' a > ,
117+ candidate_node : SyntaxNode ,
119118 ) -> Option < Self > {
120119 Some ( Self {
121120 import_candidate : ImportCandidate :: TraitMethod ( TraitImportCandidate {
122121 receiver_ty,
123122 name : NameToImport :: Fuzzy ( fuzzy_method_name) ,
124123 } ) ,
125124 module_with_candidate : module_with_method_call,
126- scope ,
125+ candidate_node ,
127126 } )
128127 }
129128}
@@ -156,7 +155,7 @@ impl LocatedImport {
156155 }
157156}
158157
159- impl < ' a > ImportAssets < ' a > {
158+ impl ImportAssets {
160159 pub fn import_candidate ( & self ) -> & ImportCandidate {
161160 & self . import_candidate
162161 }
@@ -182,7 +181,7 @@ impl<'a> ImportAssets<'a> {
182181 prefixed : Option < PrefixKind > ,
183182 ) -> Vec < LocatedImport > {
184183 let items_with_candidate_name = match self . name_to_import ( ) {
185- NameToImport :: Exact ( exact_name) => items_locator:: with_for_exact_name (
184+ NameToImport :: Exact ( exact_name) => items_locator:: with_exact_name (
186185 sema,
187186 self . module_with_candidate . krate ( ) ,
188187 exact_name. clone ( ) ,
@@ -209,7 +208,7 @@ impl<'a> ImportAssets<'a> {
209208 }
210209 } ;
211210
212- let scope_definitions = self . scope_definitions ( ) ;
211+ let scope_definitions = self . scope_definitions ( sema ) ;
213212 self . applicable_defs ( sema. db , prefixed, items_with_candidate_name)
214213 . into_iter ( )
215214 . filter ( |import| import. import_path . len ( ) > 1 )
@@ -218,9 +217,9 @@ impl<'a> ImportAssets<'a> {
218217 . collect ( )
219218 }
220219
221- fn scope_definitions ( & self ) -> FxHashSet < ScopeDef > {
220+ fn scope_definitions ( & self , sema : & Semantics < RootDatabase > ) -> FxHashSet < ScopeDef > {
222221 let mut scope_definitions = FxHashSet :: default ( ) ;
223- self . scope . process_all_names ( & mut |_, scope_def| {
222+ sema . scope ( & self . candidate_node ) . process_all_names ( & mut |_, scope_def| {
224223 scope_definitions. insert ( scope_def) ;
225224 } ) ;
226225 scope_definitions
0 commit comments