@@ -252,11 +252,10 @@ enum ImplTraitContext {
252
252
ReturnPositionOpaqueTy {
253
253
/// Origin: Either OpaqueTyOrigin::FnReturn or OpaqueTyOrigin::AsyncFn,
254
254
origin : hir:: OpaqueTyOrigin ,
255
+ in_trait : bool ,
255
256
} ,
256
257
/// Impl trait in type aliases.
257
258
TypeAliasesOpaqueTy ,
258
- /// Return-position `impl Trait` in trait definition
259
- InTrait ,
260
259
/// `impl Trait` is not accepted in this position.
261
260
Disallowed ( ImplTraitPosition ) ,
262
261
}
@@ -1343,24 +1342,26 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1343
1342
TyKind :: ImplTrait ( def_node_id, ref bounds) => {
1344
1343
let span = t. span ;
1345
1344
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
+ ) ,
1349
1354
ImplTraitContext :: TypeAliasesOpaqueTy => {
1350
1355
let mut nested_itctx = ImplTraitContext :: TypeAliasesOpaqueTy ;
1351
1356
self . lower_opaque_impl_trait (
1352
1357
span,
1353
1358
hir:: OpaqueTyOrigin :: TyAlias ,
1354
1359
def_node_id,
1355
1360
bounds,
1356
- & mut nested_itctx,
1361
+ false ,
1362
+ nested_itctx,
1357
1363
)
1358
1364
}
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
- }
1364
1365
ImplTraitContext :: Universal => {
1365
1366
let span = t. span ;
1366
1367
let ident = Ident :: from_str_and_span ( & pprust:: ty_to_string ( t) , span) ;
@@ -1430,6 +1431,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1430
1431
origin : hir:: OpaqueTyOrigin ,
1431
1432
opaque_ty_node_id : NodeId ,
1432
1433
bounds : & GenericBounds ,
1434
+ in_trait : bool ,
1433
1435
itctx : & mut ImplTraitContext ,
1434
1436
) -> hir:: TyKind < ' hir > {
1435
1437
// Make sure we know that some funky desugaring has been going on here.
@@ -1518,6 +1520,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1518
1520
} ) ,
1519
1521
bounds : hir_bounds,
1520
1522
origin,
1523
+ in_trait,
1521
1524
} ;
1522
1525
debug ! ( ?opaque_ty_item) ;
1523
1526
@@ -1544,30 +1547,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1544
1547
debug ! ( ?lifetimes) ;
1545
1548
1546
1549
// `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)
1571
1551
}
1572
1552
1573
1553
/// Registers a new opaque type with the proper `NodeId`s and
@@ -1728,30 +1708,28 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1728
1708
)
1729
1709
. emit ( ) ;
1730
1710
}
1731
- self . lower_async_fn_ret_ty_in_trait (
1711
+ self . lower_async_fn_ret_ty (
1732
1712
& decl. output ,
1733
1713
fn_node_id. expect ( "`make_ret_async` but no `fn_def_id`" ) ,
1734
1714
ret_id,
1715
+ true ,
1735
1716
)
1736
1717
}
1737
1718
_ => {
1738
1719
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 ( ) ;
1750
1727
}
1751
1728
self . lower_async_fn_ret_ty (
1752
1729
& decl. output ,
1753
1730
fn_node_id. expect ( "`make_ret_async` but no `fn_def_id`" ) ,
1754
1731
ret_id,
1732
+ false ,
1755
1733
)
1756
1734
}
1757
1735
}
@@ -1763,10 +1741,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1763
1741
let fn_def_id = self . local_def_id ( fn_node_id) ;
1764
1742
ImplTraitContext :: ReturnPositionOpaqueTy {
1765
1743
origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1744
+ in_trait : false ,
1766
1745
}
1767
1746
}
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
+ }
1770
1753
}
1771
1754
_ => ImplTraitContext :: Disallowed ( match kind {
1772
1755
FnDeclKind :: Fn | FnDeclKind :: Inherent => {
@@ -1829,6 +1812,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1829
1812
output : & FnRetTy ,
1830
1813
fn_node_id : NodeId ,
1831
1814
opaque_ty_node_id : NodeId ,
1815
+ in_trait : bool ,
1832
1816
) -> hir:: FnRetTy < ' hir > {
1833
1817
let span = output. span ( ) ;
1834
1818
@@ -1960,6 +1944,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1960
1944
span,
1961
1945
ImplTraitContext :: ReturnPositionOpaqueTy {
1962
1946
origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1947
+ in_trait,
1963
1948
} ,
1964
1949
) ;
1965
1950
@@ -1999,6 +1984,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1999
1984
} ) ,
2000
1985
bounds : arena_vec ! [ this; future_bound] ,
2001
1986
origin : hir:: OpaqueTyOrigin :: AsyncFn ( fn_def_id) ,
1987
+ in_trait,
2002
1988
} ;
2003
1989
2004
1990
trace ! ( "exist ty from async fn def id: {:#?}" , opaque_ty_def_id) ;
@@ -2043,41 +2029,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2043
2029
// Foo = impl Trait` is, internally, created as a child of the
2044
2030
// async fn, so the *type parameters* are inherited. It's
2045
2031
// 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
+ ) ;
2048
2037
let opaque_ty = self . ty ( opaque_ty_span, opaque_ty_ref) ;
2049
2038
hir:: FnRetTy :: Return ( self . arena . alloc ( opaque_ty) )
2050
2039
}
2051
2040
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
-
2081
2041
/// Transforms `-> T` into `Future<Output = T>`.
2082
2042
fn lower_async_fn_output_type_to_future_bound (
2083
2043
& mut self ,
0 commit comments