@@ -288,12 +288,7 @@ enum ImplTraitContext {
288288 /// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
289289 /// equivalent to a new opaque type like `type T = impl Debug; fn foo() -> T`.
290290 ///
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 } ,
297292 /// `impl Trait` is unstably accepted in this position.
298293 FeatureGated ( ImplTraitPosition , Symbol ) ,
299294 /// `impl Trait` is not accepted in this position.
@@ -1404,14 +1399,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14041399 TyKind :: ImplTrait ( def_node_id, bounds) => {
14051400 let span = t. span ;
14061401 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+ }
14151405 ImplTraitContext :: Universal => {
14161406 if let Some ( span) = bounds. iter ( ) . find_map ( |bound| match * bound {
14171407 ast:: GenericBound :: Use ( _, span) => Some ( span) ,
@@ -1513,7 +1503,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15131503 origin : hir:: OpaqueTyOrigin ,
15141504 opaque_ty_node_id : NodeId ,
15151505 bounds : & GenericBounds ,
1516- fn_kind : Option < FnDeclKind > ,
15171506 itctx : ImplTraitContext ,
15181507 ) -> hir:: TyKind < ' hir > {
15191508 // Make sure we know that some funky desugaring has been going on here.
@@ -1555,11 +1544,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15551544 . map ( |( ident, id, _) | Lifetime { id, ident } )
15561545 . collect ( )
15571546 }
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
15631550 || span. at_least_rust_2024 ( )
15641551 {
15651552 // return-position impl trait in trait was decided to capture all
@@ -1576,37 +1563,29 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15761563 lifetime_collector:: lifetimes_in_bounds ( self . resolver , bounds)
15771564 }
15781565 }
1579- hir:: OpaqueTyOrigin :: AsyncFn ( .. ) => {
1566+ hir:: OpaqueTyOrigin :: AsyncFn { .. } => {
15801567 unreachable ! ( "should be using `lower_async_fn_ret_ty`" )
15811568 }
15821569 }
15831570 } ;
15841571 debug ! ( ?captured_lifetimes_to_duplicate) ;
15851572
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 ( _ ) , .. } => {
15891576 if let Some ( span) = bounds. iter ( ) . find_map ( |bound| match * bound {
15901577 ast:: GenericBound :: Use ( _, span) => Some ( span) ,
15911578 _ => None ,
15921579 } ) {
15931580 self . tcx . dcx ( ) . emit_err ( errors:: NoPreciseCapturesOnRpitit { span } ) ;
15941581 }
15951582 }
1596- None
1597- | Some (
1598- FnDeclKind :: Fn
1599- | FnDeclKind :: Inherent
1600- | FnDeclKind :: ExternFn
1601- | FnDeclKind :: Closure
1602- | FnDeclKind :: Pointer ,
1603- ) => { }
1583+ _ => { }
16041584 }
16051585
16061586 self . lower_opaque_inner (
16071587 opaque_ty_node_id,
16081588 origin,
1609- matches ! ( fn_kind, Some ( FnDeclKind :: Trait ) ) ,
16101589 captured_lifetimes_to_duplicate,
16111590 span,
16121591 opaque_ty_span,
@@ -1618,7 +1597,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16181597 & mut self ,
16191598 opaque_ty_node_id : NodeId ,
16201599 origin : hir:: OpaqueTyOrigin ,
1621- in_trait : bool ,
16221600 captured_lifetimes_to_duplicate : FxIndexSet < Lifetime > ,
16231601 span : Span ,
16241602 opaque_ty_span : Span ,
@@ -1747,7 +1725,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17471725 bounds,
17481726 origin,
17491727 lifetime_mapping,
1750- in_trait,
17511728 } ;
17521729
17531730 // Generate an `type Foo = impl Trait;` declaration.
@@ -1776,7 +1753,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17761753 hir:: TyKind :: OpaqueDef (
17771754 hir:: ItemId { owner_id : hir:: OwnerId { def_id : opaque_ty_def_id } } ,
17781755 generic_args,
1779- in_trait,
17801756 )
17811757 }
17821758
@@ -1864,12 +1840,23 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18641840 None => match & decl. output {
18651841 FnRetTy :: Ty ( ty) => {
18661842 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+ } ,
18731860 } ,
18741861 FnDeclKind :: ExternFn => {
18751862 ImplTraitContext :: Disallowed ( ImplTraitPosition :: ExternFnReturn )
@@ -1951,10 +1938,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19511938 . map ( |( ident, id, _) | Lifetime { id, ident } )
19521939 . collect ( ) ;
19531940
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+
19541948 let opaque_ty_ref = self . lower_opaque_inner (
19551949 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 } ,
19581951 captured_lifetimes,
19591952 span,
19601953 opaque_ty_span,
@@ -1964,8 +1957,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19641957 coro,
19651958 opaque_ty_span,
19661959 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+ } ,
19691964 } ,
19701965 ) ;
19711966 arena_vec ! [ this; bound]
0 commit comments