@@ -167,8 +167,7 @@ fn clean_generic_bound<'tcx>(
167167 let trait_ref = ty:: Binder :: dummy ( ty:: TraitRef :: identity ( cx. tcx , def_id) ) ;
168168
169169 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 {
172171 bug ! ( "clean: parenthesized `GenericBound::LangItemTrait`" ) ;
173172 } ;
174173
@@ -1818,33 +1817,46 @@ fn can_elide_trait_object_lifetime_bound<'tcx>(
18181817#[ derive( Debug ) ]
18191818pub ( crate ) enum ContainerTy < ' tcx > {
18201819 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+ } ,
18221826}
18231827
18241828impl < ' tcx > ContainerTy < ' tcx > {
18251829 fn object_lifetime_default ( self , tcx : TyCtxt < ' tcx > ) -> ObjectLifetimeDefault < ' tcx > {
18261830 match self {
18271831 Self :: Ref ( region) => ObjectLifetimeDefault :: Arg ( region) ,
1828- Self :: Regular { ty : container, substs , arg : index } => {
1832+ Self :: Regular { ty : container, args , has_self , arg : index } => {
18291833 let ( DefKind :: Struct
18301834 | DefKind :: Union
18311835 | 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)
18361838 else {
18371839 return ObjectLifetimeDefault :: Empty ;
18381840 } ;
18391841
18401842 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 ;
18431852
1853+ let default = tcx. object_lifetime_default ( param) ;
18441854 match default {
18451855 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.
18461858 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 ( ) ;
18481860 ObjectLifetimeDefault :: Arg ( arg)
18491861 }
18501862 rbv:: ObjectLifetimeDefault :: Empty => ObjectLifetimeDefault :: Empty ,
0 commit comments