@@ -1809,21 +1809,30 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18091809
18101810 debug ! ( ?captures) ;
18111811
1812- self . with_hir_id_owner ( opaque_ty_node_id, |this| {
1813- let lifetimes_in_bounds =
1814- lifetime_collector:: lifetimes_in_ret_ty ( & this. resolver , output) ;
1815- debug ! ( ?lifetimes_in_bounds) ;
1812+ // We only want to capture the lifetimes that appear in the bounds. So visit the bounds to
1813+ // find out exactly which ones those are.
1814+ // in fn return position, like the `fn test<'a>() -> impl Debug + 'a` example,
1815+ // we only keep the lifetimes that appear in the `impl Debug` itself:
1816+ let lifetimes_to_remap = lifetime_collector:: lifetimes_in_ret_ty ( & self . resolver , output) ;
1817+ debug ! ( ?lifetimes_to_remap) ;
18161818
1819+ self . with_hir_id_owner ( opaque_ty_node_id, |this| {
1820+ // If this opaque type is only capturing a subset of the lifetimes (those that appear
1821+ // in bounds), then create the new lifetime parameters required and create a mapping
1822+ // from the old `'a` (on the function) to the new `'a` (on the opaque type).
18171823 captures. extend (
18181824 this. create_lifetime_defs (
18191825 opaque_ty_def_id,
1820- & lifetimes_in_bounds ,
1826+ & lifetimes_to_remap ,
18211827 & mut new_remapping,
18221828 )
18231829 . into_iter ( )
18241830 . map ( |( new_node_id, lifetime) | ( new_node_id, lifetime, None ) ) ,
18251831 ) ;
1832+ debug ! ( ?captures) ;
1833+ debug ! ( ?new_remapping) ;
18261834
1835+ // Install the remapping from old to new (if any):
18271836 this. with_remapping ( new_remapping, |this| {
18281837 // We have to be careful to get elision right here. The
18291838 // idea is that we create a lifetime parameter for each
0 commit comments