Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add public direct_supertraits(…) & all_supertraits(…) accessor methods to hir::Trait #18482

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/hir-ty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub use mapping::{
};
pub use method_resolution::check_orphan_rules;
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::{
cast::Cast,
Expand Down
15 changes: 13 additions & 2 deletions crates/hir-ty/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ pub(crate) fn fn_traits(
.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
/// trait itself).
pub fn all_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> SmallVec<[TraitId; 4]> {
Expand All @@ -54,7 +65,7 @@ pub fn all_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> SmallVec<[Trai
while let Some(&t) = result.get(i) {
// yeah this is quadratic, but trait hierarchies should be flat
// enough that this doesn't matter
direct_super_traits(db, t, |tt| {
direct_super_traits_cb(db, t, |tt| {
if !result.contains(&tt) {
result.push(tt);
}
Expand Down Expand Up @@ -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 generic_params = db.generic_params(trait_.into());
let trait_self = generic_params.trait_self_param();
Expand Down
15 changes: 12 additions & 3 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ use hir_ty::{
all_super_traits, autoderef, check_orphan_rules,
consteval::{try_const_usize, unknown_const_as_generic, ConstExt},
diagnostics::BodyValidationDiagnostic,
error_lifetime, known_const_to_ast,
direct_super_traits, error_lifetime, known_const_to_ast,
layout::{Layout as TyLayout, RustcEnumVariantIdx, RustcFieldIdx, TagEncoding},
method_resolution,
mir::{interpret_mir, MutBorrowKind},
Expand Down Expand Up @@ -2704,13 +2704,22 @@ impl Trait {
db.trait_data(self.id).name.clone()
}

pub fn direct_supertraits(self, db: &dyn HirDatabase) -> Vec<Trait> {
let traits = direct_super_traits(db.upcast(), self.into());
traits.iter().map(|tr| Trait::from(*tr)).collect()
}

pub fn all_supertraits(self, db: &dyn HirDatabase) -> Vec<Trait> {
let traits = all_super_traits(db.upcast(), self.into());
traits.iter().map(|tr| Trait::from(*tr)).collect()
}

pub fn items(self, db: &dyn HirDatabase) -> Vec<AssocItem> {
db.trait_data(self.id).items.iter().map(|(_name, it)| (*it).into()).collect()
}

pub fn items_with_supertraits(self, db: &dyn HirDatabase) -> Vec<AssocItem> {
let traits = all_super_traits(db.upcast(), self.into());
traits.iter().flat_map(|tr| Trait::from(*tr).items(db)).collect()
self.all_supertraits(db).into_iter().flat_map(|tr| tr.items(db)).collect()
}

pub fn is_auto(self, db: &dyn HirDatabase) -> bool {
Expand Down