@@ -1400,27 +1400,32 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14001400 let mut collected_lifetimes = Vec :: new ( ) ;
14011401 let mut new_remapping = FxHashMap :: default ( ) ;
14021402
1403+ // If this came from a TAIT (as opposed to a function that returns an RPIT), we only want
1404+ // to capture the lifetimes that appear in the bounds. So visit the bounds to find out
1405+ // exactly which ones those are.
1406+ let lifetimes_to_remap = if origin == hir:: OpaqueTyOrigin :: TyAlias {
1407+ // in a TAIT like `type Foo<'a> = impl Foo<'a>`, we don't keep all the lifetime parameters
1408+ Vec :: new ( )
1409+ } else {
1410+ // in fn return position, like the `fn test<'a>() -> impl Debug + 'a` example,
1411+ // we only keep the lifetimes that appear in the `impl Debug` itself:
1412+ lifetime_collector:: lifetimes_in_bounds ( & self . resolver , bounds)
1413+ } ;
1414+ debug ! ( ?lifetimes_to_remap) ;
1415+
14031416 self . with_hir_id_owner ( opaque_ty_node_id, |lctx| {
1404- if origin != hir:: OpaqueTyOrigin :: TyAlias {
1405- // When lowering `fn foo<'a>() -> impl Debug + 'a`, the `lifetime_collector` finds
1406- // the set of lifetimes that appear in the bounds (in this case, 'a) and returns
1407- // that set in the variable lifetimes_in_bounds.
1408- let lifetimes_in_bounds =
1409- lifetime_collector:: lifetimes_in_bounds ( & lctx. resolver , bounds) ;
1410- debug ! ( ?lifetimes_in_bounds) ;
1411-
1412- // For each captured lifetime (e.g., 'a), we create a new lifetime parameter that
1413- // is a generic defined on the TAIT, so we have type Foo<'a1> = ... and we
1414- // establish a mapping from the original parameter 'a to the new parameter 'a1.
1415- collected_lifetimes = lctx. create_lifetime_defs (
1416- opaque_ty_def_id,
1417- & lifetimes_in_bounds,
1418- & mut new_remapping,
1419- ) ;
1420- } ;
1421- debug ! ( ?new_remapping) ;
1417+ // If this opaque type is only capturing a subset of the lifetimes (those that appear
1418+ // in bounds), then create the new lifetime parameters required and create a mapping
1419+ // from the old `'a` (on the function) to the new `'a` (on the opaque type).
1420+ collected_lifetimes = lctx. create_lifetime_defs (
1421+ opaque_ty_def_id,
1422+ & lifetimes_to_remap,
1423+ & mut new_remapping,
1424+ ) ;
14221425 debug ! ( ?collected_lifetimes) ;
1426+ debug ! ( ?new_remapping) ;
14231427
1428+ // Install the remapping from old to new (if any):
14241429 lctx. with_remapping ( new_remapping, |lctx| {
14251430 // Then when we lower the param bounds, references to 'a are remapped to 'a1, so we
14261431 // get back Debug + 'a1, which is suitable for use on the TAIT.
0 commit comments