@@ -1539,9 +1539,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1539
1539
) ;
1540
1540
debug ! ( ?opaque_ty_def_id) ;
1541
1541
1542
- // Contains the new lifetime definitions created for the TAIT (if any).
1543
- let mut collected_lifetimes = Vec :: new ( ) ;
1544
-
1545
1542
// If this came from a TAIT (as opposed to a function that returns an RPIT), we only want
1546
1543
// to capture the lifetimes that appear in the bounds. So visit the bounds to find out
1547
1544
// exactly which ones those are.
@@ -1558,20 +1555,31 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1558
1555
} ;
1559
1556
debug ! ( ?lifetimes_to_remap) ;
1560
1557
1561
- self . with_hir_id_owner ( opaque_ty_node_id, |lctx| {
1562
- let mut new_remapping = FxHashMap :: default ( ) ;
1558
+ let mut new_remapping = FxHashMap :: default ( ) ;
1563
1559
1564
- // If this opaque type is only capturing a subset of the lifetimes (those that appear
1565
- // in bounds), then create the new lifetime parameters required and create a mapping
1566
- // from the old `'a` (on the function) to the new `'a` (on the opaque type).
1567
- collected_lifetimes = lctx. create_lifetime_defs (
1568
- opaque_ty_def_id,
1569
- & lifetimes_to_remap,
1570
- & mut new_remapping,
1571
- ) ;
1572
- debug ! ( ?collected_lifetimes) ;
1573
- debug ! ( ?new_remapping) ;
1560
+ // Contains the new lifetime definitions created for the TAIT (if any).
1561
+ // If this opaque type is only capturing a subset of the lifetimes (those that appear in
1562
+ // bounds), then create the new lifetime parameters required and create a mapping from the
1563
+ // old `'a` (on the function) to the new `'a` (on the opaque type).
1564
+ let collected_lifetimes =
1565
+ self . create_lifetime_defs ( opaque_ty_def_id, & lifetimes_to_remap, & mut new_remapping) ;
1566
+ debug ! ( ?collected_lifetimes) ;
1567
+ debug ! ( ?new_remapping) ;
1568
+
1569
+ // This creates HIR lifetime arguments as `hir::GenericArg`, in the given example `type
1570
+ // TestReturn<'a, T, 'x> = impl Debug + 'x`, it creates a collection containing `&['x]`.
1571
+ let collected_lifetime_mapping: Vec < _ > = collected_lifetimes
1572
+ . iter ( )
1573
+ . map ( |( node_id, lifetime) | {
1574
+ let id = self . next_node_id ( ) ;
1575
+ let lifetime = self . new_named_lifetime ( lifetime. id , id, lifetime. ident ) ;
1576
+ let def_id = self . local_def_id ( * node_id) ;
1577
+ ( lifetime, def_id)
1578
+ } )
1579
+ . collect ( ) ;
1580
+ debug ! ( ?collected_lifetime_mapping) ;
1574
1581
1582
+ self . with_hir_id_owner ( opaque_ty_node_id, |lctx| {
1575
1583
// Install the remapping from old to new (if any):
1576
1584
lctx. with_remapping ( new_remapping, |lctx| {
1577
1585
// This creates HIR lifetime definitions as `hir::GenericParam`, in the given
@@ -1610,6 +1618,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1610
1618
let hir_bounds = lctx. lower_param_bounds ( bounds, itctx) ;
1611
1619
debug ! ( ?hir_bounds) ;
1612
1620
1621
+ let lifetime_mapping = if in_trait {
1622
+ self . arena . alloc_from_iter (
1623
+ collected_lifetime_mapping
1624
+ . iter ( )
1625
+ . map ( |( lifetime, def_id) | ( * * lifetime, * def_id) ) ,
1626
+ )
1627
+ } else {
1628
+ & mut [ ]
1629
+ } ;
1630
+
1613
1631
let opaque_ty_item = hir:: OpaqueTy {
1614
1632
generics : self . arena . alloc ( hir:: Generics {
1615
1633
params : lifetime_defs,
@@ -1620,6 +1638,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1620
1638
} ) ,
1621
1639
bounds : hir_bounds,
1622
1640
origin,
1641
+ lifetime_mapping,
1623
1642
in_trait,
1624
1643
} ;
1625
1644
debug ! ( ?opaque_ty_item) ;
@@ -1628,20 +1647,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1628
1647
} )
1629
1648
} ) ;
1630
1649
1631
- // This creates HIR lifetime arguments as `hir::GenericArg`, in the given example `type
1632
- // TestReturn<'a, T, 'x> = impl Debug + 'x`, it creates a collection containing `&['x]`.
1633
- let lifetimes =
1634
- self . arena . alloc_from_iter ( collected_lifetimes. into_iter ( ) . map ( |( _, lifetime) | {
1635
- let id = self . next_node_id ( ) ;
1636
- let l = self . new_named_lifetime ( lifetime. id , id, lifetime. ident ) ;
1637
- hir:: GenericArg :: Lifetime ( l)
1638
- } ) ) ;
1639
- debug ! ( ?lifetimes) ;
1640
-
1641
1650
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
1642
1651
hir:: TyKind :: OpaqueDef (
1643
1652
hir:: ItemId { owner_id : hir:: OwnerId { def_id : opaque_ty_def_id } } ,
1644
- lifetimes,
1653
+ self . arena . alloc_from_iter (
1654
+ collected_lifetime_mapping
1655
+ . iter ( )
1656
+ . map ( |( lifetime, _) | hir:: GenericArg :: Lifetime ( * lifetime) ) ,
1657
+ ) ,
1645
1658
in_trait,
1646
1659
)
1647
1660
}
@@ -1655,7 +1668,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1655
1668
span : Span ,
1656
1669
opaque_ty_span : Span ,
1657
1670
) -> hir:: OwnerNode < ' hir > {
1658
- let opaque_ty_item_kind = hir:: ItemKind :: OpaqueTy ( opaque_ty_item) ;
1671
+ let opaque_ty_item_kind = hir:: ItemKind :: OpaqueTy ( self . arena . alloc ( opaque_ty_item) ) ;
1659
1672
// Generate an `type Foo = impl Trait;` declaration.
1660
1673
trace ! ( "registering opaque type with id {:#?}" , opaque_ty_id) ;
1661
1674
let opaque_ty_item = hir:: Item {
@@ -1983,7 +1996,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1983
1996
let lifetime = Lifetime { id : outer_node_id, ident } ;
1984
1997
collected_lifetimes. push ( ( inner_node_id, lifetime, Some ( inner_res) ) ) ;
1985
1998
}
1986
-
1987
1999
debug ! ( ?collected_lifetimes) ;
1988
2000
1989
2001
// We only want to capture the lifetimes that appear in the bounds. So visit the bounds to
@@ -1993,22 +2005,36 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1993
2005
let lifetimes_to_remap = lifetime_collector:: lifetimes_in_ret_ty ( & self . resolver , output) ;
1994
2006
debug ! ( ?lifetimes_to_remap) ;
1995
2007
1996
- self . with_hir_id_owner ( opaque_ty_node_id, |this| {
1997
- // If this opaque type is only capturing a subset of the lifetimes (those that appear
1998
- // in bounds), then create the new lifetime parameters required and create a mapping
1999
- // from the old `'a` (on the function) to the new `'a` (on the opaque type).
2000
- collected_lifetimes. extend (
2001
- this. create_lifetime_defs (
2002
- opaque_ty_def_id,
2003
- & lifetimes_to_remap,
2004
- & mut new_remapping,
2005
- )
2008
+ // If this opaque type is only capturing a subset of the lifetimes (those that appear in
2009
+ // bounds), then create the new lifetime parameters required and create a mapping from the
2010
+ // old `'a` (on the function) to the new `'a` (on the opaque type).
2011
+ collected_lifetimes. extend (
2012
+ self . create_lifetime_defs ( opaque_ty_def_id, & lifetimes_to_remap, & mut new_remapping)
2006
2013
. into_iter ( )
2007
2014
. map ( |( new_node_id, lifetime) | ( new_node_id, lifetime, None ) ) ,
2008
- ) ;
2009
- debug ! ( ?collected_lifetimes) ;
2010
- debug ! ( ?new_remapping) ;
2015
+ ) ;
2016
+ debug ! ( ?collected_lifetimes) ;
2017
+ debug ! ( ?new_remapping) ;
2018
+
2019
+ // This creates pairs of HIR lifetimes and def_ids. In the given example `type
2020
+ // TestReturn<'a, T, 'x> = impl Debug + 'x`, it creates a collection containing the
2021
+ // new lifetime of the RPIT 'x and the def_id of the lifetime 'x corresponding to
2022
+ // `TestReturn`.
2023
+ let collected_lifetime_mapping: Vec < _ > = collected_lifetimes
2024
+ . iter ( )
2025
+ . map ( |( node_id, lifetime, res) | {
2026
+ let id = self . next_node_id ( ) ;
2027
+ let res = res. unwrap_or (
2028
+ self . resolver . get_lifetime_res ( lifetime. id ) . unwrap_or ( LifetimeRes :: Error ) ,
2029
+ ) ;
2030
+ let lifetime = self . new_named_lifetime_with_res ( id, lifetime. ident , res) ;
2031
+ let def_id = self . local_def_id ( * node_id) ;
2032
+ ( lifetime, def_id)
2033
+ } )
2034
+ . collect ( ) ;
2035
+ debug ! ( ?collected_lifetime_mapping) ;
2011
2036
2037
+ self . with_hir_id_owner ( opaque_ty_node_id, |this| {
2012
2038
// Install the remapping from old to new (if any):
2013
2039
this. with_remapping ( new_remapping, |this| {
2014
2040
// We have to be careful to get elision right here. The
@@ -2063,6 +2089,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2063
2089
) ) ;
2064
2090
debug ! ( "lower_async_fn_ret_ty: generic_params={:#?}" , generic_params) ;
2065
2091
2092
+ let lifetime_mapping = if in_trait {
2093
+ self . arena . alloc_from_iter (
2094
+ collected_lifetime_mapping
2095
+ . iter ( )
2096
+ . map ( |( lifetime, def_id) | ( * * lifetime, * def_id) ) ,
2097
+ )
2098
+ } else {
2099
+ & mut [ ]
2100
+ } ;
2101
+
2066
2102
let opaque_ty_item = hir:: OpaqueTy {
2067
2103
generics : this. arena . alloc ( hir:: Generics {
2068
2104
params : generic_params,
@@ -2073,6 +2109,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2073
2109
} ) ,
2074
2110
bounds : arena_vec ! [ this; future_bound] ,
2075
2111
origin : hir:: OpaqueTyOrigin :: AsyncFn ( fn_def_id) ,
2112
+ lifetime_mapping,
2076
2113
in_trait,
2077
2114
} ;
2078
2115
@@ -2096,15 +2133,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2096
2133
//
2097
2134
// For the "output" lifetime parameters, we just want to
2098
2135
// generate `'_`.
2099
- let generic_args = self . arena . alloc_from_iter ( collected_lifetimes. into_iter ( ) . map (
2100
- |( _, lifetime, res) | {
2101
- let id = self . next_node_id ( ) ;
2102
- let res = res. unwrap_or (
2103
- self . resolver . get_lifetime_res ( lifetime. id ) . unwrap_or ( LifetimeRes :: Error ) ,
2104
- ) ;
2105
- hir:: GenericArg :: Lifetime ( self . new_named_lifetime_with_res ( id, lifetime. ident , res) )
2106
- } ,
2107
- ) ) ;
2136
+ let generic_args = self . arena . alloc_from_iter (
2137
+ collected_lifetime_mapping
2138
+ . iter ( )
2139
+ . map ( |( lifetime, _) | hir:: GenericArg :: Lifetime ( * lifetime) ) ,
2140
+ ) ;
2108
2141
2109
2142
// Create the `Foo<...>` reference itself. Note that the `type
2110
2143
// Foo = impl Trait` is, internally, created as a child of the
0 commit comments