@@ -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
@@ -1583,30 +1570,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
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.
@@ -1863,14 +1840,23 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1863
1840
None => match & decl. output {
1864
1841
FnRetTy :: Ty ( ty) => {
1865
1842
let itctx = match kind {
1866
- FnDeclKind :: Fn
1867
- | FnDeclKind :: Inherent
1868
- | FnDeclKind :: Trait
1869
- | FnDeclKind :: Impl => ImplTraitContext :: OpaqueTy {
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 {
1870
1850
origin : hir:: OpaqueTyOrigin :: FnReturn {
1871
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 ) ,
1872
1859
} ,
1873
- fn_kind : Some ( kind) ,
1874
1860
} ,
1875
1861
FnDeclKind :: ExternFn => {
1876
1862
ImplTraitContext :: Disallowed ( ImplTraitPosition :: ExternFnReturn )
@@ -1952,10 +1938,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1952
1938
. map ( |( ident, id, _) | Lifetime { id, ident } )
1953
1939
. collect ( ) ;
1954
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
+
1955
1948
let opaque_ty_ref = self . lower_opaque_inner (
1956
1949
opaque_ty_node_id,
1957
- hir:: OpaqueTyOrigin :: AsyncFn { parent : fn_def_id } ,
1958
- matches ! ( fn_kind, FnDeclKind :: Trait ) ,
1950
+ hir:: OpaqueTyOrigin :: AsyncFn { parent : fn_def_id, in_trait_or_impl } ,
1959
1951
captured_lifetimes,
1960
1952
span,
1961
1953
opaque_ty_span,
@@ -1965,8 +1957,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1965
1957
coro,
1966
1958
opaque_ty_span,
1967
1959
ImplTraitContext :: OpaqueTy {
1968
- origin : hir:: OpaqueTyOrigin :: FnReturn { parent : fn_def_id } ,
1969
- fn_kind : Some ( fn_kind) ,
1960
+ origin : hir:: OpaqueTyOrigin :: FnReturn {
1961
+ parent : fn_def_id,
1962
+ in_trait_or_impl,
1963
+ } ,
1970
1964
} ,
1971
1965
) ;
1972
1966
arena_vec ! [ this; bound]
0 commit comments