Add pub fn direct_super_traits(db, trait_id) to hir_ty crate

This commit is contained in:
Vincent Esche 2024-11-06 09:52:49 +01:00
parent 5a9767b115
commit c1155213f3
2 changed files with 14 additions and 3 deletions

View file

@ -98,7 +98,7 @@ pub use mapping::{
}; };
pub use method_resolution::check_orphan_rules; pub use method_resolution::check_orphan_rules;
pub use traits::TraitEnvironment; pub use traits::TraitEnvironment;
pub use utils::{all_super_traits, is_fn_unsafe_to_call}; pub use utils::{all_super_traits, direct_super_traits, is_fn_unsafe_to_call};
pub use chalk_ir::{ pub use chalk_ir::{
cast::Cast, cast::Cast,

View file

@ -43,6 +43,17 @@ pub(crate) fn fn_traits(
.flat_map(|it| it.as_trait()) .flat_map(|it| it.as_trait())
} }
/// Returns an iterator over the direct super traits (including the trait itself).
pub fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> SmallVec<[TraitId; 4]> {
let mut result = smallvec![trait_];
direct_super_traits_cb(db, trait_, |tt| {
if !result.contains(&tt) {
result.push(tt);
}
});
result
}
/// Returns an iterator over the whole super trait hierarchy (including the /// Returns an iterator over the whole super trait hierarchy (including the
/// trait itself). /// trait itself).
pub fn all_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> SmallVec<[TraitId; 4]> { pub fn all_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> SmallVec<[TraitId; 4]> {
@ -54,7 +65,7 @@ pub fn all_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> SmallVec<[Trai
while let Some(&t) = result.get(i) { while let Some(&t) = result.get(i) {
// yeah this is quadratic, but trait hierarchies should be flat // yeah this is quadratic, but trait hierarchies should be flat
// enough that this doesn't matter // enough that this doesn't matter
direct_super_traits(db, t, |tt| { direct_super_traits_cb(db, t, |tt| {
if !result.contains(&tt) { if !result.contains(&tt) {
result.push(tt); result.push(tt);
} }
@ -153,7 +164,7 @@ impl Iterator for ClauseElaborator<'_> {
} }
} }
fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId, cb: impl FnMut(TraitId)) { fn direct_super_traits_cb(db: &dyn DefDatabase, trait_: TraitId, cb: impl FnMut(TraitId)) {
let resolver = trait_.resolver(db); let resolver = trait_.resolver(db);
let generic_params = db.generic_params(trait_.into()); let generic_params = db.generic_params(trait_.into());
let trait_self = generic_params.trait_self_param(); let trait_self = generic_params.trait_self_param();