From 16c218c57ff83c82ba58753c3a67f697e260adc1 Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Fri, 15 Aug 2025 16:12:35 -0500 Subject: Introduce trait_item_of --- compiler/rustc_middle/src/ty/mod.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'compiler/rustc_middle') diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index da17ec1f9f3..5a4ebfe28a4 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1934,6 +1934,15 @@ impl<'tcx> TyCtxt<'tcx> { Some((parent, def_kind)) } + /// Returns the trait item that is implemented by the given item `DefId`. + pub fn trait_item_of(self, def_id: impl IntoQueryParam) -> Option { + let assoc = self.opt_associated_item(def_id.into_query_param())?; + if assoc.container != AssocItemContainer::Impl { + return None; + } + assoc.trait_item_def_id + } + /// If the given `DefId` is an associated item of a trait, /// returns the `DefId` of the trait; otherwise, returns `None`. pub fn trait_of_assoc(self, def_id: DefId) -> Option { -- cgit 1.4.1-3-g733a5