diff options
| author | Cameron Steffen <cam.steffen94@gmail.com> | 2025-07-24 18:34:17 -0500 |
|---|---|---|
| committer | Cameron Steffen <cam.steffen94@gmail.com> | 2025-07-28 09:53:50 -0500 |
| commit | 0d7abc8df083296c29e0fb816c1c4b10e4cf6577 (patch) | |
| tree | c9f7b45d8ce47205c5ffcdbda7c2c269d6166dfc /compiler/rustc_middle/src/ty/mod.rs | |
| parent | 96aca2b442f42684f2deeaf2be560e9548864363 (diff) | |
| download | rust-0d7abc8df083296c29e0fb816c1c4b10e4cf6577.tar.gz rust-0d7abc8df083296c29e0fb816c1c4b10e4cf6577.zip | |
Introduce assoc_parent
Diffstat (limited to 'compiler/rustc_middle/src/ty/mod.rs')
| -rw-r--r-- | compiler/rustc_middle/src/ty/mod.rs | 21 |
1 files changed, 7 insertions, 14 deletions
diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 2e1be6e322f..55606557770 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1988,29 +1988,22 @@ impl<'tcx> TyCtxt<'tcx> { self.impl_trait_ref(def_id).map(|tr| tr.skip_binder().def_id) } + /// If the given `DefId` is an associated item, returns the `DefId` of the parent trait or impl. + pub fn assoc_parent(self, def_id: DefId) -> Option<DefId> { + self.def_kind(def_id).is_assoc().then(|| self.parent(def_id)) + } + /// If the given `DefId` describes an item belonging to a trait, /// returns the `DefId` of the trait that the trait item belongs to; /// otherwise, returns `None`. pub fn trait_of_item(self, def_id: DefId) -> Option<DefId> { - if let DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy = self.def_kind(def_id) { - let parent = self.parent(def_id); - if let DefKind::Trait = self.def_kind(parent) { - return Some(parent); - } - } - None + self.assoc_parent(def_id).filter(|id| self.def_kind(id) == DefKind::Trait) } /// If the given `DefId` describes a method belonging to an impl, returns the /// `DefId` of the impl that the method belongs to; otherwise, returns `None`. pub fn impl_of_method(self, def_id: DefId) -> Option<DefId> { - if let DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy = self.def_kind(def_id) { - let parent = self.parent(def_id); - if let DefKind::Impl { .. } = self.def_kind(parent) { - return Some(parent); - } - } - None + self.assoc_parent(def_id).filter(|id| matches!(self.def_kind(id), DefKind::Impl { .. })) } pub fn is_exportable(self, def_id: DefId) -> bool { |
