]> git.lizzy.rs Git - rust.git/blob - crates/ide_db/src/traits.rs
Merge #11157
[rust.git] / crates / ide_db / src / traits.rs
1 //! Functionality for obtaining data related to traits from the DB.
2
3 use crate::RootDatabase;
4 use hir::Semantics;
5 use rustc_hash::FxHashSet;
6 use syntax::{
7     ast::{self, HasName},
8     AstNode,
9 };
10
11 /// Given the `impl` block, attempts to find the trait this `impl` corresponds to.
12 pub fn resolve_target_trait(
13     sema: &Semantics<RootDatabase>,
14     impl_def: &ast::Impl,
15 ) -> Option<hir::Trait> {
16     let ast_path =
17         impl_def.trait_().map(|it| it.syntax().clone()).and_then(ast::PathType::cast)?.path()?;
18
19     match sema.resolve_path(&ast_path) {
20         Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def),
21         _ => None,
22     }
23 }
24
25 /// Given the `impl` block, returns the list of associated items (e.g. functions or types) that are
26 /// missing in this `impl` block.
27 pub fn get_missing_assoc_items(
28     sema: &Semantics<RootDatabase>,
29     impl_def: &ast::Impl,
30 ) -> Vec<hir::AssocItem> {
31     // Names must be unique between constants and functions. However, type aliases
32     // may share the same name as a function or constant.
33     let mut impl_fns_consts = FxHashSet::default();
34     let mut impl_type = FxHashSet::default();
35
36     if let Some(item_list) = impl_def.assoc_item_list() {
37         for item in item_list.assoc_items() {
38             match item {
39                 ast::AssocItem::Fn(f) => {
40                     if let Some(n) = f.name() {
41                         impl_fns_consts.insert(n.syntax().to_string());
42                     }
43                 }
44
45                 ast::AssocItem::TypeAlias(t) => {
46                     if let Some(n) = t.name() {
47                         impl_type.insert(n.syntax().to_string());
48                     }
49                 }
50
51                 ast::AssocItem::Const(c) => {
52                     if let Some(n) = c.name() {
53                         impl_fns_consts.insert(n.syntax().to_string());
54                     }
55                 }
56                 ast::AssocItem::MacroCall(_) => (),
57             }
58         }
59     }
60
61     resolve_target_trait(sema, impl_def).map_or(vec![], |target_trait| {
62         target_trait
63             .items(sema.db)
64             .into_iter()
65             .filter(|i| match i {
66                 hir::AssocItem::Function(f) => {
67                     !impl_fns_consts.contains(&f.name(sema.db).to_string())
68                 }
69                 hir::AssocItem::TypeAlias(t) => !impl_type.contains(&t.name(sema.db).to_string()),
70                 hir::AssocItem::Const(c) => c
71                     .name(sema.db)
72                     .map(|n| !impl_fns_consts.contains(&n.to_string()))
73                     .unwrap_or_default(),
74             })
75             .collect()
76     })
77 }
78
79 #[cfg(test)]
80 mod tests;