@@ -175,6 +175,28 @@ impl<'tcx> TyCtxt<'tcx> {
175
175
self . opt_hir_node_by_def_id ( id)
176
176
. unwrap_or_else ( || bug ! ( "couldn't find HIR node for def id {id:?}" ) )
177
177
}
178
+
179
+ /// Returns `HirId` of the parent HIR node of node with this `hir_id`.
180
+ /// Returns the same `hir_id` if and only if `hir_id == CRATE_HIR_ID`.
181
+ ///
182
+ /// If calling repeatedly and iterating over parents, prefer [`Map::parent_iter`].
183
+ pub fn parent_hir_id ( self , hir_id : HirId ) -> HirId {
184
+ let HirId { owner, local_id } = hir_id;
185
+ if local_id == ItemLocalId :: from_u32 ( 0 ) {
186
+ self . hir_owner_parent ( owner)
187
+ } else {
188
+ let parent_local_id = self . hir_owner_nodes ( owner) . nodes [ local_id] . parent ;
189
+ // HIR indexing should have checked that.
190
+ debug_assert_ne ! ( parent_local_id, local_id) ;
191
+ HirId { owner, local_id : parent_local_id }
192
+ }
193
+ }
194
+
195
+ /// Returns parent HIR node of node with this `hir_id`.
196
+ /// Returns HIR node of the same `hir_id` if and only if `hir_id == CRATE_HIR_ID`.
197
+ pub fn parent_hir_node ( self , hir_id : HirId ) -> Node < ' tcx > {
198
+ self . hir_node ( self . parent_hir_id ( hir_id) )
199
+ }
178
200
}
179
201
180
202
impl < ' hir > Map < ' hir > {
@@ -217,37 +239,20 @@ impl<'hir> Map<'hir> {
217
239
self . tcx . definitions_untracked ( ) . def_path_hash ( def_id)
218
240
}
219
241
220
- /// Finds the id of the parent node to this one.
221
- ///
222
- /// If calling repeatedly and iterating over parents, prefer [`Map::parent_iter`].
223
242
pub fn opt_parent_id ( self , id : HirId ) -> Option < HirId > {
224
- if id. local_id == ItemLocalId :: from_u32 ( 0 ) {
225
- // FIXME: This function never returns `None` right now, and the parent chain end is
226
- // determined by checking for `parent(id) == id`. This function should return `None`
227
- // for the crate root instead.
228
- Some ( self . tcx . hir_owner_parent ( id. owner ) )
229
- } else {
230
- let owner = self . tcx . hir_owner_nodes ( id. owner ) ;
231
- let node = & owner. nodes [ id. local_id ] ;
232
- let hir_id = HirId { owner : id. owner , local_id : node. parent } ;
233
- // HIR indexing should have checked that.
234
- debug_assert_ne ! ( id. local_id, node. parent) ;
235
- Some ( hir_id)
236
- }
243
+ Some ( self . tcx . parent_hir_id ( id) )
237
244
}
238
245
239
- #[ track_caller]
240
246
pub fn parent_id ( self , hir_id : HirId ) -> HirId {
241
- self . opt_parent_id ( hir_id)
242
- . unwrap_or_else ( || bug ! ( "No parent for node {}" , self . node_to_string( hir_id) ) )
247
+ self . tcx . parent_hir_id ( hir_id)
243
248
}
244
249
245
250
pub fn get_parent ( self , hir_id : HirId ) -> Node < ' hir > {
246
- self . tcx . hir_node ( self . parent_id ( hir_id) )
251
+ self . tcx . parent_hir_node ( hir_id)
247
252
}
248
253
249
254
pub fn find_parent ( self , hir_id : HirId ) -> Option < Node < ' hir > > {
250
- Some ( self . tcx . hir_node ( self . opt_parent_id ( hir_id) ? ) )
255
+ Some ( self . tcx . parent_hir_node ( hir_id) )
251
256
}
252
257
253
258
pub fn get_if_local ( self , id : DefId ) -> Option < Node < ' hir > > {
0 commit comments