@@ -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
@@ -248,32 +246,22 @@ impl<'tcx> TypeckResults<'tcx> {
248246
249247 /// Returns the final resolution of a `QPath` in an `Expr` or `Pat` node.
250248 pub fn qpath_res ( & self , qpath : & hir:: QPath < ' _ > , id : HirId ) -> Res {
251- match * qpath {
252- hir:: QPath :: Resolved ( _, path) => path. res ,
253- hir:: QPath :: TypeRelative ( ..) | hir:: QPath :: LangItem ( ..) => self
254- . type_dependent_def ( id)
255- . map_or ( Res :: Err , |( kind, def_id) | Res :: Def ( kind, def_id) ) ,
256- }
249+ HasTypeDependentDefs :: qpath_res ( self , qpath, id)
257250 }
258251
259- pub fn type_dependent_defs (
260- & self ,
261- ) -> LocalTableInContext < ' _ , Result < ( DefKind , DefId ) , ErrorGuaranteed > > {
252+ pub fn type_dependent_defs ( & self ) -> LocalTableInContext < ' _ , TypeDependentDef > {
262253 LocalTableInContext { hir_owner : self . hir_owner , data : & self . type_dependent_defs }
263254 }
264255
265256 pub fn type_dependent_def ( & self , id : HirId ) -> Option < ( DefKind , DefId ) > {
266- validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
267- self . type_dependent_defs . get ( & id. local_id ) . cloned ( ) . and_then ( |r| r. ok ( ) )
257+ self . type_dependent_defs ( ) . get ( id) . copied ( ) . and_then ( |result| result. ok ( ) )
268258 }
269259
270260 pub fn type_dependent_def_id ( & self , id : HirId ) -> Option < DefId > {
271261 self . type_dependent_def ( id) . map ( |( _, def_id) | def_id)
272262 }
273263
274- pub fn type_dependent_defs_mut (
275- & mut self ,
276- ) -> LocalTableInContextMut < ' _ , Result < ( DefKind , DefId ) , ErrorGuaranteed > > {
264+ pub fn type_dependent_defs_mut ( & mut self ) -> LocalTableInContextMut < ' _ , TypeDependentDef > {
277265 LocalTableInContextMut { hir_owner : self . hir_owner , data : & mut self . type_dependent_defs }
278266 }
279267
@@ -531,6 +519,33 @@ impl<'tcx> TypeckResults<'tcx> {
531519 }
532520}
533521
522+ /// Resolved definitions for `<T>::X` associated paths and
523+ /// method calls, including those of overloaded operators.
524+ pub type TypeDependentDefs = ItemLocalMap < TypeDependentDef > ;
525+
526+ pub type TypeDependentDef = Result < ( DefKind , DefId ) , ErrorGuaranteed > ;
527+
528+ // FIXME(fmease): Yuck!
529+ pub trait HasTypeDependentDefs {
530+ fn type_dependent_def ( & self , id : HirId ) -> Option < ( DefKind , DefId ) > ;
531+
532+ /// Returns the final resolution of a `QPath`.
533+ fn qpath_res ( & self , qpath : & hir:: QPath < ' _ > , id : HirId ) -> Res {
534+ match qpath {
535+ hir:: QPath :: Resolved ( _, path) => path. res ,
536+ hir:: QPath :: TypeRelative ( ..) | hir:: QPath :: LangItem ( ..) => self
537+ . type_dependent_def ( id)
538+ . map_or ( Res :: Err , |( kind, def_id) | Res :: Def ( kind, def_id) ) ,
539+ }
540+ }
541+ }
542+
543+ impl HasTypeDependentDefs for TypeckResults < ' _ > {
544+ fn type_dependent_def ( & self , id : HirId ) -> Option < ( DefKind , DefId ) > {
545+ self . type_dependent_def ( id)
546+ }
547+ }
548+
534549/// Validate that the given HirId (respectively its `local_id` part) can be
535550/// safely used as a key in the maps of a TypeckResults. For that to be
536551/// the case, the HirId must have the same `owner` as all the other IDs in
@@ -563,6 +578,10 @@ pub struct LocalTableInContext<'a, V> {
563578}
564579
565580impl < ' a , V > LocalTableInContext < ' a , V > {
581+ pub fn new ( hir_owner : OwnerId , data : & ' a ItemLocalMap < V > ) -> Self {
582+ Self { hir_owner, data }
583+ }
584+
566585 pub fn contains_key ( & self , id : HirId ) -> bool {
567586 validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
568587 self . data . contains_key ( & id. local_id )
@@ -601,6 +620,10 @@ pub struct LocalTableInContextMut<'a, V> {
601620}
602621
603622impl < ' a , V > LocalTableInContextMut < ' a , V > {
623+ pub fn new ( hir_owner : OwnerId , data : & ' a mut ItemLocalMap < V > ) -> Self {
624+ Self { hir_owner, data }
625+ }
626+
604627 pub fn get_mut ( & mut self , id : HirId ) -> Option < & mut V > {
605628 validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
606629 self . data . get_mut ( & id. local_id )
0 commit comments