@@ -61,7 +61,7 @@ pub struct ParentHirIterator<'hir> {
61
61
}
62
62
63
63
impl < ' hir > Iterator for ParentHirIterator < ' hir > {
64
- type Item = HirId ;
64
+ type Item = ( HirId , Node < ' hir > ) ;
65
65
66
66
fn next ( & mut self ) -> Option < Self :: Item > {
67
67
if self . current_id == CRATE_HIR_ID {
@@ -77,7 +77,10 @@ impl<'hir> Iterator for ParentHirIterator<'hir> {
77
77
}
78
78
79
79
self . current_id = parent_id;
80
- return Some ( parent_id) ;
80
+ if let Some ( node) = self . map . find ( parent_id) {
81
+ return Some ( ( parent_id, node) ) ;
82
+ }
83
+ // If this `HirId` doesn't have an entry, skip it and look for its `parent_id`.
81
84
}
82
85
}
83
86
}
@@ -390,8 +393,8 @@ impl<'hir> Map<'hir> {
390
393
}
391
394
392
395
pub fn enclosing_body_owner ( self , hir_id : HirId ) -> LocalDefId {
393
- for ( _ , node ) in self . parent_iter ( hir_id) {
394
- if let Some ( body) = associated_body ( node ) {
396
+ for ( parent , _ ) in self . parent_iter ( hir_id) {
397
+ if let Some ( body) = self . find ( parent ) . map ( associated_body) . flatten ( ) {
395
398
return self . body_owner_def_id ( body) ;
396
399
}
397
400
}
@@ -632,17 +635,10 @@ impl<'hir> Map<'hir> {
632
635
/// Returns an iterator for the nodes in the ancestor tree of the `current_id`
633
636
/// until the crate root is reached. Prefer this over your own loop using `get_parent_node`.
634
637
#[ inline]
635
- pub fn parent_id_iter ( self , current_id : HirId ) -> impl Iterator < Item = HirId > + ' hir {
638
+ pub fn parent_iter ( self , current_id : HirId ) -> ParentHirIterator < ' hir > {
636
639
ParentHirIterator { current_id, map : self }
637
640
}
638
641
639
- /// Returns an iterator for the nodes in the ancestor tree of the `current_id`
640
- /// until the crate root is reached. Prefer this over your own loop using `get_parent_node`.
641
- #[ inline]
642
- pub fn parent_iter ( self , current_id : HirId ) -> impl Iterator < Item = ( HirId , Node < ' hir > ) > {
643
- self . parent_id_iter ( current_id) . filter_map ( move |id| Some ( ( id, self . find ( id) ?) ) )
644
- }
645
-
646
642
/// Returns an iterator for the nodes in the ancestor tree of the `current_id`
647
643
/// until the crate root is reached. Prefer this over your own loop using `get_parent_node`.
648
644
#[ inline]
0 commit comments