@@ -10,7 +10,7 @@ use rustc_middle::ty::{
1010 self , EarlyBinder , GenericPredicates , Ty , TyCtxt , TypeFoldable , TypeFolder , TypeSuperFoldable ,
1111 TypeVisitableExt ,
1212} ;
13- use rustc_span:: { ErrorGuaranteed , Span } ;
13+ use rustc_span:: { ErrorGuaranteed , Span , kw } ;
1414
1515type RemapTable = FxHashMap < u32 , u32 > ;
1616
@@ -185,7 +185,7 @@ fn create_mapping<'tcx>(
185185 args_index += 1 ;
186186 }
187187
188- // If self after lifetimes insert mapping, relying that self is at 0 in sig parent
188+ // If self after lifetimes insert mapping, relying that self is at 0 in sig parent.
189189 if matches ! ( self_pos_kind, SelfPositionKind :: AfterLifetimes ) {
190190 mapping. insert ( 0 , args_index as u32 ) ;
191191 args_index += 1 ;
@@ -277,35 +277,25 @@ pub(crate) fn get_delegation_self_ty<'tcx>(
277277 let ( caller_kind, callee_kind) = get_caller_and_callee_kind ( tcx, delegation_id, sig_id) ;
278278
279279 match ( caller_kind, callee_kind) {
280- ( FnKind :: Free , FnKind :: Free ) => None ,
281-
282280 ( FnKind :: Free , FnKind :: AssocTrait )
283281 | ( FnKind :: AssocInherentImpl , FnKind :: Free )
282+ | ( FnKind :: Free , FnKind :: Free )
284283 | ( FnKind :: AssocTrait , FnKind :: Free )
285284 | ( FnKind :: AssocTrait , FnKind :: AssocTrait ) => {
286285 match create_self_position_kind ( caller_kind, callee_kind) {
287286 SelfPositionKind :: None => None ,
288287 SelfPositionKind :: AfterLifetimes => {
289- ty:: GenericArgs :: identity_for_item ( tcx, delegation_id)
290- . iter ( )
291- . skip_while ( |a| a. as_region ( ) . is_some ( ) )
292- . next ( )
293- . and_then ( |a| a. as_type ( ) )
288+ // Both sig parent and child lifetimes are in included in this count.
289+ Some ( tcx. generics_of ( delegation_id) . own_counts ( ) . lifetimes )
294290 }
295- SelfPositionKind :: Zero => ty:: GenericArgs :: identity_for_item ( tcx, delegation_id)
296- . first ( )
297- . and_then ( |a| a. as_type ( ) ) ,
291+ SelfPositionKind :: Zero => Some ( 0 ) ,
298292 }
293+ . map ( |self_index| Ty :: new_param ( tcx, self_index as u32 , kw:: SelfUpper ) )
299294 }
300295
301- ( FnKind :: AssocTraitImpl , FnKind :: AssocTrait ) => {
302- create_trait_impl_to_trait_parent_args ( tcx, delegation_id)
303- . first ( )
304- . and_then ( |a| a. as_type ( ) )
305- }
306-
307- ( FnKind :: AssocInherentImpl , FnKind :: AssocTrait ) => {
308- Some ( create_inherent_impl_to_trait_self_ty ( tcx, delegation_id) )
296+ ( FnKind :: AssocTraitImpl , FnKind :: AssocTrait )
297+ | ( FnKind :: AssocInherentImpl , FnKind :: AssocTrait ) => {
298+ Some ( create_parent_self_ty ( tcx, delegation_id) )
309299 }
310300
311301 unsupported_caller_callee_kinds ! ( ) => unreachable ! ( ) ,
@@ -320,10 +310,7 @@ fn create_trait_impl_to_trait_parent_args<'tcx>(
320310 tcx. impl_trait_header ( parent) . trait_ref . instantiate_identity ( ) . args
321311}
322312
323- fn create_inherent_impl_to_trait_self_ty < ' tcx > (
324- tcx : TyCtxt < ' tcx > ,
325- delegation_id : LocalDefId ,
326- ) -> Ty < ' tcx > {
313+ fn create_parent_self_ty < ' tcx > ( tcx : TyCtxt < ' tcx > , delegation_id : LocalDefId ) -> Ty < ' tcx > {
327314 let parent = tcx. parent ( delegation_id. into ( ) ) ;
328315 tcx. type_of ( parent) . instantiate_identity ( )
329316}
@@ -373,7 +360,7 @@ fn create_generic_args<'tcx>(
373360 }
374361
375362 ( FnKind :: AssocInherentImpl , FnKind :: AssocTrait ) => {
376- let self_ty = create_inherent_impl_to_trait_self_ty ( tcx, delegation_id) ;
363+ let self_ty = create_parent_self_ty ( tcx, delegation_id) ;
377364
378365 tcx. mk_args_from_iter (
379366 std:: iter:: once ( ty:: GenericArg :: from ( self_ty) ) . chain ( delegation_args. iter ( ) ) ,
@@ -436,20 +423,18 @@ fn create_generic_args<'tcx>(
436423 }
437424
438425 new_args. extend_from_slice ( & child_args[ child_lifetimes_count..] ) ;
439- } else {
440- if !parent_args. is_empty ( ) {
441- let child_args = & delegation_args[ delegation_parent_args_count..] ;
426+ } else if !parent_args. is_empty ( ) {
427+ let child_args = & delegation_args[ delegation_parent_args_count..] ;
442428
443- let child_lifetimes_count =
444- child_args. iter ( ) . take_while ( |a| a. as_region ( ) . is_some ( ) ) . count ( ) ;
445-
446- for i in 0 ..child_lifetimes_count {
447- new_args. insert ( lifetimes_end_pos + i, child_args[ i] ) ;
448- }
429+ let child_lifetimes_count =
430+ child_args. iter ( ) . take_while ( |a| a. as_region ( ) . is_some ( ) ) . count ( ) ;
449431
450- let skip_self = matches ! ( self_pos_kind , SelfPositionKind :: AfterLifetimes ) ;
451- new_args. extend ( & child_args [ child_lifetimes_count + skip_self as usize .. ] ) ;
432+ for i in 0 ..child_lifetimes_count {
433+ new_args. insert ( lifetimes_end_pos + i , child_args [ i ] ) ;
452434 }
435+
436+ let skip_self = matches ! ( self_pos_kind, SelfPositionKind :: AfterLifetimes ) ;
437+ new_args. extend ( & child_args[ child_lifetimes_count + skip_self as usize ..] ) ;
453438 }
454439
455440 new_args
0 commit comments