@@ -158,24 +158,32 @@ impl<'a> SymbolCollector<'a> {
158
158
// Nested trees are very common, so a cache here will hit a lot.
159
159
let import_child_source_cache = & mut FxHashMap :: default ( ) ;
160
160
161
- let mut push_import = |this : & mut Self , i : ImportId , name : & Name , def : ModuleDefId | {
161
+ let is_explicit_import = |vis| match vis {
162
+ Visibility :: Public => true ,
163
+ Visibility :: Module ( _, VisibilityExplicitness :: Explicit ) => true ,
164
+ Visibility :: Module ( _, VisibilityExplicitness :: Implicit ) => false ,
165
+ } ;
166
+
167
+ let mut push_import = |this : & mut Self , i : ImportId , name : & Name , def : ModuleDefId , vis| {
162
168
let source = import_child_source_cache
163
169
. entry ( i. use_ )
164
170
. or_insert_with ( || i. use_ . child_source ( this. db . upcast ( ) ) ) ;
165
171
let Some ( use_tree_src) = source. value . get ( i. idx ) else { return } ;
166
- let Some ( name_ptr) = use_tree_src
167
- . rename ( )
168
- . and_then ( |rename| rename. name ( ) )
169
- . map ( Either :: Left )
170
- . or_else ( || use_tree_src. path ( ) ?. segment ( ) ?. name_ref ( ) . map ( Either :: Right ) )
171
- . map ( |it| AstPtr :: new ( & it) )
172
- else {
172
+ let rename = use_tree_src. rename ( ) . and_then ( |rename| rename. name ( ) ) ;
173
+ let name_syntax = match rename {
174
+ Some ( name) => Some ( Either :: Left ( name) ) ,
175
+ None if is_explicit_import ( vis) => {
176
+ ( || use_tree_src. path ( ) ?. segment ( ) ?. name_ref ( ) . map ( Either :: Right ) ) ( )
177
+ }
178
+ None => None ,
179
+ } ;
180
+ let Some ( name_syntax) = name_syntax else {
173
181
return ;
174
182
} ;
175
183
let dec_loc = DeclarationLocation {
176
184
hir_file_id : source. file_id ,
177
185
ptr : SyntaxNodePtr :: new ( use_tree_src. syntax ( ) ) ,
178
- name_ptr,
186
+ name_ptr : AstPtr :: new ( & name_syntax ) ,
179
187
} ;
180
188
this. symbols . insert ( FileSymbol {
181
189
name : name. symbol ( ) . clone ( ) ,
@@ -188,23 +196,23 @@ impl<'a> SymbolCollector<'a> {
188
196
} ;
189
197
190
198
let push_extern_crate =
191
- |this : & mut Self , i : ExternCrateId , name : & Name , def : ModuleDefId | {
199
+ |this : & mut Self , i : ExternCrateId , name : & Name , def : ModuleDefId , vis | {
192
200
let loc = i. lookup ( this. db . upcast ( ) ) ;
193
201
let source = loc. source ( this. db . upcast ( ) ) ;
194
- let Some ( name_ptr ) = source
195
- . value
196
- . rename ( )
197
- . and_then ( |rename| rename . name ( ) )
198
- . map ( Either :: Left )
199
- . or_else ( || source. value . name_ref ( ) . map ( Either :: Right ) )
200
- . map ( |it| AstPtr :: new ( & it ) )
201
- else {
202
+ let rename = source. value . rename ( ) . and_then ( |rename| rename . name ( ) ) ;
203
+
204
+ let name_syntax = match rename {
205
+ Some ( name) => Some ( Either :: Left ( name ) ) ,
206
+ None if is_explicit_import ( vis ) => None ,
207
+ None => source. value . name_ref ( ) . map ( Either :: Right ) ,
208
+ } ;
209
+ let Some ( name_syntax ) = name_syntax else {
202
210
return ;
203
211
} ;
204
212
let dec_loc = DeclarationLocation {
205
213
hir_file_id : source. file_id ,
206
214
ptr : SyntaxNodePtr :: new ( source. value . syntax ( ) ) ,
207
- name_ptr,
215
+ name_ptr : AstPtr :: new ( & name_syntax ) ,
208
216
} ;
209
217
this. symbols . insert ( FileSymbol {
210
218
name : name. symbol ( ) . clone ( ) ,
@@ -216,18 +224,6 @@ impl<'a> SymbolCollector<'a> {
216
224
} ) ;
217
225
} ;
218
226
219
- let is_explicit_import = |vis| {
220
- match vis {
221
- Visibility :: Module ( _, VisibilityExplicitness :: Explicit ) => true ,
222
- Visibility :: Module ( _, VisibilityExplicitness :: Implicit ) => {
223
- // consider imports in the crate root explicit, as these are visibly
224
- // crate-wide anyways
225
- module_id. is_crate_root ( )
226
- }
227
- Visibility :: Public => true ,
228
- }
229
- } ;
230
-
231
227
let def_map = module_id. def_map ( self . db . upcast ( ) ) ;
232
228
let scope = & def_map[ module_id. local_id ] . scope ;
233
229
@@ -237,15 +233,14 @@ impl<'a> SymbolCollector<'a> {
237
233
238
234
for ( name, Item { def, vis, import } ) in scope. types ( ) {
239
235
if let Some ( i) = import {
240
- if is_explicit_import ( vis) {
241
- match i {
242
- ImportOrExternCrate :: Import ( i) => push_import ( self , i, name, def) ,
243
- ImportOrExternCrate :: Glob ( _) => ( ) ,
244
- ImportOrExternCrate :: ExternCrate ( i) => {
245
- push_extern_crate ( self , i, name, def)
246
- }
236
+ match i {
237
+ ImportOrExternCrate :: Import ( i) => push_import ( self , i, name, def, vis) ,
238
+ ImportOrExternCrate :: Glob ( _) => ( ) ,
239
+ ImportOrExternCrate :: ExternCrate ( i) => {
240
+ push_extern_crate ( self , i, name, def, vis)
247
241
}
248
242
}
243
+
249
244
continue ;
250
245
}
251
246
// self is a declaration
@@ -254,11 +249,9 @@ impl<'a> SymbolCollector<'a> {
254
249
255
250
for ( name, Item { def, vis, import } ) in scope. macros ( ) {
256
251
if let Some ( i) = import {
257
- if is_explicit_import ( vis) {
258
- match i {
259
- ImportOrGlob :: Import ( i) => push_import ( self , i, name, def. into ( ) ) ,
260
- ImportOrGlob :: Glob ( _) => ( ) ,
261
- }
252
+ match i {
253
+ ImportOrGlob :: Import ( i) => push_import ( self , i, name, def. into ( ) , vis) ,
254
+ ImportOrGlob :: Glob ( _) => ( ) ,
262
255
}
263
256
continue ;
264
257
}
@@ -268,11 +261,9 @@ impl<'a> SymbolCollector<'a> {
268
261
269
262
for ( name, Item { def, vis, import } ) in scope. values ( ) {
270
263
if let Some ( i) = import {
271
- if is_explicit_import ( vis) {
272
- match i {
273
- ImportOrGlob :: Import ( i) => push_import ( self , i, name, def) ,
274
- ImportOrGlob :: Glob ( _) => ( ) ,
275
- }
264
+ match i {
265
+ ImportOrGlob :: Import ( i) => push_import ( self , i, name, def, vis) ,
266
+ ImportOrGlob :: Glob ( _) => ( ) ,
276
267
}
277
268
continue ;
278
269
}
0 commit comments