@@ -288,12 +288,7 @@ enum ImplTraitContext {
288
288
/// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
289
289
/// equivalent to a new opaque type like `type T = impl Debug; fn foo() -> T`.
290
290
///
291
- OpaqueTy {
292
- origin : hir:: OpaqueTyOrigin ,
293
- /// Only used to change the lifetime capture rules, since
294
- /// RPITIT captures all in scope, RPIT does not.
295
- fn_kind : Option < FnDeclKind > ,
296
- } ,
291
+ OpaqueTy { origin : hir:: OpaqueTyOrigin } ,
297
292
/// `impl Trait` is unstably accepted in this position.
298
293
FeatureGated ( ImplTraitPosition , Symbol ) ,
299
294
/// `impl Trait` is not accepted in this position.
@@ -1404,14 +1399,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1404
1399
TyKind :: ImplTrait ( def_node_id, bounds) => {
1405
1400
let span = t. span ;
1406
1401
match itctx {
1407
- ImplTraitContext :: OpaqueTy { origin, fn_kind } => self . lower_opaque_impl_trait (
1408
- span,
1409
- origin,
1410
- * def_node_id,
1411
- bounds,
1412
- fn_kind,
1413
- itctx,
1414
- ) ,
1402
+ ImplTraitContext :: OpaqueTy { origin } => {
1403
+ self . lower_opaque_impl_trait ( span, origin, * def_node_id, bounds, itctx)
1404
+ }
1415
1405
ImplTraitContext :: Universal => {
1416
1406
if let Some ( span) = bounds. iter ( ) . find_map ( |bound| match * bound {
1417
1407
ast:: GenericBound :: Use ( _, span) => Some ( span) ,
@@ -1513,7 +1503,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1513
1503
origin : hir:: OpaqueTyOrigin ,
1514
1504
opaque_ty_node_id : NodeId ,
1515
1505
bounds : & GenericBounds ,
1516
- fn_kind : Option < FnDeclKind > ,
1517
1506
itctx : ImplTraitContext ,
1518
1507
) -> hir:: TyKind < ' hir > {
1519
1508
// Make sure we know that some funky desugaring has been going on here.
@@ -1555,11 +1544,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1555
1544
. map ( |( ident, id, _) | Lifetime { id, ident } )
1556
1545
. collect ( )
1557
1546
}
1558
- hir:: OpaqueTyOrigin :: FnReturn ( ..) => {
1559
- if matches ! (
1560
- fn_kind. expect( "expected RPITs to be lowered with a FnKind" ) ,
1561
- FnDeclKind :: Impl | FnDeclKind :: Trait
1562
- ) || self . tcx . features ( ) . lifetime_capture_rules_2024
1547
+ hir:: OpaqueTyOrigin :: FnReturn { in_trait_or_impl, .. } => {
1548
+ if in_trait_or_impl. is_some ( )
1549
+ || self . tcx . features ( ) . lifetime_capture_rules_2024
1563
1550
|| span. at_least_rust_2024 ( )
1564
1551
{
1565
1552
// return-position impl trait in trait was decided to capture all
@@ -1576,37 +1563,29 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1576
1563
lifetime_collector:: lifetimes_in_bounds ( self . resolver , bounds)
1577
1564
}
1578
1565
}
1579
- hir:: OpaqueTyOrigin :: AsyncFn ( .. ) => {
1566
+ hir:: OpaqueTyOrigin :: AsyncFn { .. } => {
1580
1567
unreachable ! ( "should be using `lower_async_fn_ret_ty`" )
1581
1568
}
1582
1569
}
1583
1570
} ;
1584
1571
debug ! ( ?captured_lifetimes_to_duplicate) ;
1585
1572
1586
- match fn_kind {
1587
- // Deny `use<>` on RPITIT in trait/trait-impl for now.
1588
- Some ( FnDeclKind :: Trait | FnDeclKind :: Impl ) => {
1573
+ // Feature gate for RPITIT + use<..>
1574
+ match origin {
1575
+ rustc_hir :: OpaqueTyOrigin :: FnReturn { in_trait_or_impl : Some ( _ ) , .. } => {
1589
1576
if let Some ( span) = bounds. iter ( ) . find_map ( |bound| match * bound {
1590
1577
ast:: GenericBound :: Use ( _, span) => Some ( span) ,
1591
1578
_ => None ,
1592
1579
} ) {
1593
1580
self . tcx . dcx ( ) . emit_err ( errors:: NoPreciseCapturesOnRpitit { span } ) ;
1594
1581
}
1595
1582
}
1596
- None
1597
- | Some (
1598
- FnDeclKind :: Fn
1599
- | FnDeclKind :: Inherent
1600
- | FnDeclKind :: ExternFn
1601
- | FnDeclKind :: Closure
1602
- | FnDeclKind :: Pointer ,
1603
- ) => { }
1583
+ _ => { }
1604
1584
}
1605
1585
1606
1586
self . lower_opaque_inner (
1607
1587
opaque_ty_node_id,
1608
1588
origin,
1609
- matches ! ( fn_kind, Some ( FnDeclKind :: Trait ) ) ,
1610
1589
captured_lifetimes_to_duplicate,
1611
1590
span,
1612
1591
opaque_ty_span,
@@ -1618,7 +1597,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1618
1597
& mut self ,
1619
1598
opaque_ty_node_id : NodeId ,
1620
1599
origin : hir:: OpaqueTyOrigin ,
1621
- in_trait : bool ,
1622
1600
captured_lifetimes_to_duplicate : FxIndexSet < Lifetime > ,
1623
1601
span : Span ,
1624
1602
opaque_ty_span : Span ,
@@ -1747,7 +1725,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1747
1725
bounds,
1748
1726
origin,
1749
1727
lifetime_mapping,
1750
- in_trait,
1751
1728
} ;
1752
1729
1753
1730
// Generate an `type Foo = impl Trait;` declaration.
@@ -1776,7 +1753,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1776
1753
hir:: TyKind :: OpaqueDef (
1777
1754
hir:: ItemId { owner_id : hir:: OwnerId { def_id : opaque_ty_def_id } } ,
1778
1755
generic_args,
1779
- in_trait,
1780
1756
)
1781
1757
}
1782
1758
@@ -1864,12 +1840,23 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1864
1840
None => match & decl. output {
1865
1841
FnRetTy :: Ty ( ty) => {
1866
1842
let itctx = match kind {
1867
- FnDeclKind :: Fn
1868
- | FnDeclKind :: Inherent
1869
- | FnDeclKind :: Trait
1870
- | FnDeclKind :: Impl => ImplTraitContext :: OpaqueTy {
1871
- origin : hir:: OpaqueTyOrigin :: FnReturn ( self . local_def_id ( fn_node_id) ) ,
1872
- fn_kind : Some ( kind) ,
1843
+ FnDeclKind :: Fn | FnDeclKind :: Inherent => ImplTraitContext :: OpaqueTy {
1844
+ origin : hir:: OpaqueTyOrigin :: FnReturn {
1845
+ parent : self . local_def_id ( fn_node_id) ,
1846
+ in_trait_or_impl : None ,
1847
+ } ,
1848
+ } ,
1849
+ FnDeclKind :: Trait => ImplTraitContext :: OpaqueTy {
1850
+ origin : hir:: OpaqueTyOrigin :: FnReturn {
1851
+ parent : self . local_def_id ( fn_node_id) ,
1852
+ in_trait_or_impl : Some ( hir:: RpitContext :: Trait ) ,
1853
+ } ,
1854
+ } ,
1855
+ FnDeclKind :: Impl => ImplTraitContext :: OpaqueTy {
1856
+ origin : hir:: OpaqueTyOrigin :: FnReturn {
1857
+ parent : self . local_def_id ( fn_node_id) ,
1858
+ in_trait_or_impl : Some ( hir:: RpitContext :: TraitImpl ) ,
1859
+ } ,
1873
1860
} ,
1874
1861
FnDeclKind :: ExternFn => {
1875
1862
ImplTraitContext :: Disallowed ( ImplTraitPosition :: ExternFnReturn )
@@ -1951,10 +1938,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1951
1938
. map ( |( ident, id, _) | Lifetime { id, ident } )
1952
1939
. collect ( ) ;
1953
1940
1941
+ let in_trait_or_impl = match fn_kind {
1942
+ FnDeclKind :: Trait => Some ( hir:: RpitContext :: Trait ) ,
1943
+ FnDeclKind :: Impl => Some ( hir:: RpitContext :: TraitImpl ) ,
1944
+ FnDeclKind :: Fn | FnDeclKind :: Inherent => None ,
1945
+ FnDeclKind :: ExternFn | FnDeclKind :: Closure | FnDeclKind :: Pointer => unreachable ! ( ) ,
1946
+ } ;
1947
+
1954
1948
let opaque_ty_ref = self . lower_opaque_inner (
1955
1949
opaque_ty_node_id,
1956
- hir:: OpaqueTyOrigin :: AsyncFn ( fn_def_id) ,
1957
- matches ! ( fn_kind, FnDeclKind :: Trait ) ,
1950
+ hir:: OpaqueTyOrigin :: AsyncFn { parent : fn_def_id, in_trait_or_impl } ,
1958
1951
captured_lifetimes,
1959
1952
span,
1960
1953
opaque_ty_span,
@@ -1964,8 +1957,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1964
1957
coro,
1965
1958
opaque_ty_span,
1966
1959
ImplTraitContext :: OpaqueTy {
1967
- origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1968
- fn_kind : Some ( fn_kind) ,
1960
+ origin : hir:: OpaqueTyOrigin :: FnReturn {
1961
+ parent : fn_def_id,
1962
+ in_trait_or_impl,
1963
+ } ,
1969
1964
} ,
1970
1965
) ;
1971
1966
arena_vec ! [ this; bound]
0 commit comments