@@ -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
@@ -254,32 +252,22 @@ impl<'tcx> TypeckResults<'tcx> {
254252
255253 /// Returns the final resolution of a `QPath` in an `Expr` or `Pat` node.
256254 pub fn qpath_res ( & self , qpath : & hir:: QPath < ' _ > , id : HirId ) -> Res {
257- match * qpath {
258- hir:: QPath :: Resolved ( _, path) => path. res ,
259- hir:: QPath :: TypeRelative ( ..) | hir:: QPath :: LangItem ( ..) => self
260- . type_dependent_def ( id)
261- . map_or ( Res :: Err , |( kind, def_id) | Res :: Def ( kind, def_id) ) ,
262- }
255+ HasTypeDependentDefs :: qpath_res ( self , qpath, id)
263256 }
264257
265- pub fn type_dependent_defs (
266- & self ,
267- ) -> LocalTableInContext < ' _ , Result < ( DefKind , DefId ) , ErrorGuaranteed > > {
258+ pub fn type_dependent_defs ( & self ) -> LocalTableInContext < ' _ , TypeDependentDef > {
268259 LocalTableInContext { hir_owner : self . hir_owner , data : & self . type_dependent_defs }
269260 }
270261
271262 pub fn type_dependent_def ( & self , id : HirId ) -> Option < ( DefKind , DefId ) > {
272- validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
273- self . type_dependent_defs . get ( & id. local_id ) . cloned ( ) . and_then ( |r| r. ok ( ) )
263+ self . type_dependent_defs ( ) . get ( id) . copied ( ) . and_then ( |result| result. ok ( ) )
274264 }
275265
276266 pub fn type_dependent_def_id ( & self , id : HirId ) -> Option < DefId > {
277267 self . type_dependent_def ( id) . map ( |( _, def_id) | def_id)
278268 }
279269
280- pub fn type_dependent_defs_mut (
281- & mut self ,
282- ) -> LocalTableInContextMut < ' _ , Result < ( DefKind , DefId ) , ErrorGuaranteed > > {
270+ pub fn type_dependent_defs_mut ( & mut self ) -> LocalTableInContextMut < ' _ , TypeDependentDef > {
283271 LocalTableInContextMut { hir_owner : self . hir_owner , data : & mut self . type_dependent_defs }
284272 }
285273
@@ -549,6 +537,33 @@ impl<'tcx> TypeckResults<'tcx> {
549537 }
550538}
551539
540+ /// Resolved definitions for `<T>::X` associated paths and
541+ /// method calls, including those of overloaded operators.
542+ pub type TypeDependentDefs = ItemLocalMap < TypeDependentDef > ;
543+
544+ pub type TypeDependentDef = Result < ( DefKind , DefId ) , ErrorGuaranteed > ;
545+
546+ // FIXME(fmease): Yuck!
547+ pub trait HasTypeDependentDefs {
548+ fn type_dependent_def ( & self , id : HirId ) -> Option < ( DefKind , DefId ) > ;
549+
550+ /// Returns the final resolution of a `QPath`.
551+ fn qpath_res ( & self , qpath : & hir:: QPath < ' _ > , id : HirId ) -> Res {
552+ match qpath {
553+ hir:: QPath :: Resolved ( _, path) => path. res ,
554+ hir:: QPath :: TypeRelative ( ..) | hir:: QPath :: LangItem ( ..) => self
555+ . type_dependent_def ( id)
556+ . map_or ( Res :: Err , |( kind, def_id) | Res :: Def ( kind, def_id) ) ,
557+ }
558+ }
559+ }
560+
561+ impl HasTypeDependentDefs for TypeckResults < ' _ > {
562+ fn type_dependent_def ( & self , id : HirId ) -> Option < ( DefKind , DefId ) > {
563+ self . type_dependent_def ( id)
564+ }
565+ }
566+
552567/// Validate that the given HirId (respectively its `local_id` part) can be
553568/// safely used as a key in the maps of a TypeckResults. For that to be
554569/// the case, the HirId must have the same `owner` as all the other IDs in
@@ -581,6 +596,10 @@ pub struct LocalTableInContext<'a, V> {
581596}
582597
583598impl < ' a , V > LocalTableInContext < ' a , V > {
599+ pub fn new ( hir_owner : OwnerId , data : & ' a ItemLocalMap < V > ) -> Self {
600+ Self { hir_owner, data }
601+ }
602+
584603 pub fn contains_key ( & self , id : HirId ) -> bool {
585604 validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
586605 self . data . contains_key ( & id. local_id )
@@ -619,6 +638,10 @@ pub struct LocalTableInContextMut<'a, V> {
619638}
620639
621640impl < ' a , V > LocalTableInContextMut < ' a , V > {
641+ pub fn new ( hir_owner : OwnerId , data : & ' a mut ItemLocalMap < V > ) -> Self {
642+ Self { hir_owner, data }
643+ }
644+
622645 pub fn get_mut ( & mut self , id : HirId ) -> Option < & mut V > {
623646 validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
624647 self . data . get_mut ( & id. local_id )
0 commit comments