@@ -290,11 +290,28 @@ pub struct ClosureSizeProfileData<'tcx> {
290
290
}
291
291
292
292
pub trait DefIdTree : Copy {
293
- fn parent ( self , id : DefId ) -> Option < DefId > ;
293
+ fn opt_parent ( self , id : DefId ) -> Option < DefId > ;
294
294
295
295
#[ inline]
296
- fn local_parent ( self , id : LocalDefId ) -> Option < LocalDefId > {
297
- Some ( self . parent ( id. to_def_id ( ) ) ?. expect_local ( ) )
296
+ #[ track_caller]
297
+ fn parent ( self , id : DefId ) -> DefId {
298
+ match self . opt_parent ( id) {
299
+ Some ( id) => id,
300
+ // not `unwrap_or_else` to avoid breaking caller tracking
301
+ None => bug ! ( "{id:?} doesn't have a parent" ) ,
302
+ }
303
+ }
304
+
305
+ #[ inline]
306
+ #[ track_caller]
307
+ fn opt_local_parent ( self , id : LocalDefId ) -> Option < LocalDefId > {
308
+ self . opt_parent ( id. to_def_id ( ) ) . map ( DefId :: expect_local)
309
+ }
310
+
311
+ #[ inline]
312
+ #[ track_caller]
313
+ fn local_parent ( self , id : LocalDefId ) -> LocalDefId {
314
+ self . parent ( id. to_def_id ( ) ) . expect_local ( )
298
315
}
299
316
300
317
fn is_descendant_of ( self , mut descendant : DefId , ancestor : DefId ) -> bool {
@@ -303,7 +320,7 @@ pub trait DefIdTree: Copy {
303
320
}
304
321
305
322
while descendant != ancestor {
306
- match self . parent ( descendant) {
323
+ match self . opt_parent ( descendant) {
307
324
Some ( parent) => descendant = parent,
308
325
None => return false ,
309
326
}
@@ -313,7 +330,8 @@ pub trait DefIdTree: Copy {
313
330
}
314
331
315
332
impl < ' tcx > DefIdTree for TyCtxt < ' tcx > {
316
- fn parent ( self , id : DefId ) -> Option < DefId > {
333
+ #[ inline]
334
+ fn opt_parent ( self , id : DefId ) -> Option < DefId > {
317
335
self . def_key ( id) . parent . map ( |index| DefId { index, ..id } )
318
336
}
319
337
}
@@ -2123,17 +2141,17 @@ impl<'tcx> TyCtxt<'tcx> {
2123
2141
pub fn expect_variant_res ( self , res : Res ) -> & ' tcx VariantDef {
2124
2142
match res {
2125
2143
Res :: Def ( DefKind :: Variant , did) => {
2126
- let enum_did = self . parent ( did) . unwrap ( ) ;
2144
+ let enum_did = self . parent ( did) ;
2127
2145
self . adt_def ( enum_did) . variant_with_id ( did)
2128
2146
}
2129
2147
Res :: Def ( DefKind :: Struct | DefKind :: Union , did) => self . adt_def ( did) . non_enum_variant ( ) ,
2130
2148
Res :: Def ( DefKind :: Ctor ( CtorOf :: Variant , ..) , variant_ctor_did) => {
2131
- let variant_did = self . parent ( variant_ctor_did) . unwrap ( ) ;
2132
- let enum_did = self . parent ( variant_did) . unwrap ( ) ;
2149
+ let variant_did = self . parent ( variant_ctor_did) ;
2150
+ let enum_did = self . parent ( variant_did) ;
2133
2151
self . adt_def ( enum_did) . variant_with_ctor_id ( variant_ctor_did)
2134
2152
}
2135
2153
Res :: Def ( DefKind :: Ctor ( CtorOf :: Struct , ..) , ctor_did) => {
2136
- let struct_did = self . parent ( ctor_did) . expect ( "struct ctor has no parent" ) ;
2154
+ let struct_did = self . parent ( ctor_did) ;
2137
2155
self . adt_def ( struct_did) . non_enum_variant ( )
2138
2156
}
2139
2157
_ => bug ! ( "expect_variant_res used with unexpected res {:?}" , res) ,
0 commit comments