@@ -265,13 +265,12 @@ enum ImplTraitContext {
265265     /// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually 
266266     /// equivalent to a new opaque type like `type T = impl Debug; fn foo() -> T`. 
267267     /// 
268-      ReturnPositionOpaqueTy  { 
269-         /// Origin: Either OpaqueTyOrigin::FnReturn or OpaqueTyOrigin::AsyncFn, 
268+      OpaqueTy  { 
270269        origin :  hir:: OpaqueTyOrigin , 
271-         fn_kind :  FnDeclKind , 
270+         /// Only used to change the lifetime capture rules, since 
271+          /// RPITIT captures all in scope, RPIT does not. 
272+          fn_kind :  Option < FnDeclKind > , 
272273    } , 
273-     /// Impl trait in type aliases. 
274-      TypeAliasesOpaqueTy  {  in_assoc_ty :  bool  } , 
275274    /// `impl Trait` is unstably accepted in this position. 
276275     FeatureGated ( ImplTraitPosition ,  Symbol ) , 
277276    /// `impl Trait` is not accepted in this position. 
@@ -1075,9 +1074,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10751074                // Disallow ATB in dyn types 
10761075                if  self . is_in_dyn_type  { 
10771076                    let  suggestion = match  itctx { 
1078-                         ImplTraitContext :: ReturnPositionOpaqueTy  {  .. } 
1079-                         | ImplTraitContext :: TypeAliasesOpaqueTy  {  .. } 
1080-                         | ImplTraitContext :: Universal  => { 
1077+                         ImplTraitContext :: OpaqueTy  {  .. }  | ImplTraitContext :: Universal  => { 
10811078                            let  bound_end_span = constraint
10821079                                . gen_args 
10831080                                . as_ref ( ) 
@@ -1417,24 +1414,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14171414            TyKind :: ImplTrait ( def_node_id,  bounds)  => { 
14181415                let  span = t. span ; 
14191416                match  itctx { 
1420-                     ImplTraitContext :: ReturnPositionOpaqueTy  {  origin,  fn_kind }  => self 
1421-                         . lower_opaque_impl_trait ( 
1422-                             span, 
1423-                             origin, 
1424-                             * def_node_id, 
1425-                             bounds, 
1426-                             Some ( fn_kind) , 
1427-                             itctx, 
1428-                         ) , 
1429-                     ImplTraitContext :: TypeAliasesOpaqueTy  {  in_assoc_ty }  => self 
1430-                         . lower_opaque_impl_trait ( 
1431-                             span, 
1432-                             hir:: OpaqueTyOrigin :: TyAlias  {  in_assoc_ty } , 
1433-                             * def_node_id, 
1434-                             bounds, 
1435-                             None , 
1436-                             itctx, 
1437-                         ) , 
1417+                     ImplTraitContext :: OpaqueTy  {  origin,  fn_kind }  => self . lower_opaque_impl_trait ( 
1418+                         span, 
1419+                         origin, 
1420+                         * def_node_id, 
1421+                         bounds, 
1422+                         fn_kind, 
1423+                         itctx, 
1424+                     ) , 
14381425                    ImplTraitContext :: Universal  => { 
14391426                        let  span = t. span ; 
14401427
@@ -1553,9 +1540,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15531540
15541541        let  captured_lifetimes_to_duplicate = match  origin { 
15551542            hir:: OpaqueTyOrigin :: TyAlias  {  .. }  => { 
1556-                 // in a TAIT like `type Foo<'a> = impl Foo<'a>`, we don't duplicate any 
1557-                 // lifetimes, since we don't have the issue that any are late-bound. 
1558-                 Vec :: new ( ) 
1543+                 // type alias impl trait and associated type position impl trait were 
1544+                 // decided to capture all in-scope lifetimes, which we collect for 
1545+                 // all opaques during resolution. 
1546+                 self . resolver 
1547+                     . take_extra_lifetime_params ( opaque_ty_node_id) 
1548+                     . into_iter ( ) 
1549+                     . map ( |( ident,  id,  _) | Lifetime  {  id,  ident } ) 
1550+                     . collect ( ) 
15591551            } 
15601552            hir:: OpaqueTyOrigin :: FnReturn ( ..)  => { 
15611553                if  matches ! ( 
@@ -1823,9 +1815,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18231815                        FnDeclKind :: Fn 
18241816                        | FnDeclKind :: Inherent 
18251817                        | FnDeclKind :: Trait 
1826-                         | FnDeclKind :: Impl  => ImplTraitContext :: ReturnPositionOpaqueTy  { 
1818+                         | FnDeclKind :: Impl  => ImplTraitContext :: OpaqueTy  { 
18271819                            origin :  hir:: OpaqueTyOrigin :: FnReturn ( self . local_def_id ( fn_node_id) ) , 
1828-                             fn_kind :  kind, 
1820+                             fn_kind :  Some ( kind) , 
18291821                        } , 
18301822                        FnDeclKind :: ExternFn  => { 
18311823                            ImplTraitContext :: Disallowed ( ImplTraitPosition :: ExternFnReturn ) 
@@ -1919,9 +1911,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19191911                    output, 
19201912                    coro, 
19211913                    opaque_ty_span, 
1922-                     ImplTraitContext :: ReturnPositionOpaqueTy  { 
1914+                     ImplTraitContext :: OpaqueTy  { 
19231915                        origin :  hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) , 
1924-                         fn_kind, 
1916+                         fn_kind :   Some ( fn_kind ) , 
19251917                    } , 
19261918                ) ; 
19271919                arena_vec ! [ this;  bound] 
0 commit comments