1 //! Functionality for obtaining data related to traits from the DB.
3 use crate::RootDatabase;
5 use rustc_hash::FxHashSet;
11 /// Given the `impl` block, attempts to find the trait this `impl` corresponds to.
12 pub fn resolve_target_trait(
13 sema: &Semantics<RootDatabase>,
15 ) -> Option<hir::Trait> {
17 impl_def.trait_().map(|it| it.syntax().clone()).and_then(ast::PathType::cast)?.path()?;
19 match sema.resolve_path(&ast_path) {
20 Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def),
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>,
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();
36 if let Some(item_list) = impl_def.assoc_item_list() {
37 for item in item_list.assoc_items() {
39 ast::AssocItem::Fn(f) => {
40 if let Some(n) = f.name() {
41 impl_fns_consts.insert(n.syntax().to_string());
45 ast::AssocItem::TypeAlias(t) => {
46 if let Some(n) = t.name() {
47 impl_type.insert(n.syntax().to_string());
51 ast::AssocItem::Const(c) => {
52 if let Some(n) = c.name() {
53 impl_fns_consts.insert(n.syntax().to_string());
56 ast::AssocItem::MacroCall(_) => (),
61 resolve_target_trait(sema, impl_def).map_or(vec![], |target_trait| {
66 hir::AssocItem::Function(f) => {
67 !impl_fns_consts.contains(&f.name(sema.db).to_string())
69 hir::AssocItem::TypeAlias(t) => !impl_type.contains(&t.name(sema.db).to_string()),
70 hir::AssocItem::Const(c) => c
72 .map(|n| !impl_fns_consts.contains(&n.to_string()))