@@ -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
@@ -258,32 +256,22 @@ impl<'tcx> TypeckResults<'tcx> {
258256
259257 /// Returns the final resolution of a `QPath` in an `Expr` or `Pat` node.
260258 pub fn qpath_res ( & self , qpath : & hir:: QPath < ' _ > , id : HirId ) -> Res {
261- match * qpath {
262- hir:: QPath :: Resolved ( _, path) => path. res ,
263- hir:: QPath :: TypeRelative ( ..) => self
264- . type_dependent_def ( id)
265- . map_or ( Res :: Err , |( kind, def_id) | Res :: Def ( kind, def_id) ) ,
266- }
259+ HasTypeDependentDefs :: qpath_res ( self , qpath, id)
267260 }
268261
269- pub fn type_dependent_defs (
270- & self ,
271- ) -> LocalTableInContext < ' _ , Result < ( DefKind , DefId ) , ErrorGuaranteed > > {
262+ pub fn type_dependent_defs ( & self ) -> LocalTableInContext < ' _ , TypeDependentDef > {
272263 LocalTableInContext { hir_owner : self . hir_owner , data : & self . type_dependent_defs }
273264 }
274265
275266 pub fn type_dependent_def ( & self , id : HirId ) -> Option < ( DefKind , DefId ) > {
276- validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
277- self . type_dependent_defs . get ( & id. local_id ) . cloned ( ) . and_then ( |r| r. ok ( ) )
267+ self . type_dependent_defs ( ) . get ( id) . copied ( ) . and_then ( |result| result. ok ( ) )
278268 }
279269
280270 pub fn type_dependent_def_id ( & self , id : HirId ) -> Option < DefId > {
281271 self . type_dependent_def ( id) . map ( |( _, def_id) | def_id)
282272 }
283273
284- pub fn type_dependent_defs_mut (
285- & mut self ,
286- ) -> LocalTableInContextMut < ' _ , Result < ( DefKind , DefId ) , ErrorGuaranteed > > {
274+ pub fn type_dependent_defs_mut ( & mut self ) -> LocalTableInContextMut < ' _ , TypeDependentDef > {
287275 LocalTableInContextMut { hir_owner : self . hir_owner , data : & mut self . type_dependent_defs }
288276 }
289277
@@ -569,6 +557,33 @@ impl<'tcx> TypeckResults<'tcx> {
569557 }
570558}
571559
560+ /// Resolved definitions for `<T>::X` associated paths and
561+ /// method calls, including those of overloaded operators.
562+ pub type TypeDependentDefs = ItemLocalMap < TypeDependentDef > ;
563+
564+ pub type TypeDependentDef = Result < ( DefKind , DefId ) , ErrorGuaranteed > ;
565+
566+ // FIXME(fmease): Yuck!
567+ pub trait HasTypeDependentDefs {
568+ fn type_dependent_def ( & self , id : HirId ) -> Option < ( DefKind , DefId ) > ;
569+
570+ /// Returns the final resolution of a `QPath`.
571+ fn qpath_res ( & self , qpath : & hir:: QPath < ' _ > , id : HirId ) -> Res {
572+ match qpath {
573+ hir:: QPath :: Resolved ( _, path) => path. res ,
574+ hir:: QPath :: TypeRelative ( ..) => self
575+ . type_dependent_def ( id)
576+ . map_or ( Res :: Err , |( kind, def_id) | Res :: Def ( kind, def_id) ) ,
577+ }
578+ }
579+ }
580+
581+ impl HasTypeDependentDefs for TypeckResults < ' _ > {
582+ fn type_dependent_def ( & self , id : HirId ) -> Option < ( DefKind , DefId ) > {
583+ self . type_dependent_def ( id)
584+ }
585+ }
586+
572587/// Validate that the given HirId (respectively its `local_id` part) can be
573588/// safely used as a key in the maps of a TypeckResults. For that to be
574589/// the case, the HirId must have the same `owner` as all the other IDs in
@@ -601,6 +616,10 @@ pub struct LocalTableInContext<'a, V> {
601616}
602617
603618impl < ' a , V > LocalTableInContext < ' a , V > {
619+ pub fn new ( hir_owner : OwnerId , data : & ' a ItemLocalMap < V > ) -> Self {
620+ Self { hir_owner, data }
621+ }
622+
604623 pub fn contains_key ( & self , id : HirId ) -> bool {
605624 validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
606625 self . data . contains_key ( & id. local_id )
@@ -639,6 +658,10 @@ pub struct LocalTableInContextMut<'a, V> {
639658}
640659
641660impl < ' a , V > LocalTableInContextMut < ' a , V > {
661+ pub fn new ( hir_owner : OwnerId , data : & ' a mut ItemLocalMap < V > ) -> Self {
662+ Self { hir_owner, data }
663+ }
664+
642665 pub fn get_mut ( & mut self , id : HirId ) -> Option < & mut V > {
643666 validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
644667 self . data . get_mut ( & id. local_id )
0 commit comments