@@ -1925,21 +1925,50 @@ impl<'tcx> TyCtxt<'tcx> {
1925
1925
self . impl_trait_ref ( def_id) . map ( |tr| tr. skip_binder ( ) . def_id )
1926
1926
}
1927
1927
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) )
1931
1936
}
1932
1937
1933
1938
/// If the given `DefId` is an associated item of a trait,
1934
1939
/// returns the `DefId` of the trait; otherwise, returns `None`.
1935
1940
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
+ }
1937
1945
}
1938
1946
1939
1947
/// If the given `DefId` is an associated item of an impl,
1940
1948
/// returns the `DefId` of the impl; otherwise returns `None`.
1941
1949
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
+ }
1943
1972
}
1944
1973
1945
1974
pub fn is_exportable ( self , def_id : DefId ) -> bool {
0 commit comments