diff options
Diffstat (limited to 'crates/ra_hir_ty/src/utils.rs')
-rw-r--r-- | crates/ra_hir_ty/src/utils.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/crates/ra_hir_ty/src/utils.rs b/crates/ra_hir_ty/src/utils.rs index 508ae9046..463fd65b4 100644 --- a/crates/ra_hir_ty/src/utils.rs +++ b/crates/ra_hir_ty/src/utils.rs | |||
@@ -62,6 +62,38 @@ pub(super) fn all_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec<Tr | |||
62 | result | 62 | result |
63 | } | 63 | } |
64 | 64 | ||
65 | /// Finds a path from a trait to one of its super traits. Returns an empty | ||
66 | /// vector if there is no path. | ||
67 | pub(super) fn find_super_trait_path( | ||
68 | db: &impl DefDatabase, | ||
69 | trait_: TraitId, | ||
70 | super_trait: TraitId, | ||
71 | ) -> Vec<TraitId> { | ||
72 | let mut result = Vec::with_capacity(2); | ||
73 | result.push(trait_); | ||
74 | return if go(db, super_trait, &mut result) { result } else { Vec::new() }; | ||
75 | |||
76 | fn go(db: &impl DefDatabase, super_trait: TraitId, path: &mut Vec<TraitId>) -> bool { | ||
77 | let trait_ = *path.last().unwrap(); | ||
78 | if trait_ == super_trait { | ||
79 | return true; | ||
80 | } | ||
81 | |||
82 | for tt in direct_super_traits(db, trait_) { | ||
83 | if path.contains(&tt) { | ||
84 | continue; | ||
85 | } | ||
86 | path.push(tt); | ||
87 | if go(db, super_trait, path) { | ||
88 | return true; | ||
89 | } else { | ||
90 | path.pop(); | ||
91 | } | ||
92 | } | ||
93 | false | ||
94 | } | ||
95 | } | ||
96 | |||
65 | pub(super) fn associated_type_by_name_including_super_traits( | 97 | pub(super) fn associated_type_by_name_including_super_traits( |
66 | db: &impl DefDatabase, | 98 | db: &impl DefDatabase, |
67 | trait_: TraitId, | 99 | trait_: TraitId, |