From 89a002ef9bbf2a49526087dc543ca1a8ad4c0cee Mon Sep 17 00:00:00 2001 From: Vincent Esche Date: Wed, 6 Nov 2024 09:53:56 +0100 Subject: [PATCH 1/4] =?UTF-8?q?Add=20`pub=20fn=20all=5Fsupertraits(?= =?UTF-8?q?=E2=80=A6)`=20HIR-level=20method=20to=20`hir::Trait`=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/hir/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 8c3b7a6d3cf5..00a46daa54e7 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -2704,6 +2704,11 @@ impl Trait { db.trait_data(self.id).name.clone() } + pub fn all_supertraits(self, db: &dyn HirDatabase) -> Vec { + 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 { db.trait_data(self.id).items.iter().map(|(_name, it)| (*it).into()).collect() } From 5a9767b115d1b80d7a05e5084a55b355dff4000e Mon Sep 17 00:00:00 2001 From: Vincent Esche Date: Wed, 6 Nov 2024 09:37:53 +0100 Subject: [PATCH 2/4] =?UTF-8?q?Refactor=20`hir::Trait`'s=20existing=20`ite?= =?UTF-8?q?ms=5Fwith=5Fsupertraits(=E2=80=A6)`=20method=20based=20on=20new?= =?UTF-8?q?=20`all=5Fsupertraits(=E2=80=A6)`=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/hir/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 00a46daa54e7..bfe9f3bd74ef 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -2714,8 +2714,7 @@ impl Trait { } pub fn items_with_supertraits(self, db: &dyn HirDatabase) -> Vec { - 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 { From c1155213f3fe6e0f7c511f552602cfcf1a68ec8f Mon Sep 17 00:00:00 2001 From: Vincent Esche Date: Wed, 6 Nov 2024 09:52:49 +0100 Subject: [PATCH 3/4] Add `pub fn direct_super_traits(db, trait_id)` to `hir_ty` crate --- crates/hir-ty/src/lib.rs | 2 +- crates/hir-ty/src/utils.rs | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/crates/hir-ty/src/lib.rs b/crates/hir-ty/src/lib.rs index 9c1d8bcf36f0..22e7b1d920f6 100644 --- a/crates/hir-ty/src/lib.rs +++ b/crates/hir-ty/src/lib.rs @@ -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, diff --git a/crates/hir-ty/src/utils.rs b/crates/hir-ty/src/utils.rs index 7429ce3c73e0..28bda1e10e58 100644 --- a/crates/hir-ty/src/utils.rs +++ b/crates/hir-ty/src/utils.rs @@ -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]> { @@ -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); } @@ -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(); From e6461522bc5a73dc67267ebedd4c3f86b2011b3e Mon Sep 17 00:00:00 2001 From: Vincent Esche Date: Wed, 6 Nov 2024 09:53:11 +0100 Subject: [PATCH 4/4] =?UTF-8?q?Add=20`direct=5Fsupertraits(=E2=80=A6)`=20H?= =?UTF-8?q?IR-level=20method=20to=20`hir::Trait`=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/hir/src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index bfe9f3bd74ef..c9498b3aead7 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -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}, @@ -2704,6 +2704,11 @@ impl Trait { db.trait_data(self.id).name.clone() } + pub fn direct_supertraits(self, db: &dyn HirDatabase) -> Vec { + 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 { let traits = all_super_traits(db.upcast(), self.into()); traits.iter().map(|tr| Trait::from(*tr)).collect()