@@ -32,9 +32,7 @@ pub struct TypeckResults<'tcx> {
32
32
/// The `HirId::owner` all `ItemLocalId`s in this table are relative to.
33
33
pub hir_owner : OwnerId ,
34
34
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 ,
38
36
39
37
/// Resolved field indices for field accesses in expressions (`S { field }`, `obj.field`)
40
38
/// or patterns (`S { field }`). The index is often useful by itself, but to learn more
@@ -254,32 +252,22 @@ impl<'tcx> TypeckResults<'tcx> {
254
252
255
253
/// Returns the final resolution of a `QPath` in an `Expr` or `Pat` node.
256
254
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)
263
256
}
264
257
265
- pub fn type_dependent_defs (
266
- & self ,
267
- ) -> LocalTableInContext < ' _ , Result < ( DefKind , DefId ) , ErrorGuaranteed > > {
258
+ pub fn type_dependent_defs ( & self ) -> LocalTableInContext < ' _ , TypeDependentDef > {
268
259
LocalTableInContext { hir_owner : self . hir_owner , data : & self . type_dependent_defs }
269
260
}
270
261
271
262
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 ( ) )
274
264
}
275
265
276
266
pub fn type_dependent_def_id ( & self , id : HirId ) -> Option < DefId > {
277
267
self . type_dependent_def ( id) . map ( |( _, def_id) | def_id)
278
268
}
279
269
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 > {
283
271
LocalTableInContextMut { hir_owner : self . hir_owner , data : & mut self . type_dependent_defs }
284
272
}
285
273
@@ -549,6 +537,33 @@ impl<'tcx> TypeckResults<'tcx> {
549
537
}
550
538
}
551
539
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
+
552
567
/// Validate that the given HirId (respectively its `local_id` part) can be
553
568
/// safely used as a key in the maps of a TypeckResults. For that to be
554
569
/// the case, the HirId must have the same `owner` as all the other IDs in
@@ -581,6 +596,10 @@ pub struct LocalTableInContext<'a, V> {
581
596
}
582
597
583
598
impl < ' a , V > LocalTableInContext < ' a , V > {
599
+ pub fn new ( hir_owner : OwnerId , data : & ' a ItemLocalMap < V > ) -> Self {
600
+ Self { hir_owner, data }
601
+ }
602
+
584
603
pub fn contains_key ( & self , id : HirId ) -> bool {
585
604
validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
586
605
self . data . contains_key ( & id. local_id )
@@ -619,6 +638,10 @@ pub struct LocalTableInContextMut<'a, V> {
619
638
}
620
639
621
640
impl < ' 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
+
622
645
pub fn get_mut ( & mut self , id : HirId ) -> Option < & mut V > {
623
646
validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
624
647
self . data . get_mut ( & id. local_id )
0 commit comments