@@ -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
@@ -540,6 +528,33 @@ impl<'tcx> TypeckResults<'tcx> {
540528 }
541529}
542530
531+ /// Resolved definitions for `<T>::X` associated paths and
532+ /// method calls, including those of overloaded operators.
533+ pub type TypeDependentDefs = ItemLocalMap < TypeDependentDef > ;
534+
535+ pub type TypeDependentDef = Result < ( DefKind , DefId ) , ErrorGuaranteed > ;
536+
537+ // FIXME(fmease): Yuck!
538+ pub trait HasTypeDependentDefs {
539+ fn type_dependent_def ( & self , id : HirId ) -> Option < ( DefKind , DefId ) > ;
540+
541+ /// Returns the final resolution of a `QPath`.
542+ fn qpath_res ( & self , qpath : & hir:: QPath < ' _ > , id : HirId ) -> Res {
543+ match qpath {
544+ hir:: QPath :: Resolved ( _, path) => path. res ,
545+ hir:: QPath :: TypeRelative ( ..) | hir:: QPath :: LangItem ( ..) => self
546+ . type_dependent_def ( id)
547+ . map_or ( Res :: Err , |( kind, def_id) | Res :: Def ( kind, def_id) ) ,
548+ }
549+ }
550+ }
551+
552+ impl HasTypeDependentDefs for TypeckResults < ' _ > {
553+ fn type_dependent_def ( & self , id : HirId ) -> Option < ( DefKind , DefId ) > {
554+ self . type_dependent_def ( id)
555+ }
556+ }
557+
543558/// Validate that the given HirId (respectively its `local_id` part) can be
544559/// safely used as a key in the maps of a TypeckResults. For that to be
545560/// the case, the HirId must have the same `owner` as all the other IDs in
@@ -572,6 +587,10 @@ pub struct LocalTableInContext<'a, V> {
572587}
573588
574589impl < ' a , V > LocalTableInContext < ' a , V > {
590+ pub fn new ( hir_owner : OwnerId , data : & ' a ItemLocalMap < V > ) -> Self {
591+ Self { hir_owner, data }
592+ }
593+
575594 pub fn contains_key ( & self , id : HirId ) -> bool {
576595 validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
577596 self . data . contains_key ( & id. local_id )
@@ -610,6 +629,10 @@ pub struct LocalTableInContextMut<'a, V> {
610629}
611630
612631impl < ' a , V > LocalTableInContextMut < ' a , V > {
632+ pub fn new ( hir_owner : OwnerId , data : & ' a mut ItemLocalMap < V > ) -> Self {
633+ Self { hir_owner, data }
634+ }
635+
613636 pub fn get_mut ( & mut self , id : HirId ) -> Option < & mut V > {
614637 validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
615638 self . data . get_mut ( & id. local_id )
0 commit comments