@@ -447,6 +447,17 @@ fn krate<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) -> NamedRegionMap {
447
447
map
448
448
}
449
449
450
+ /// In traits, there is an implicit `Self` type parameter which comes before the generics.
451
+ /// We have to account for this when computing the index of the other generic parameters.
452
+ /// This function returns whether there is such an implicit parameter defined on the given item.
453
+ fn sub_items_have_self_param ( node : & hir:: ItemKind ) -> bool {
454
+ match * node {
455
+ hir:: ItemKind :: Trait ( ..) |
456
+ hir:: ItemKind :: TraitAlias ( ..) => true ,
457
+ _ => false ,
458
+ }
459
+ }
460
+
450
461
impl < ' a , ' tcx > Visitor < ' tcx > for LifetimeContext < ' a , ' tcx > {
451
462
fn nested_visit_map < ' this > ( & ' this mut self ) -> NestedVisitorMap < ' this , ' tcx > {
452
463
NestedVisitorMap :: All ( & self . tcx . hir )
@@ -522,8 +533,8 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
522
533
hir:: ItemKind :: Impl ( ..) => true ,
523
534
_ => false ,
524
535
} ;
525
- // These kinds of items have only early bound lifetime parameters.
526
- let mut index = if let hir :: ItemKind :: Trait ( .. ) = item. node {
536
+ // These kinds of items have only early- bound lifetime parameters.
537
+ let mut index = if sub_items_have_self_param ( & item. node ) {
527
538
1 // Self comes before lifetimes
528
539
} else {
529
540
0
@@ -1602,8 +1613,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
1602
1613
let mut index = 0 ;
1603
1614
if let Some ( parent_id) = parent_id {
1604
1615
let parent = self . tcx . hir . expect_item ( parent_id) ;
1605
- if let hir :: ItemKind :: Trait ( .. ) = parent. node {
1606
- index += 1 ; // Self comes first.
1616
+ if sub_items_have_self_param ( & parent. node ) {
1617
+ index += 1 ; // Self comes before lifetimes
1607
1618
}
1608
1619
match parent. node {
1609
1620
hir:: ItemKind :: Trait ( _, _, ref generics, ..)
0 commit comments