@@ -1809,21 +1809,30 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1809
1809
1810
1810
debug ! ( ?captures) ;
1811
1811
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) ;
1816
1818
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).
1817
1823
captures. extend (
1818
1824
this. create_lifetime_defs (
1819
1825
opaque_ty_def_id,
1820
- & lifetimes_in_bounds ,
1826
+ & lifetimes_to_remap ,
1821
1827
& mut new_remapping,
1822
1828
)
1823
1829
. into_iter ( )
1824
1830
. map ( |( new_node_id, lifetime) | ( new_node_id, lifetime, None ) ) ,
1825
1831
) ;
1832
+ debug ! ( ?captures) ;
1833
+ debug ! ( ?new_remapping) ;
1826
1834
1835
+ // Install the remapping from old to new (if any):
1827
1836
this. with_remapping ( new_remapping, |this| {
1828
1837
// We have to be careful to get elision right here. The
1829
1838
// idea is that we create a lifetime parameter for each
0 commit comments