@@ -236,7 +236,7 @@ enum ImplTraitContext {
236
236
ReturnPositionOpaqueTy {
237
237
/// Origin: Either OpaqueTyOrigin::FnReturn or OpaqueTyOrigin::AsyncFn,
238
238
origin : hir:: OpaqueTyOrigin ,
239
- in_trait : bool ,
239
+ fn_kind : FnDeclKind ,
240
240
} ,
241
241
/// Impl trait in type aliases.
242
242
TypeAliasesOpaqueTy { in_assoc_ty : bool } ,
@@ -312,7 +312,7 @@ impl std::fmt::Display for ImplTraitPosition {
312
312
}
313
313
}
314
314
315
- #[ derive( Debug , PartialEq , Eq ) ]
315
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
316
316
enum FnDeclKind {
317
317
Fn ,
318
318
Inherent ,
@@ -1401,13 +1401,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1401
1401
TyKind :: ImplTrait ( def_node_id, bounds) => {
1402
1402
let span = t. span ;
1403
1403
match itctx {
1404
- ImplTraitContext :: ReturnPositionOpaqueTy { origin, in_trait } => self
1404
+ ImplTraitContext :: ReturnPositionOpaqueTy { origin, fn_kind } => self
1405
1405
. lower_opaque_impl_trait (
1406
1406
span,
1407
1407
* origin,
1408
1408
* def_node_id,
1409
1409
bounds,
1410
- * in_trait ,
1410
+ Some ( * fn_kind ) ,
1411
1411
itctx,
1412
1412
) ,
1413
1413
& ImplTraitContext :: TypeAliasesOpaqueTy { in_assoc_ty } => self
@@ -1416,7 +1416,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1416
1416
hir:: OpaqueTyOrigin :: TyAlias { in_assoc_ty } ,
1417
1417
* def_node_id,
1418
1418
bounds,
1419
- false ,
1419
+ None ,
1420
1420
itctx,
1421
1421
) ,
1422
1422
ImplTraitContext :: Universal => {
@@ -1523,7 +1523,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1523
1523
origin : hir:: OpaqueTyOrigin ,
1524
1524
opaque_ty_node_id : NodeId ,
1525
1525
bounds : & GenericBounds ,
1526
- in_trait : bool ,
1526
+ fn_kind : Option < FnDeclKind > ,
1527
1527
itctx : & ImplTraitContext ,
1528
1528
) -> hir:: TyKind < ' hir > {
1529
1529
// Make sure we know that some funky desugaring has been going on here.
@@ -1540,10 +1540,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1540
1540
Vec :: new ( )
1541
1541
}
1542
1542
hir:: OpaqueTyOrigin :: FnReturn ( ..) => {
1543
- // in fn return position, like the `fn test<'a>() -> impl Debug + 'a`
1544
- // example, we only need to duplicate lifetimes that appear in the
1545
- // bounds, since those are the only ones that are captured by the opaque.
1546
- lifetime_collector:: lifetimes_in_bounds ( & self . resolver , bounds)
1543
+ if let FnDeclKind :: Impl | FnDeclKind :: Trait =
1544
+ fn_kind. expect ( "expected RPITs to be lowered with a FnKind" )
1545
+ {
1546
+ // return-position impl trait in trait was decided to capture all
1547
+ // in-scope lifetimes, which we collect for all opaques during resolution.
1548
+ self . resolver
1549
+ . take_extra_lifetime_params ( opaque_ty_node_id)
1550
+ . into_iter ( )
1551
+ . map ( |( ident, id, _) | Lifetime { id, ident } )
1552
+ . collect ( )
1553
+ } else {
1554
+ // in fn return position, like the `fn test<'a>() -> impl Debug + 'a`
1555
+ // example, we only need to duplicate lifetimes that appear in the
1556
+ // bounds, since those are the only ones that are captured by the opaque.
1557
+ lifetime_collector:: lifetimes_in_bounds ( & self . resolver , bounds)
1558
+ }
1547
1559
}
1548
1560
hir:: OpaqueTyOrigin :: AsyncFn ( ..) => {
1549
1561
unreachable ! ( "should be using `lower_async_fn_ret_ty`" )
@@ -1554,7 +1566,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1554
1566
self . lower_opaque_inner (
1555
1567
opaque_ty_node_id,
1556
1568
origin,
1557
- in_trait ,
1569
+ matches ! ( fn_kind , Some ( FnDeclKind :: Trait ) ) ,
1558
1570
captured_lifetimes_to_duplicate,
1559
1571
span,
1560
1572
opaque_ty_span,
@@ -1802,20 +1814,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1802
1814
}
1803
1815
1804
1816
let fn_def_id = self . local_def_id ( fn_node_id) ;
1805
- self . lower_async_fn_ret_ty (
1806
- & decl. output ,
1807
- fn_def_id,
1808
- ret_id,
1809
- matches ! ( kind, FnDeclKind :: Trait ) ,
1810
- )
1817
+ self . lower_async_fn_ret_ty ( & decl. output , fn_def_id, ret_id, kind)
1811
1818
} else {
1812
1819
match & decl. output {
1813
1820
FnRetTy :: Ty ( ty) => {
1814
1821
let context = if kind. return_impl_trait_allowed ( self . tcx ) {
1815
1822
let fn_def_id = self . local_def_id ( fn_node_id) ;
1816
1823
ImplTraitContext :: ReturnPositionOpaqueTy {
1817
1824
origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1818
- in_trait : matches ! ( kind, FnDeclKind :: Trait ) ,
1825
+ fn_kind : kind,
1819
1826
}
1820
1827
} else {
1821
1828
let position = match kind {
@@ -1883,7 +1890,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1883
1890
output : & FnRetTy ,
1884
1891
fn_def_id : LocalDefId ,
1885
1892
opaque_ty_node_id : NodeId ,
1886
- in_trait : bool ,
1893
+ fn_kind : FnDeclKind ,
1887
1894
) -> hir:: FnRetTy < ' hir > {
1888
1895
let span = self . lower_span ( output. span ( ) ) ;
1889
1896
let opaque_ty_span = self . mark_span_with_reason ( DesugaringKind :: Async , span, None ) ;
@@ -1898,23 +1905,25 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1898
1905
let opaque_ty_ref = self . lower_opaque_inner (
1899
1906
opaque_ty_node_id,
1900
1907
hir:: OpaqueTyOrigin :: AsyncFn ( fn_def_id) ,
1901
- in_trait ,
1908
+ matches ! ( fn_kind , FnDeclKind :: Trait ) ,
1902
1909
captured_lifetimes,
1903
1910
span,
1904
1911
opaque_ty_span,
1905
1912
|this| {
1906
1913
let future_bound = this. lower_async_fn_output_type_to_future_bound (
1907
1914
output,
1908
1915
span,
1909
- if in_trait && !this. tcx . features ( ) . return_position_impl_trait_in_trait {
1916
+ if let FnDeclKind :: Trait = fn_kind
1917
+ && !this. tcx . features ( ) . return_position_impl_trait_in_trait
1918
+ {
1910
1919
ImplTraitContext :: FeatureGated (
1911
1920
ImplTraitPosition :: TraitReturn ,
1912
1921
sym:: return_position_impl_trait_in_trait,
1913
1922
)
1914
1923
} else {
1915
1924
ImplTraitContext :: ReturnPositionOpaqueTy {
1916
1925
origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1917
- in_trait ,
1926
+ fn_kind ,
1918
1927
}
1919
1928
} ,
1920
1929
) ;
0 commit comments