@@ -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
@@ -247,32 +245,22 @@ impl<'tcx> TypeckResults<'tcx> {
247
245
248
246
/// Returns the final resolution of a `QPath` in an `Expr` or `Pat` node.
249
247
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)
256
249
}
257
250
258
- pub fn type_dependent_defs (
259
- & self ,
260
- ) -> LocalTableInContext < ' _ , Result < ( DefKind , DefId ) , ErrorGuaranteed > > {
251
+ pub fn type_dependent_defs ( & self ) -> LocalTableInContext < ' _ , TypeDependentDef > {
261
252
LocalTableInContext { hir_owner : self . hir_owner , data : & self . type_dependent_defs }
262
253
}
263
254
264
255
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 ( ) )
267
257
}
268
258
269
259
pub fn type_dependent_def_id ( & self , id : HirId ) -> Option < DefId > {
270
260
self . type_dependent_def ( id) . map ( |( _, def_id) | def_id)
271
261
}
272
262
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 > {
276
264
LocalTableInContextMut { hir_owner : self . hir_owner , data : & mut self . type_dependent_defs }
277
265
}
278
266
@@ -540,6 +528,33 @@ impl<'tcx> TypeckResults<'tcx> {
540
528
}
541
529
}
542
530
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
+
543
558
/// Validate that the given HirId (respectively its `local_id` part) can be
544
559
/// safely used as a key in the maps of a TypeckResults. For that to be
545
560
/// the case, the HirId must have the same `owner` as all the other IDs in
@@ -572,6 +587,10 @@ pub struct LocalTableInContext<'a, V> {
572
587
}
573
588
574
589
impl < ' a , V > LocalTableInContext < ' a , V > {
590
+ pub fn new ( hir_owner : OwnerId , data : & ' a ItemLocalMap < V > ) -> Self {
591
+ Self { hir_owner, data }
592
+ }
593
+
575
594
pub fn contains_key ( & self , id : HirId ) -> bool {
576
595
validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
577
596
self . data . contains_key ( & id. local_id )
@@ -610,6 +629,10 @@ pub struct LocalTableInContextMut<'a, V> {
610
629
}
611
630
612
631
impl < ' 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
+
613
636
pub fn get_mut ( & mut self , id : HirId ) -> Option < & mut V > {
614
637
validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
615
638
self . data . get_mut ( & id. local_id )
0 commit comments