Skip to content

Commit f45b8ad

Browse files
authored
Merge pull request rust-lang#19026 from Veykril/push-wrxrutptywzx
Only collect implicit visibile use symbols if they have renames
2 parents 37fc7ee + 592ecee commit f45b8ad

File tree

2 files changed

+72
-48
lines changed

2 files changed

+72
-48
lines changed

src/tools/rust-analyzer/crates/hir/src/symbols.rs

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,32 @@ impl<'a> SymbolCollector<'a> {
158158
// Nested trees are very common, so a cache here will hit a lot.
159159
let import_child_source_cache = &mut FxHashMap::default();
160160

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| {
162168
let source = import_child_source_cache
163169
.entry(i.use_)
164170
.or_insert_with(|| i.use_.child_source(this.db.upcast()));
165171
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 {
173181
return;
174182
};
175183
let dec_loc = DeclarationLocation {
176184
hir_file_id: source.file_id,
177185
ptr: SyntaxNodePtr::new(use_tree_src.syntax()),
178-
name_ptr,
186+
name_ptr: AstPtr::new(&name_syntax),
179187
};
180188
this.symbols.insert(FileSymbol {
181189
name: name.symbol().clone(),
@@ -188,23 +196,23 @@ impl<'a> SymbolCollector<'a> {
188196
};
189197

190198
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| {
192200
let loc = i.lookup(this.db.upcast());
193201
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 {
202210
return;
203211
};
204212
let dec_loc = DeclarationLocation {
205213
hir_file_id: source.file_id,
206214
ptr: SyntaxNodePtr::new(source.value.syntax()),
207-
name_ptr,
215+
name_ptr: AstPtr::new(&name_syntax),
208216
};
209217
this.symbols.insert(FileSymbol {
210218
name: name.symbol().clone(),
@@ -216,18 +224,6 @@ impl<'a> SymbolCollector<'a> {
216224
});
217225
};
218226

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-
231227
let def_map = module_id.def_map(self.db.upcast());
232228
let scope = &def_map[module_id.local_id].scope;
233229

@@ -237,15 +233,14 @@ impl<'a> SymbolCollector<'a> {
237233

238234
for (name, Item { def, vis, import }) in scope.types() {
239235
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)
247241
}
248242
}
243+
249244
continue;
250245
}
251246
// self is a declaration
@@ -254,11 +249,9 @@ impl<'a> SymbolCollector<'a> {
254249

255250
for (name, Item { def, vis, import }) in scope.macros() {
256251
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(_) => (),
262255
}
263256
continue;
264257
}
@@ -268,11 +261,9 @@ impl<'a> SymbolCollector<'a> {
268261

269262
for (name, Item { def, vis, import }) in scope.values() {
270263
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(_) => (),
276267
}
277268
continue;
278269
}

src/tools/rust-analyzer/crates/ide-db/src/test_data/test_symbol_index_collection.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,39 @@
10071007
is_alias: false,
10081008
is_assoc: false,
10091009
},
1010+
FileSymbol {
1011+
name: "ThisStruct",
1012+
def: Adt(
1013+
Struct(
1014+
Struct {
1015+
id: StructId(
1016+
4,
1017+
),
1018+
},
1019+
),
1020+
),
1021+
loc: DeclarationLocation {
1022+
hir_file_id: EditionedFileId(
1023+
FileId(
1024+
1,
1025+
),
1026+
Edition2021,
1027+
),
1028+
ptr: SyntaxNodePtr {
1029+
kind: USE_TREE,
1030+
range: 85..125,
1031+
},
1032+
name_ptr: AstPtr(
1033+
SyntaxNodePtr {
1034+
kind: NAME,
1035+
range: 115..125,
1036+
},
1037+
),
1038+
},
1039+
container_name: None,
1040+
is_alias: false,
1041+
is_assoc: false,
1042+
},
10101043
],
10111044
),
10121045
]

0 commit comments

Comments
 (0)