@@ -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
@@ -248,32 +246,22 @@ impl<'tcx> TypeckResults<'tcx> {
248
246
249
247
/// Returns the final resolution of a `QPath` in an `Expr` or `Pat` node.
250
248
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)
257
250
}
258
251
259
- pub fn type_dependent_defs (
260
- & self ,
261
- ) -> LocalTableInContext < ' _ , Result < ( DefKind , DefId ) , ErrorGuaranteed > > {
252
+ pub fn type_dependent_defs ( & self ) -> LocalTableInContext < ' _ , TypeDependentDef > {
262
253
LocalTableInContext { hir_owner : self . hir_owner , data : & self . type_dependent_defs }
263
254
}
264
255
265
256
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 ( ) )
268
258
}
269
259
270
260
pub fn type_dependent_def_id ( & self , id : HirId ) -> Option < DefId > {
271
261
self . type_dependent_def ( id) . map ( |( _, def_id) | def_id)
272
262
}
273
263
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 > {
277
265
LocalTableInContextMut { hir_owner : self . hir_owner , data : & mut self . type_dependent_defs }
278
266
}
279
267
@@ -531,6 +519,33 @@ impl<'tcx> TypeckResults<'tcx> {
531
519
}
532
520
}
533
521
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
+
534
549
/// Validate that the given HirId (respectively its `local_id` part) can be
535
550
/// safely used as a key in the maps of a TypeckResults. For that to be
536
551
/// the case, the HirId must have the same `owner` as all the other IDs in
@@ -563,6 +578,10 @@ pub struct LocalTableInContext<'a, V> {
563
578
}
564
579
565
580
impl < ' a , V > LocalTableInContext < ' a , V > {
581
+ pub fn new ( hir_owner : OwnerId , data : & ' a ItemLocalMap < V > ) -> Self {
582
+ Self { hir_owner, data }
583
+ }
584
+
566
585
pub fn contains_key ( & self , id : HirId ) -> bool {
567
586
validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
568
587
self . data . contains_key ( & id. local_id )
@@ -601,6 +620,10 @@ pub struct LocalTableInContextMut<'a, V> {
601
620
}
602
621
603
622
impl < ' 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
+
604
627
pub fn get_mut ( & mut self , id : HirId ) -> Option < & mut V > {
605
628
validate_hir_id_for_typeck_results ( self . hir_owner , id) ;
606
629
self . data . get_mut ( & id. local_id )
0 commit comments