@@ -236,7 +236,7 @@ enum ImplTraitContext {
236236 ReturnPositionOpaqueTy {
237237 /// Origin: Either OpaqueTyOrigin::FnReturn or OpaqueTyOrigin::AsyncFn,
238238 origin : hir:: OpaqueTyOrigin ,
239- in_trait : bool ,
239+ fn_kind : FnDeclKind ,
240240 } ,
241241 /// Impl trait in type aliases.
242242 TypeAliasesOpaqueTy { in_assoc_ty : bool } ,
@@ -312,7 +312,7 @@ impl std::fmt::Display for ImplTraitPosition {
312312 }
313313}
314314
315- #[ derive( Debug , PartialEq , Eq ) ]
315+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
316316enum FnDeclKind {
317317 Fn ,
318318 Inherent ,
@@ -1401,13 +1401,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14011401 TyKind :: ImplTrait ( def_node_id, bounds) => {
14021402 let span = t. span ;
14031403 match itctx {
1404- ImplTraitContext :: ReturnPositionOpaqueTy { origin, in_trait } => self
1404+ ImplTraitContext :: ReturnPositionOpaqueTy { origin, fn_kind } => self
14051405 . lower_opaque_impl_trait (
14061406 span,
14071407 * origin,
14081408 * def_node_id,
14091409 bounds,
1410- * in_trait ,
1410+ Some ( * fn_kind ) ,
14111411 itctx,
14121412 ) ,
14131413 & ImplTraitContext :: TypeAliasesOpaqueTy { in_assoc_ty } => self
@@ -1416,7 +1416,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14161416 hir:: OpaqueTyOrigin :: TyAlias { in_assoc_ty } ,
14171417 * def_node_id,
14181418 bounds,
1419- false ,
1419+ None ,
14201420 itctx,
14211421 ) ,
14221422 ImplTraitContext :: Universal => {
@@ -1523,7 +1523,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15231523 origin : hir:: OpaqueTyOrigin ,
15241524 opaque_ty_node_id : NodeId ,
15251525 bounds : & GenericBounds ,
1526- in_trait : bool ,
1526+ fn_kind : Option < FnDeclKind > ,
15271527 itctx : & ImplTraitContext ,
15281528 ) -> hir:: TyKind < ' hir > {
15291529 // Make sure we know that some funky desugaring has been going on here.
@@ -1540,10 +1540,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15401540 Vec :: new ( )
15411541 }
15421542 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+ }
15471559 }
15481560 hir:: OpaqueTyOrigin :: AsyncFn ( ..) => {
15491561 unreachable ! ( "should be using `lower_async_fn_ret_ty`" )
@@ -1554,7 +1566,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15541566 self . lower_opaque_inner (
15551567 opaque_ty_node_id,
15561568 origin,
1557- in_trait ,
1569+ matches ! ( fn_kind , Some ( FnDeclKind :: Trait ) ) ,
15581570 captured_lifetimes_to_duplicate,
15591571 span,
15601572 opaque_ty_span,
@@ -1802,20 +1814,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18021814 }
18031815
18041816 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)
18111818 } else {
18121819 match & decl. output {
18131820 FnRetTy :: Ty ( ty) => {
18141821 let context = if kind. return_impl_trait_allowed ( self . tcx ) {
18151822 let fn_def_id = self . local_def_id ( fn_node_id) ;
18161823 ImplTraitContext :: ReturnPositionOpaqueTy {
18171824 origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1818- in_trait : matches ! ( kind, FnDeclKind :: Trait ) ,
1825+ fn_kind : kind,
18191826 }
18201827 } else {
18211828 let position = match kind {
@@ -1883,7 +1890,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18831890 output : & FnRetTy ,
18841891 fn_def_id : LocalDefId ,
18851892 opaque_ty_node_id : NodeId ,
1886- in_trait : bool ,
1893+ fn_kind : FnDeclKind ,
18871894 ) -> hir:: FnRetTy < ' hir > {
18881895 let span = self . lower_span ( output. span ( ) ) ;
18891896 let opaque_ty_span = self . mark_span_with_reason ( DesugaringKind :: Async , span, None ) ;
@@ -1898,23 +1905,25 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18981905 let opaque_ty_ref = self . lower_opaque_inner (
18991906 opaque_ty_node_id,
19001907 hir:: OpaqueTyOrigin :: AsyncFn ( fn_def_id) ,
1901- in_trait ,
1908+ matches ! ( fn_kind , FnDeclKind :: Trait ) ,
19021909 captured_lifetimes,
19031910 span,
19041911 opaque_ty_span,
19051912 |this| {
19061913 let future_bound = this. lower_async_fn_output_type_to_future_bound (
19071914 output,
19081915 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+ {
19101919 ImplTraitContext :: FeatureGated (
19111920 ImplTraitPosition :: TraitReturn ,
19121921 sym:: return_position_impl_trait_in_trait,
19131922 )
19141923 } else {
19151924 ImplTraitContext :: ReturnPositionOpaqueTy {
19161925 origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1917- in_trait ,
1926+ fn_kind ,
19181927 }
19191928 } ,
19201929 ) ;
0 commit comments