@@ -264,7 +264,15 @@ impl ExpnId {
264
264
HygieneData :: with ( |data| data. expn_data ( self ) . clone ( ) )
265
265
}
266
266
267
+ #[ inline]
267
268
pub fn is_descendant_of ( self , ancestor : ExpnId ) -> bool {
269
+ // a few "fast path" cases to avoid locking HygieneData
270
+ if ancestor == ExpnId :: root ( ) || ancestor == self {
271
+ return true ;
272
+ }
273
+ if ancestor. krate != self . krate {
274
+ return false ;
275
+ }
268
276
HygieneData :: with ( |data| data. is_descendant_of ( self , ancestor) )
269
277
}
270
278
@@ -376,13 +384,22 @@ impl HygieneData {
376
384
}
377
385
378
386
fn is_descendant_of ( & self , mut expn_id : ExpnId , ancestor : ExpnId ) -> bool {
379
- while expn_id != ancestor {
387
+ // a couple "fast path" cases to avoid traversing parents in the loop below
388
+ if ancestor == ExpnId :: root ( ) {
389
+ return true ;
390
+ }
391
+ if expn_id. krate != ancestor. krate {
392
+ return false ;
393
+ }
394
+ loop {
395
+ if expn_id == ancestor {
396
+ return true ;
397
+ }
380
398
if expn_id == ExpnId :: root ( ) {
381
399
return false ;
382
400
}
383
401
expn_id = self . expn_data ( expn_id) . parent ;
384
402
}
385
- true
386
403
}
387
404
388
405
fn normalize_to_macros_2_0 ( & self , ctxt : SyntaxContext ) -> SyntaxContext {
@@ -1223,6 +1240,7 @@ pub fn register_expn_id(
1223
1240
data : ExpnData ,
1224
1241
hash : ExpnHash ,
1225
1242
) -> ExpnId {
1243
+ debug_assert ! ( data. parent == ExpnId :: root( ) || krate == data. parent. krate) ;
1226
1244
let expn_id = ExpnId { krate, local_id } ;
1227
1245
HygieneData :: with ( |hygiene_data| {
1228
1246
let _old_data = hygiene_data. foreign_expn_data . insert ( expn_id, data) ;
0 commit comments