@@ -32,9 +32,7 @@ pub struct TypeckResults<'tcx> {
3232 /// The `HirId::owner` all `ItemLocalId`s in this table are relative to.
3333 pub hir_owner : OwnerId ,
3434
35- /// Resolved definitions for `<T>::X` associated paths and
36- /// method calls, including those of overloaded operators.
37- type_dependent_defs : ItemLocalMap < Result < ( DefKind , DefId ) , ErrorGuaranteed > > ,
35+ type_dependent_defs : TypeDependentDefs ,
3836
3937 /// Resolved field indices for field accesses in expressions (`S { field }`, `obj.field`)
4038 /// or patterns (`S { field }`). The index is often useful by itself, but to learn more
@@ -247,32 +245,22 @@ impl<'tcx> TypeckResults<'tcx> {
247245
248246 /// Returns the final resolution of a `QPath` in an `Expr` or `Pat` node.
249247 pub fn qpath_res ( & self , qpath : & hir:: QPath < ' _ > , id : HirId ) -> Res {
250- match * qpath {
251- hir:: QPath :: Resolved ( _, path) => path. res ,
252- hir:: QPath :: TypeRelative ( ..) | hir:: QPath :: LangItem ( ..) => self
253- . type_dependent_def ( id)
254- . map_or ( Res :: Err , |( kind, def_id) | Res :: Def ( kind, def_id) ) ,
255- }
248+ HasTypeDependentDefs :: qpath_res ( self , qpath, id)
256249 }
257250
258- pub fn type_dependent_defs (
259- & self ,
260- ) -> LocalTableInContext < ' _ , Result < ( DefKind , DefId ) , ErrorGuaranteed > > {
251+ pub fn type_dependent_defs ( & self ) -> LocalTableInContext < ' _ , TypeDependentDef > {
261252 LocalTableInContext { hir_owner : self . hir_owner , data : & self . type_dependent_defs }
262253 }
263254
264255 pub fn type_dependent_def ( & self , id : HirId ) -> Option < ( DefKind , DefId ) > {
265- validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
266- self . type_dependent_defs . get ( & id. local_id ) . cloned ( ) . and_then ( |r| r. ok ( ) )
256+ self . type_dependent_defs ( ) . get ( id) . copied ( ) . and_then ( |result| result. ok ( ) )
267257 }
268258
269259 pub fn type_dependent_def_id ( & self , id : HirId ) -> Option < DefId > {
270260 self . type_dependent_def ( id) . map ( |( _, def_id) | def_id)
271261 }
272262
273- pub fn type_dependent_defs_mut (
274- & mut self ,
275- ) -> LocalTableInContextMut < ' _ , Result < ( DefKind , DefId ) , ErrorGuaranteed > > {
263+ pub fn type_dependent_defs_mut ( & mut self ) -> LocalTableInContextMut < ' _ , TypeDependentDef > {
276264 LocalTableInContextMut { hir_owner : self . hir_owner , data : & mut self . type_dependent_defs }
277265 }
278266
@@ -555,6 +543,33 @@ impl<'tcx> TypeckResults<'tcx> {
555543 }
556544}
557545
546+ /// Resolved definitions for `<T>::X` associated paths and
547+ /// method calls, including those of overloaded operators.
548+ pub type TypeDependentDefs = ItemLocalMap < TypeDependentDef > ;
549+
550+ pub type TypeDependentDef = Result < ( DefKind , DefId ) , ErrorGuaranteed > ;
551+
552+ // FIXME(fmease): Yuck!
553+ pub trait HasTypeDependentDefs {
554+ fn type_dependent_def ( & self , id : HirId ) -> Option < ( DefKind , DefId ) > ;
555+
556+ /// Returns the final resolution of a `QPath`.
557+ fn qpath_res ( & self , qpath : & hir:: QPath < ' _ > , id : HirId ) -> Res {
558+ match qpath {
559+ hir:: QPath :: Resolved ( _, path) => path. res ,
560+ hir:: QPath :: TypeRelative ( ..) | hir:: QPath :: LangItem ( ..) => self
561+ . type_dependent_def ( id)
562+ . map_or ( Res :: Err , |( kind, def_id) | Res :: Def ( kind, def_id) ) ,
563+ }
564+ }
565+ }
566+
567+ impl HasTypeDependentDefs for TypeckResults < ' _ > {
568+ fn type_dependent_def ( & self , id : HirId ) -> Option < ( DefKind , DefId ) > {
569+ self . type_dependent_def ( id)
570+ }
571+ }
572+
558573/// Validate that the given HirId (respectively its `local_id` part) can be
559574/// safely used as a key in the maps of a TypeckResults. For that to be
560575/// the case, the HirId must have the same `owner` as all the other IDs in
@@ -587,6 +602,10 @@ pub struct LocalTableInContext<'a, V> {
587602}
588603
589604impl < ' a , V > LocalTableInContext < ' a , V > {
605+ pub fn new ( hir_owner : OwnerId , data : & ' a ItemLocalMap < V > ) -> Self {
606+ Self { hir_owner, data }
607+ }
608+
590609 pub fn contains_key ( & self , id : HirId ) -> bool {
591610 validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
592611 self . data . contains_key ( & id. local_id )
@@ -625,6 +644,10 @@ pub struct LocalTableInContextMut<'a, V> {
625644}
626645
627646impl < ' a , V > LocalTableInContextMut < ' a , V > {
647+ pub fn new ( hir_owner : OwnerId , data : & ' a mut ItemLocalMap < V > ) -> Self {
648+ Self { hir_owner, data }
649+ }
650+
628651 pub fn get_mut ( & mut self , id : HirId ) -> Option < & mut V > {
629652 validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
630653 self . data . get_mut ( & id. local_id )
0 commit comments