1 //! Implementation of find-usages functionality.
3 //! It is based on the standard ide trick: first, we run a fast text search to
4 //! get a super-set of matches. Then, we we confirm each match using precise
7 use std::{convert::TryInto, mem, sync::Arc};
9 use base_db::{FileId, FileRange, SourceDatabase, SourceDatabaseExt};
11 AsAssocItem, DefWithBody, HasAttrs, HasSource, InFile, ModuleSource, Semantics, Visibility,
13 use once_cell::unsync::Lazy;
14 use rustc_hash::FxHashMap;
15 use syntax::{ast, match_ast, AstNode, TextRange, TextSize};
18 defs::{Definition, NameClass, NameRefClass},
22 #[derive(Debug, Default, Clone)]
23 pub struct UsageSearchResult {
24 pub references: FxHashMap<FileId, Vec<FileReference>>,
27 impl UsageSearchResult {
28 pub fn is_empty(&self) -> bool {
29 self.references.is_empty()
32 pub fn len(&self) -> usize {
36 pub fn iter(&self) -> impl Iterator<Item = (&FileId, &[FileReference])> + '_ {
37 self.references.iter().map(|(file_id, refs)| (file_id, &**refs))
40 pub fn file_ranges(&self) -> impl Iterator<Item = FileRange> + '_ {
41 self.references.iter().flat_map(|(&file_id, refs)| {
42 refs.iter().map(move |&FileReference { range, .. }| FileRange { file_id, range })
47 impl IntoIterator for UsageSearchResult {
48 type Item = (FileId, Vec<FileReference>);
49 type IntoIter = <FxHashMap<FileId, Vec<FileReference>> as IntoIterator>::IntoIter;
51 fn into_iter(self) -> Self::IntoIter {
52 self.references.into_iter()
56 #[derive(Debug, Clone)]
57 pub struct FileReference {
59 pub name: ast::NameLike,
60 pub category: Option<ReferenceCategory>,
63 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
64 pub enum ReferenceCategory {
65 // FIXME: Add this variant and delete the `retain_adt_literal_usages` function.
69 // FIXME: Some day should be able to search in doc comments. Would probably
70 // need to switch from enum to bitflags then?
74 /// Generally, `search_scope` returns files that might contain references for the element.
75 /// For `pub(crate)` things it's a crate, for `pub` things it's a crate and dependant crates.
76 /// In some cases, the location of the references is known to within a `TextRange`,
77 /// e.g. for things like local variables.
78 #[derive(Clone, Debug)]
79 pub struct SearchScope {
80 entries: FxHashMap<FileId, Option<TextRange>>,
84 fn new(entries: FxHashMap<FileId, Option<TextRange>>) -> SearchScope {
85 SearchScope { entries }
88 /// Build a search scope spanning the entire crate graph of files.
89 fn crate_graph(db: &RootDatabase) -> SearchScope {
90 let mut entries = FxHashMap::default();
92 let graph = db.crate_graph();
93 for krate in graph.iter() {
94 let root_file = graph[krate].root_file_id;
95 let source_root_id = db.file_source_root(root_file);
96 let source_root = db.source_root(source_root_id);
97 entries.extend(source_root.iter().map(|id| (id, None)));
99 SearchScope { entries }
102 /// Build a search scope spanning all the reverse dependencies of the given crate.
103 fn reverse_dependencies(db: &RootDatabase, of: hir::Crate) -> SearchScope {
104 let mut entries = FxHashMap::default();
105 for rev_dep in of.transitive_reverse_dependencies(db) {
106 let root_file = rev_dep.root_file(db);
107 let source_root_id = db.file_source_root(root_file);
108 let source_root = db.source_root(source_root_id);
109 entries.extend(source_root.iter().map(|id| (id, None)));
111 SearchScope { entries }
114 /// Build a search scope spanning the given crate.
115 fn krate(db: &RootDatabase, of: hir::Crate) -> SearchScope {
116 let root_file = of.root_file(db);
117 let source_root_id = db.file_source_root(root_file);
118 let source_root = db.source_root(source_root_id);
120 entries: source_root.iter().map(|id| (id, None)).collect::<FxHashMap<_, _>>(),
124 /// Build a search scope spanning the given module and all its submodules.
125 fn module_and_children(db: &RootDatabase, module: hir::Module) -> SearchScope {
126 let mut entries = FxHashMap::default();
128 let (file_id, range) = {
129 let InFile { file_id, value } = module.definition_source(db);
130 if let Some((file_id, call_source)) = file_id.original_call_node(db) {
131 (file_id, Some(call_source.text_range()))
134 file_id.original_file(db),
136 ModuleSource::SourceFile(_) => None,
137 ModuleSource::Module(it) => Some(it.syntax().text_range()),
138 ModuleSource::BlockExpr(it) => Some(it.syntax().text_range()),
143 entries.insert(file_id, range);
145 let mut to_visit: Vec<_> = module.children(db).collect();
146 while let Some(module) = to_visit.pop() {
147 if let InFile { file_id, value: ModuleSource::SourceFile(_) } =
148 module.definition_source(db)
150 entries.insert(file_id.original_file(db), None);
152 to_visit.extend(module.children(db));
154 SearchScope { entries }
157 /// Build an empty search scope.
158 pub fn empty() -> SearchScope {
159 SearchScope::new(FxHashMap::default())
162 /// Build a empty search scope spanning the given file.
163 pub fn single_file(file: FileId) -> SearchScope {
164 SearchScope::new(std::iter::once((file, None)).collect())
167 /// Build a empty search scope spanning the text range of the given file.
168 pub fn file_range(range: FileRange) -> SearchScope {
169 SearchScope::new(std::iter::once((range.file_id, Some(range.range))).collect())
172 /// Build a empty search scope spanning the given files.
173 pub fn files(files: &[FileId]) -> SearchScope {
174 SearchScope::new(files.iter().map(|f| (*f, None)).collect())
177 pub fn intersection(&self, other: &SearchScope) -> SearchScope {
178 let (mut small, mut large) = (&self.entries, &other.entries);
179 if small.len() > large.len() {
180 mem::swap(&mut small, &mut large)
183 let intersect_ranges =
184 |r1: Option<TextRange>, r2: Option<TextRange>| -> Option<Option<TextRange>> {
186 (None, r) | (r, None) => Some(r),
187 (Some(r1), Some(r2)) => r1.intersect(r2).map(Some),
192 .filter_map(|(&file_id, &r1)| {
193 let &r2 = large.get(&file_id)?;
194 let r = intersect_ranges(r1, r2)?;
199 SearchScope::new(res)
203 impl IntoIterator for SearchScope {
204 type Item = (FileId, Option<TextRange>);
205 type IntoIter = std::collections::hash_map::IntoIter<FileId, Option<TextRange>>;
207 fn into_iter(self) -> Self::IntoIter {
208 self.entries.into_iter()
213 fn search_scope(&self, db: &RootDatabase) -> SearchScope {
214 let _p = profile::span("search_scope");
216 if let Definition::BuiltinType(_) = self {
217 return SearchScope::crate_graph(db);
221 // FIXME: We don't do searches for crates currently, as a crate does not actually have a single name
222 if let &Definition::Module(module) = self {
223 if module.is_crate_root(db) {
224 return SearchScope::reverse_dependencies(db, module.krate());
228 let module = match self.module(db) {
230 None => return SearchScope::empty(),
232 let InFile { file_id, value: module_source } = module.definition_source(db);
233 let file_id = file_id.original_file(db);
235 if let Definition::Local(var) = self {
236 let def = match var.parent(db) {
237 DefWithBody::Function(f) => f.source(db).map(|src| src.syntax().cloned()),
238 DefWithBody::Const(c) => c.source(db).map(|src| src.syntax().cloned()),
239 DefWithBody::Static(s) => s.source(db).map(|src| src.syntax().cloned()),
242 Some(def) => SearchScope::file_range(def.as_ref().original_file_range(db)),
243 None => SearchScope::single_file(file_id),
247 if let Definition::SelfType(impl_) = self {
248 return match impl_.source(db).map(|src| src.syntax().cloned()) {
249 Some(def) => SearchScope::file_range(def.as_ref().original_file_range(db)),
250 None => SearchScope::single_file(file_id),
254 if let Definition::GenericParam(hir::GenericParam::LifetimeParam(param)) = self {
255 let def = match param.parent(db) {
256 hir::GenericDef::Function(it) => it.source(db).map(|src| src.syntax().cloned()),
257 hir::GenericDef::Adt(it) => it.source(db).map(|src| src.syntax().cloned()),
258 hir::GenericDef::Trait(it) => it.source(db).map(|src| src.syntax().cloned()),
259 hir::GenericDef::TypeAlias(it) => it.source(db).map(|src| src.syntax().cloned()),
260 hir::GenericDef::Impl(it) => it.source(db).map(|src| src.syntax().cloned()),
261 hir::GenericDef::Variant(it) => it.source(db).map(|src| src.syntax().cloned()),
262 hir::GenericDef::Const(it) => it.source(db).map(|src| src.syntax().cloned()),
265 Some(def) => SearchScope::file_range(def.as_ref().original_file_range(db)),
266 None => SearchScope::single_file(file_id),
270 if let Definition::Macro(macro_def) = self {
271 return match macro_def.kind(db) {
272 hir::MacroKind::Declarative => {
273 if macro_def.attrs(db).by_key("macro_export").exists() {
274 SearchScope::reverse_dependencies(db, module.krate())
276 SearchScope::krate(db, module.krate())
279 hir::MacroKind::BuiltIn => SearchScope::crate_graph(db),
280 // FIXME: We don't actually see derives in derive attributes as these do not
281 // expand to something that references the derive macro in the output.
282 // We could get around this by doing pseudo expansions for proc_macro_derive like we
283 // do for the derive attribute
284 hir::MacroKind::Derive | hir::MacroKind::Attr | hir::MacroKind::ProcMacro => {
285 SearchScope::reverse_dependencies(db, module.krate())
290 let vis = self.visibility(db);
291 if let Some(Visibility::Public) = vis {
292 return SearchScope::reverse_dependencies(db, module.krate());
294 if let Some(Visibility::Module(module)) = vis {
295 return SearchScope::module_and_children(db, module.into());
298 let range = match module_source {
299 ModuleSource::Module(m) => Some(m.syntax().text_range()),
300 ModuleSource::BlockExpr(b) => Some(b.syntax().text_range()),
301 ModuleSource::SourceFile(_) => None,
304 Some(range) => SearchScope::file_range(FileRange { file_id, range }),
305 None => SearchScope::single_file(file_id),
309 pub fn usages<'a>(self, sema: &'a Semantics<RootDatabase>) -> FindUsages<'a> {
311 local_repr: match self {
312 Definition::Local(local) => Some(local.representative(sema.db)),
318 include_self_kw_refs: None,
319 search_self_mod: false,
325 pub struct FindUsages<'a> {
327 sema: &'a Semantics<'a, RootDatabase>,
328 scope: Option<SearchScope>,
329 include_self_kw_refs: Option<hir::Type>,
330 local_repr: Option<hir::Local>,
331 search_self_mod: bool,
334 impl<'a> FindUsages<'a> {
335 /// Enable searching for `Self` when the definition is a type or `self` for modules.
336 pub fn include_self_refs(mut self) -> FindUsages<'a> {
337 self.include_self_kw_refs = def_to_ty(self.sema, &self.def);
338 self.search_self_mod = true;
342 /// Limit the search to a given [`SearchScope`].
343 pub fn in_scope(self, scope: SearchScope) -> FindUsages<'a> {
344 self.set_scope(Some(scope))
347 /// Limit the search to a given [`SearchScope`].
348 pub fn set_scope(mut self, scope: Option<SearchScope>) -> FindUsages<'a> {
349 assert!(self.scope.is_none());
354 pub fn at_least_one(&self) -> bool {
355 let mut found = false;
356 self.search(&mut |_, _| {
363 pub fn all(self) -> UsageSearchResult {
364 let mut res = UsageSearchResult::default();
365 self.search(&mut |file_id, reference| {
366 res.references.entry(file_id).or_default().push(reference);
372 fn search(&self, sink: &mut dyn FnMut(FileId, FileReference) -> bool) {
373 let _p = profile::span("FindUsages:search");
374 let sema = self.sema;
377 let base = self.def.search_scope(sema.db);
380 Some(scope) => base.intersection(scope),
384 let name = match self.def {
385 // special case crate modules as these do not have a proper name
386 Definition::Module(module) if module.is_crate_root(self.sema.db) => {
387 // FIXME: This assumes the crate name is always equal to its display name when it really isn't
390 .display_name(self.sema.db)
391 .map(|crate_name| crate_name.crate_name().as_smol_str().clone())
394 let self_kw_refs = || {
395 self.include_self_kw_refs.as_ref().and_then(|ty| {
397 .map(|adt| adt.name(self.sema.db))
398 .or_else(|| ty.as_builtin().map(|builtin| builtin.name()))
401 self.def.name(sema.db).or_else(self_kw_refs).map(|it| it.to_smol_str())
404 let name = match &name {
405 Some(s) => s.as_str(),
409 // these can't be closures because rust infers the lifetimes wrong ...
410 fn match_indices<'a>(
413 search_range: TextRange,
414 ) -> impl Iterator<Item = TextSize> + 'a {
415 text.match_indices(name).filter_map(move |(idx, _)| {
416 let offset: TextSize = idx.try_into().unwrap();
417 if !search_range.contains_inclusive(offset) {
425 sema: &'a Semantics<RootDatabase>,
426 scope: &'a SearchScope,
427 ) -> impl Iterator<Item = (Arc<String>, FileId, TextRange)> + 'a {
428 scope.entries.iter().map(|(&file_id, &search_range)| {
429 let text = sema.db.file_text(file_id);
431 search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(text.as_str())));
433 (text, file_id, search_range)
437 // FIXME: There should be optimization potential here
438 // Currently we try to descend everything we find which
439 // means we call `Semantics::descend_into_macros` on
440 // every textual hit. That function is notoriously
441 // expensive even for things that do not get down mapped
443 for (text, file_id, search_range) in scope_files(sema, &search_scope) {
444 let tree = Lazy::new(move || sema.parse(file_id).syntax().clone());
446 // Search for occurrences of the items name
447 for offset in match_indices(&text, name, search_range) {
448 for name in sema.find_nodes_at_offset_with_descend(&tree, offset) {
450 ast::NameLike::NameRef(name_ref) => self.found_name_ref(&name_ref, sink),
451 ast::NameLike::Name(name) => self.found_name(&name, sink),
452 ast::NameLike::Lifetime(lifetime) => self.found_lifetime(&lifetime, sink),
458 // Search for occurrences of the `Self` referring to our type
459 if let Some(self_ty) = &self.include_self_kw_refs {
460 for offset in match_indices(&text, "Self", search_range) {
461 for name_ref in sema.find_nodes_at_offset_with_descend(&tree, offset) {
462 if self.found_self_ty_name_ref(self_ty, &name_ref, sink) {
470 // Search for `super` and `crate` resolving to our module
472 Definition::Module(module) => {
473 let scope = search_scope
474 .intersection(&SearchScope::module_and_children(self.sema.db, module));
476 let is_crate_root = module.is_crate_root(self.sema.db);
478 for (text, file_id, search_range) in scope_files(sema, &scope) {
479 let tree = Lazy::new(move || sema.parse(file_id).syntax().clone());
481 for offset in match_indices(&text, "super", search_range) {
482 for name_ref in sema.find_nodes_at_offset_with_descend(&tree, offset) {
483 if self.found_name_ref(&name_ref, sink) {
489 for offset in match_indices(&text, "crate", search_range) {
490 for name_ref in sema.find_nodes_at_offset_with_descend(&tree, offset) {
491 if self.found_name_ref(&name_ref, sink) {
502 // search for module `self` references in our module's definition source
504 Definition::Module(module) if self.search_self_mod => {
505 let src = module.definition_source(sema.db);
506 let file_id = src.file_id.original_file(sema.db);
507 let (file_id, search_range) = match src.value {
508 ModuleSource::Module(m) => (file_id, Some(m.syntax().text_range())),
509 ModuleSource::BlockExpr(b) => (file_id, Some(b.syntax().text_range())),
510 ModuleSource::SourceFile(_) => (file_id, None),
513 let search_range = if let Some(&range) = search_scope.entries.get(&file_id) {
514 match (range, search_range) {
515 (None, range) | (range, None) => range,
516 (Some(range), Some(search_range)) => match range.intersect(search_range) {
517 Some(range) => Some(range),
525 let text = sema.db.file_text(file_id);
527 search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(text.as_str())));
529 let tree = Lazy::new(|| sema.parse(file_id).syntax().clone());
531 for offset in match_indices(&text, "self", search_range) {
532 for name_ref in sema.find_nodes_at_offset_with_descend(&tree, offset) {
533 if self.found_self_module_name_ref(&name_ref, sink) {
543 fn found_self_ty_name_ref(
546 name_ref: &ast::NameRef,
547 sink: &mut dyn FnMut(FileId, FileReference) -> bool,
549 match NameRefClass::classify(self.sema, name_ref) {
550 Some(NameRefClass::Definition(Definition::SelfType(impl_)))
551 if impl_.self_ty(self.sema.db) == *self_ty =>
553 let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
554 let reference = FileReference {
556 name: ast::NameLike::NameRef(name_ref.clone()),
559 sink(file_id, reference)
565 fn found_self_module_name_ref(
567 name_ref: &ast::NameRef,
568 sink: &mut dyn FnMut(FileId, FileReference) -> bool,
570 match NameRefClass::classify(self.sema, name_ref) {
571 Some(NameRefClass::Definition(def @ Definition::Module(_))) if def == self.def => {
572 let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
573 let reference = FileReference {
575 name: ast::NameLike::NameRef(name_ref.clone()),
578 sink(file_id, reference)
586 lifetime: &ast::Lifetime,
587 sink: &mut dyn FnMut(FileId, FileReference) -> bool,
589 match NameRefClass::classify_lifetime(self.sema, lifetime) {
590 Some(NameRefClass::Definition(def)) if def == self.def => {
591 let FileRange { file_id, range } = self.sema.original_range(lifetime.syntax());
592 let reference = FileReference {
594 name: ast::NameLike::Lifetime(lifetime.clone()),
597 sink(file_id, reference)
605 name_ref: &ast::NameRef,
606 sink: &mut dyn FnMut(FileId, FileReference) -> bool,
608 match NameRefClass::classify(self.sema, name_ref) {
609 Some(NameRefClass::Definition(def @ Definition::Local(local)))
611 self.local_repr, Some(repr) if repr == local.representative(self.sema.db)
614 let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
615 let reference = FileReference {
617 name: ast::NameLike::NameRef(name_ref.clone()),
618 category: ReferenceCategory::new(&def, name_ref),
620 sink(file_id, reference)
622 Some(NameRefClass::Definition(def)) if def == self.def => {
623 let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
624 let reference = FileReference {
626 name: ast::NameLike::NameRef(name_ref.clone()),
627 category: ReferenceCategory::new(&def, name_ref),
629 sink(file_id, reference)
631 Some(NameRefClass::Definition(def)) if self.include_self_kw_refs.is_some() => {
632 if self.include_self_kw_refs == def_to_ty(self.sema, &def) {
633 let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
634 let reference = FileReference {
636 name: ast::NameLike::NameRef(name_ref.clone()),
637 category: ReferenceCategory::new(&def, name_ref),
639 sink(file_id, reference)
644 Some(NameRefClass::FieldShorthand { local_ref: local, field_ref: field }) => {
645 let field = Definition::Field(field);
646 let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
647 let access = match self.def {
648 Definition::Field(_) if field == self.def => {
649 ReferenceCategory::new(&field, name_ref)
651 Definition::Local(_) if matches!(self.local_repr, Some(repr) if repr == local.representative(self.sema.db)) => {
652 ReferenceCategory::new(&Definition::Local(local), name_ref)
656 let reference = FileReference {
658 name: ast::NameLike::NameRef(name_ref.clone()),
661 sink(file_id, reference)
670 sink: &mut dyn FnMut(FileId, FileReference) -> bool,
672 match NameClass::classify(self.sema, name) {
673 Some(NameClass::PatFieldShorthand { local_def: _, field_ref })
675 self.def, Definition::Field(_) if Definition::Field(field_ref) == self.def
678 let FileRange { file_id, range } = self.sema.original_range(name.syntax());
679 let reference = FileReference {
681 name: ast::NameLike::Name(name.clone()),
682 // FIXME: mutable patterns should have `Write` access
683 category: Some(ReferenceCategory::Read),
685 sink(file_id, reference)
687 Some(NameClass::ConstReference(def)) if self.def == def => {
688 let FileRange { file_id, range } = self.sema.original_range(name.syntax());
689 let reference = FileReference {
691 name: ast::NameLike::Name(name.clone()),
694 sink(file_id, reference)
696 Some(NameClass::Definition(def @ Definition::Local(local))) if def != self.def => {
699 Some(repr) if local.representative(self.sema.db) == repr
701 let FileRange { file_id, range } = self.sema.original_range(name.syntax());
702 let reference = FileReference {
704 name: ast::NameLike::Name(name.clone()),
707 return sink(file_id, reference);
711 // Resolve trait impl function definitions to the trait definition's version if self.def is the trait definition's
712 Some(NameClass::Definition(def)) if def != self.def => {
713 /* poor man's try block */
715 let this_trait = self
717 .as_assoc_item(self.sema.db)?
718 .containing_trait_or_trait_impl(self.sema.db)?;
720 .as_assoc_item(self.sema.db)?
721 .containing_trait_or_trait_impl(self.sema.db)?;
722 (trait_ == this_trait && self.def.name(self.sema.db) == def.name(self.sema.db))
724 let FileRange { file_id, range } =
725 self.sema.original_range(name.syntax());
726 let reference = FileReference {
728 name: ast::NameLike::Name(name.clone()),
731 sink(file_id, reference)
741 fn def_to_ty(sema: &Semantics<RootDatabase>, def: &Definition) -> Option<hir::Type> {
743 Definition::Adt(adt) => Some(adt.ty(sema.db)),
744 Definition::TypeAlias(it) => Some(it.ty(sema.db)),
745 Definition::BuiltinType(it) => Some(it.ty(sema.db)),
746 Definition::SelfType(it) => Some(it.self_ty(sema.db)),
751 impl ReferenceCategory {
752 fn new(def: &Definition, r: &ast::NameRef) -> Option<ReferenceCategory> {
753 // Only Locals and Fields have accesses for now.
754 if !matches!(def, Definition::Local(_) | Definition::Field(_)) {
758 let mode = r.syntax().ancestors().find_map(|node| {
761 ast::BinExpr(expr) => {
762 if matches!(expr.op_kind()?, ast::BinaryOp::Assignment { .. }) {
763 // If the variable or field ends on the LHS's end then it's a Write (covers fields and locals).
764 // FIXME: This is not terribly accurate.
765 if let Some(lhs) = expr.lhs() {
766 if lhs.syntax().text_range().end() == r.syntax().text_range().end() {
767 return Some(ReferenceCategory::Write);
771 Some(ReferenceCategory::Read)
778 // Default Locals and Fields to read
779 mode.or(Some(ReferenceCategory::Read))