@@ -252,11 +252,10 @@ enum ImplTraitContext {
252252 ReturnPositionOpaqueTy {
253253 /// Origin: Either OpaqueTyOrigin::FnReturn or OpaqueTyOrigin::AsyncFn,
254254 origin : hir:: OpaqueTyOrigin ,
255+ in_trait : bool ,
255256 } ,
256257 /// Impl trait in type aliases.
257258 TypeAliasesOpaqueTy ,
258- /// Return-position `impl Trait` in trait definition
259- InTrait ,
260259 /// `impl Trait` is not accepted in this position.
261260 Disallowed ( ImplTraitPosition ) ,
262261}
@@ -1343,24 +1342,26 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13431342 TyKind :: ImplTrait ( def_node_id, ref bounds) => {
13441343 let span = t. span ;
13451344 match itctx {
1346- ImplTraitContext :: ReturnPositionOpaqueTy { origin } => {
1347- self . lower_opaque_impl_trait ( span, * origin, def_node_id, bounds, itctx)
1348- }
1345+ ImplTraitContext :: ReturnPositionOpaqueTy { origin, in_trait } => self
1346+ . lower_opaque_impl_trait (
1347+ span,
1348+ * origin,
1349+ def_node_id,
1350+ bounds,
1351+ * in_trait,
1352+ itctx,
1353+ ) ,
13491354 ImplTraitContext :: TypeAliasesOpaqueTy => {
13501355 let mut nested_itctx = ImplTraitContext :: TypeAliasesOpaqueTy ;
13511356 self . lower_opaque_impl_trait (
13521357 span,
13531358 hir:: OpaqueTyOrigin :: TyAlias ,
13541359 def_node_id,
13551360 bounds,
1356- & mut nested_itctx,
1361+ false ,
1362+ nested_itctx,
13571363 )
13581364 }
1359- ImplTraitContext :: InTrait => {
1360- self . lower_impl_trait_in_trait ( span, def_node_id, |lctx| {
1361- lctx. lower_param_bounds ( bounds, ImplTraitContext :: InTrait )
1362- } )
1363- }
13641365 ImplTraitContext :: Universal => {
13651366 let span = t. span ;
13661367 let ident = Ident :: from_str_and_span ( & pprust:: ty_to_string ( t) , span) ;
@@ -1430,6 +1431,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14301431 origin : hir:: OpaqueTyOrigin ,
14311432 opaque_ty_node_id : NodeId ,
14321433 bounds : & GenericBounds ,
1434+ in_trait : bool ,
14331435 itctx : & mut ImplTraitContext ,
14341436 ) -> hir:: TyKind < ' hir > {
14351437 // Make sure we know that some funky desugaring has been going on here.
@@ -1518,6 +1520,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15181520 } ) ,
15191521 bounds : hir_bounds,
15201522 origin,
1523+ in_trait,
15211524 } ;
15221525 debug ! ( ?opaque_ty_item) ;
15231526
@@ -1544,30 +1547,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15441547 debug ! ( ?lifetimes) ;
15451548
15461549 // `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
1547- hir:: TyKind :: OpaqueDef ( hir:: ItemId { def_id : opaque_ty_def_id } , lifetimes)
1548- }
1549-
1550- #[ instrument( level = "debug" , skip( self , lower_bounds) ) ]
1551- fn lower_impl_trait_in_trait (
1552- & mut self ,
1553- span : Span ,
1554- opaque_ty_node_id : NodeId ,
1555- lower_bounds : impl FnOnce ( & mut Self ) -> hir:: GenericBounds < ' hir > ,
1556- ) -> hir:: TyKind < ' hir > {
1557- let opaque_ty_def_id = self . local_def_id ( opaque_ty_node_id) ;
1558- self . with_hir_id_owner ( opaque_ty_node_id, |lctx| {
1559- let hir_bounds = lower_bounds ( lctx) ;
1560- let rpitit_placeholder = hir:: ImplTraitPlaceholder { bounds : hir_bounds } ;
1561- let rpitit_item = hir:: Item {
1562- def_id : opaque_ty_def_id,
1563- ident : Ident :: empty ( ) ,
1564- kind : hir:: ItemKind :: ImplTraitPlaceholder ( rpitit_placeholder) ,
1565- span : lctx. lower_span ( span) ,
1566- vis_span : lctx. lower_span ( span. shrink_to_lo ( ) ) ,
1567- } ;
1568- hir:: OwnerNode :: Item ( lctx. arena . alloc ( rpitit_item) )
1569- } ) ;
1570- hir:: TyKind :: ImplTraitInTrait ( hir:: ItemId { def_id : opaque_ty_def_id } )
1550+ hir:: TyKind :: OpaqueDef ( hir:: ItemId { def_id : opaque_ty_def_id } , lifetimes, in_trait)
15711551 }
15721552
15731553 /// Registers a new opaque type with the proper `NodeId`s and
@@ -1728,30 +1708,28 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17281708 )
17291709 . emit ( ) ;
17301710 }
1731- self . lower_async_fn_ret_ty_in_trait (
1711+ self . lower_async_fn_ret_ty (
17321712 & decl. output ,
17331713 fn_node_id. expect ( "`make_ret_async` but no `fn_def_id`" ) ,
17341714 ret_id,
1715+ true ,
17351716 )
17361717 }
17371718 _ => {
17381719 if !kind. impl_trait_return_allowed ( self . tcx ) {
1739- if kind == FnDeclKind :: Impl {
1740- self . tcx
1741- . sess
1742- . create_feature_err (
1743- TraitFnAsync { fn_span, span } ,
1744- sym:: return_position_impl_trait_in_trait,
1745- )
1746- . emit ( ) ;
1747- } else {
1748- self . tcx . sess . emit_err ( TraitFnAsync { fn_span, span } ) ;
1749- }
1720+ self . tcx
1721+ . sess
1722+ . create_feature_err (
1723+ TraitFnAsync { fn_span, span } ,
1724+ sym:: return_position_impl_trait_in_trait,
1725+ )
1726+ . emit ( ) ;
17501727 }
17511728 self . lower_async_fn_ret_ty (
17521729 & decl. output ,
17531730 fn_node_id. expect ( "`make_ret_async` but no `fn_def_id`" ) ,
17541731 ret_id,
1732+ false ,
17551733 )
17561734 }
17571735 }
@@ -1763,10 +1741,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17631741 let fn_def_id = self . local_def_id ( fn_node_id) ;
17641742 ImplTraitContext :: ReturnPositionOpaqueTy {
17651743 origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1744+ in_trait : false ,
17661745 }
17671746 }
1768- Some ( _) if kind. impl_trait_in_trait_allowed ( self . tcx ) => {
1769- ImplTraitContext :: InTrait
1747+ Some ( fn_node_id) if kind. impl_trait_in_trait_allowed ( self . tcx ) => {
1748+ let fn_def_id = self . local_def_id ( fn_node_id) ;
1749+ ImplTraitContext :: ReturnPositionOpaqueTy {
1750+ origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1751+ in_trait : true ,
1752+ }
17701753 }
17711754 _ => ImplTraitContext :: Disallowed ( match kind {
17721755 FnDeclKind :: Fn | FnDeclKind :: Inherent => {
@@ -1829,6 +1812,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18291812 output : & FnRetTy ,
18301813 fn_node_id : NodeId ,
18311814 opaque_ty_node_id : NodeId ,
1815+ in_trait : bool ,
18321816 ) -> hir:: FnRetTy < ' hir > {
18331817 let span = output. span ( ) ;
18341818
@@ -1960,6 +1944,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19601944 span,
19611945 ImplTraitContext :: ReturnPositionOpaqueTy {
19621946 origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1947+ in_trait,
19631948 } ,
19641949 ) ;
19651950
@@ -1999,6 +1984,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19991984 } ) ,
20001985 bounds : arena_vec ! [ this; future_bound] ,
20011986 origin : hir:: OpaqueTyOrigin :: AsyncFn ( fn_def_id) ,
1987+ in_trait,
20021988 } ;
20031989
20041990 trace ! ( "exist ty from async fn def id: {:#?}" , opaque_ty_def_id) ;
@@ -2043,41 +2029,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20432029 // Foo = impl Trait` is, internally, created as a child of the
20442030 // async fn, so the *type parameters* are inherited. It's
20452031 // only the lifetime parameters that we must supply.
2046- let opaque_ty_ref =
2047- hir:: TyKind :: OpaqueDef ( hir:: ItemId { def_id : opaque_ty_def_id } , generic_args) ;
2032+ let opaque_ty_ref = hir:: TyKind :: OpaqueDef (
2033+ hir:: ItemId { def_id : opaque_ty_def_id } ,
2034+ generic_args,
2035+ in_trait,
2036+ ) ;
20482037 let opaque_ty = self . ty ( opaque_ty_span, opaque_ty_ref) ;
20492038 hir:: FnRetTy :: Return ( self . arena . alloc ( opaque_ty) )
20502039 }
20512040
2052- // Transforms `-> T` for `async fn` into `-> OpaqueTy { .. }`
2053- // combined with the following definition of `OpaqueTy`:
2054- //
2055- // type OpaqueTy<generics_from_parent_fn> = impl Future<Output = T>;
2056- //
2057- // `output`: unlowered output type (`T` in `-> T`)
2058- // `fn_def_id`: `DefId` of the parent function (used to create child impl trait definition)
2059- // `opaque_ty_node_id`: `NodeId` of the opaque `impl Trait` type that should be created
2060- #[ instrument( level = "debug" , skip( self ) ) ]
2061- fn lower_async_fn_ret_ty_in_trait (
2062- & mut self ,
2063- output : & FnRetTy ,
2064- fn_node_id : NodeId ,
2065- opaque_ty_node_id : NodeId ,
2066- ) -> hir:: FnRetTy < ' hir > {
2067- let kind = self . lower_impl_trait_in_trait ( output. span ( ) , opaque_ty_node_id, |lctx| {
2068- let bound = lctx. lower_async_fn_output_type_to_future_bound (
2069- output,
2070- output. span ( ) ,
2071- ImplTraitContext :: InTrait ,
2072- ) ;
2073- arena_vec ! [ lctx; bound]
2074- } ) ;
2075-
2076- let opaque_ty_span = self . mark_span_with_reason ( DesugaringKind :: Async , output. span ( ) , None ) ;
2077- let opaque_ty = self . ty ( opaque_ty_span, kind) ;
2078- hir:: FnRetTy :: Return ( self . arena . alloc ( opaque_ty) )
2079- }
2080-
20812041 /// Transforms `-> T` into `Future<Output = T>`.
20822042 fn lower_async_fn_output_type_to_future_bound (
20832043 & mut self ,
0 commit comments