@@ -506,13 +506,6 @@ fn sanity_check_found_hidden_type<'tcx>(
506506 } ) ;
507507 // Get the hidden type.
508508 let mut hidden_ty = tcx. type_of ( key. def_id ) . instantiate ( tcx, key. args ) ;
509- // In case it is in a nested opaque type, find that opaque type's
510- // usage in the function signature and use the generic arguments from the usage site.
511- // We need to do because RPITs ignore the lifetimes of the function,
512- // as they have their own copies of all the lifetimes they capture.
513- // So the only way to get the lifetimes represented in terms of the function,
514- // is to look how they are used in the function signature (or do some other fancy
515- // recording of this mapping at ast -> hir lowering time).
516509 if let hir:: OpaqueTyOrigin :: FnReturn ( ..) | hir:: OpaqueTyOrigin :: AsyncFn ( ..) = origin {
517510 if hidden_ty != ty. ty {
518511 hidden_ty = find_and_apply_rpit_args (
@@ -534,6 +527,36 @@ fn sanity_check_found_hidden_type<'tcx>(
534527 }
535528}
536529
530+ /// In case it is in a nested opaque type, find that opaque type's
531+ /// usage in the function signature and use the generic arguments from the usage site.
532+ /// We need to do because RPITs ignore the lifetimes of the function,
533+ /// as they have their own copies of all the lifetimes they capture.
534+ /// So the only way to get the lifetimes represented in terms of the function,
535+ /// is to look how they are used in the function signature (or do some other fancy
536+ /// recording of this mapping at ast -> hir lowering time).
537+ ///
538+ /// As an example:
539+ /// ```text
540+ /// trait Id {
541+ /// type Assoc;
542+ /// }
543+ /// impl<'a> Id for &'a () {
544+ /// type Assoc = &'a ();
545+ /// }
546+ /// fn func<'a>(x: &'a ()) -> impl Id<Assoc = impl Sized + 'a> { x }
547+ /// // desugared to
548+ ///
549+ /// // hidden type is `&'bDup ()`
550+ /// // During wfcheck the hidden type of `Inner` is `&'a ()`, but
551+ /// // `typeof(Inner<'b, 'bDup>) = &'bDup ()`.
552+ /// // So we walk the signature of `func` to find the use of `Inner<'static, 'a>`
553+ /// // and then use that to replace the lifetimes in the hidden type, obtaining
554+ /// // `&'a ()`.
555+ /// type Outer<'b, 'bDup> = impl Id<Assoc = Inner<'b, 'bDup>>;
556+ /// // hidden type is `&'cDup ()`
557+ /// type Inner<'c, 'cDup> = impl Sized + 'cDup;
558+ /// fn func<'a>(x: &'a () -> Outer<'static, 'a> { x }
559+ /// ```
537560fn find_and_apply_rpit_args < ' tcx > (
538561 tcx : TyCtxt < ' tcx > ,
539562 mut hidden_ty : Ty < ' tcx > ,
0 commit comments