@@ -167,8 +167,7 @@ fn clean_generic_bound<'tcx>(
167
167
let trait_ref = ty:: Binder :: dummy ( ty:: TraitRef :: identity ( cx. tcx , def_id) ) ;
168
168
169
169
let generic_args = clean_generic_args ( generic_args, cx) ;
170
- let GenericArgs :: AngleBracketed { bindings, .. } = generic_args
171
- else {
170
+ let GenericArgs :: AngleBracketed { bindings, .. } = generic_args else {
172
171
bug ! ( "clean: parenthesized `GenericBound::LangItemTrait`" ) ;
173
172
} ;
174
173
@@ -1818,33 +1817,46 @@ fn can_elide_trait_object_lifetime_bound<'tcx>(
1818
1817
#[ derive( Debug ) ]
1819
1818
pub ( crate ) enum ContainerTy < ' tcx > {
1820
1819
Ref ( ty:: Region < ' tcx > ) ,
1821
- Regular { ty : DefId , substs : ty:: Binder < ' tcx , & ' tcx [ ty:: GenericArg < ' tcx > ] > , arg : usize } ,
1820
+ Regular {
1821
+ ty : DefId ,
1822
+ args : ty:: Binder < ' tcx , & ' tcx [ ty:: GenericArg < ' tcx > ] > ,
1823
+ has_self : bool ,
1824
+ arg : usize ,
1825
+ } ,
1822
1826
}
1823
1827
1824
1828
impl < ' tcx > ContainerTy < ' tcx > {
1825
1829
fn object_lifetime_default ( self , tcx : TyCtxt < ' tcx > ) -> ObjectLifetimeDefault < ' tcx > {
1826
1830
match self {
1827
1831
Self :: Ref ( region) => ObjectLifetimeDefault :: Arg ( region) ,
1828
- Self :: Regular { ty : container, substs , arg : index } => {
1832
+ Self :: Regular { ty : container, args , has_self , arg : index } => {
1829
1833
let ( DefKind :: Struct
1830
1834
| DefKind :: Union
1831
1835
| DefKind :: Enum
1832
- | DefKind :: TyAlias
1833
- | DefKind :: Trait
1834
- | DefKind :: AssocTy
1835
- | DefKind :: Variant ) = tcx. def_kind ( container)
1836
+ | DefKind :: TyAlias { .. }
1837
+ | DefKind :: Trait ) = tcx. def_kind ( container)
1836
1838
else {
1837
1839
return ObjectLifetimeDefault :: Empty ;
1838
1840
} ;
1839
1841
1840
1842
let generics = tcx. generics_of ( container) ;
1841
- let param = generics. params [ index] . def_id ;
1842
- let default = tcx. object_lifetime_default ( param) ;
1843
+ debug_assert_eq ! ( generics. parent_count, 0 ) ;
1844
+
1845
+ // If the container is a trait object type, the arguments won't contain the self type but the
1846
+ // generics of the corresponding trait will. In such a case, offset the index by one.
1847
+ // For comparison, if the container is a trait inside a bound, the arguments do contain the
1848
+ // self type.
1849
+ let offset =
1850
+ if !has_self && generics. parent . is_none ( ) && generics. has_self { 1 } else { 0 } ;
1851
+ let param = generics. params [ index + offset] . def_id ;
1843
1852
1853
+ let default = tcx. object_lifetime_default ( param) ;
1844
1854
match default {
1845
1855
rbv:: ObjectLifetimeDefault :: Param ( lifetime) => {
1856
+ // The index is relative to the parent generics but since we don't have any,
1857
+ // we don't need to translate it.
1846
1858
let index = generics. param_def_id_to_index [ & lifetime] ;
1847
- let arg = substs . skip_binder ( ) [ index as usize ] . expect_region ( ) ;
1859
+ let arg = args . skip_binder ( ) [ index as usize ] . expect_region ( ) ;
1848
1860
ObjectLifetimeDefault :: Arg ( arg)
1849
1861
}
1850
1862
rbv:: ObjectLifetimeDefault :: Empty => ObjectLifetimeDefault :: Empty ,
0 commit comments