@@ -1925,21 +1925,50 @@ impl<'tcx> TyCtxt<'tcx> {
19251925 self . impl_trait_ref ( def_id) . map ( |tr| tr. skip_binder ( ) . def_id )
19261926 }
19271927
1928- /// If the given `DefId` is an associated item, returns the `DefId` of the parent trait or impl.
1929- pub fn assoc_parent ( self , def_id : DefId ) -> Option < DefId > {
1930- self . def_kind ( def_id) . is_assoc ( ) . then ( || self . parent ( def_id) )
1928+ /// If the given `DefId` is an associated item, returns the `DefId` and `DefKind` of the parent trait or impl.
1929+ pub fn assoc_parent ( self , def_id : DefId ) -> Option < ( DefId , DefKind ) > {
1930+ if !self . def_kind ( def_id) . is_assoc ( ) {
1931+ return None ;
1932+ }
1933+ let parent = self . parent ( def_id) ;
1934+ let def_kind = self . def_kind ( parent) ;
1935+ Some ( ( parent, def_kind) )
19311936 }
19321937
19331938 /// If the given `DefId` is an associated item of a trait,
19341939 /// returns the `DefId` of the trait; otherwise, returns `None`.
19351940 pub fn trait_of_assoc ( self , def_id : DefId ) -> Option < DefId > {
1936- self . assoc_parent ( def_id) . filter ( |id| self . def_kind ( id) == DefKind :: Trait )
1941+ match self . assoc_parent ( def_id) {
1942+ Some ( ( id, DefKind :: Trait ) ) => Some ( id) ,
1943+ _ => None ,
1944+ }
19371945 }
19381946
19391947 /// If the given `DefId` is an associated item of an impl,
19401948 /// returns the `DefId` of the impl; otherwise returns `None`.
19411949 pub fn impl_of_assoc ( self , def_id : DefId ) -> Option < DefId > {
1942- self . assoc_parent ( def_id) . filter ( |id| matches ! ( self . def_kind( id) , DefKind :: Impl { .. } ) )
1950+ match self . assoc_parent ( def_id) {
1951+ Some ( ( id, DefKind :: Impl { .. } ) ) => Some ( id) ,
1952+ _ => None ,
1953+ }
1954+ }
1955+
1956+ /// If the given `DefId` is an associated item of an inherent impl,
1957+ /// returns the `DefId` of the impl; otherwise, returns `None`.
1958+ pub fn inherent_impl_of_assoc ( self , def_id : DefId ) -> Option < DefId > {
1959+ match self . assoc_parent ( def_id) {
1960+ Some ( ( id, DefKind :: Impl { of_trait : false } ) ) => Some ( id) ,
1961+ _ => None ,
1962+ }
1963+ }
1964+
1965+ /// If the given `DefId` is an associated item of a trait impl,
1966+ /// returns the `DefId` of the impl; otherwise, returns `None`.
1967+ pub fn trait_impl_of_assoc ( self , def_id : DefId ) -> Option < DefId > {
1968+ match self . assoc_parent ( def_id) {
1969+ Some ( ( id, DefKind :: Impl { of_trait : true } ) ) => Some ( id) ,
1970+ _ => None ,
1971+ }
19431972 }
19441973
19451974 pub fn is_exportable ( self , def_id : DefId ) -> bool {
0 commit comments