@@ -259,6 +259,8 @@ enum ImplTraitContext {
259259 } ,
260260 /// Impl trait in type aliases.
261261 TypeAliasesOpaqueTy ,
262+ /// `impl Trait` is unstably accepted in this position.
263+ FeatureGated ( ImplTraitPosition , Symbol ) ,
262264 /// `impl Trait` is not accepted in this position.
263265 Disallowed ( ImplTraitPosition ) ,
264266}
@@ -1372,25 +1374,23 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13721374 }
13731375 path
13741376 }
1375- ImplTraitContext :: Disallowed (
1376- position @ ( ImplTraitPosition :: TraitReturn | ImplTraitPosition :: ImplReturn ) ,
1377- ) => {
1377+ ImplTraitContext :: FeatureGated ( position, feature) => {
13781378 self . tcx
13791379 . sess
13801380 . create_feature_err (
13811381 MisplacedImplTrait {
13821382 span : t. span ,
1383- position : DiagnosticArgFromDisplay ( & position) ,
1383+ position : DiagnosticArgFromDisplay ( position) ,
13841384 } ,
1385- sym :: return_position_impl_trait_in_trait ,
1385+ * feature ,
13861386 )
13871387 . emit ( ) ;
13881388 hir:: TyKind :: Err
13891389 }
13901390 ImplTraitContext :: Disallowed ( position) => {
13911391 self . tcx . sess . emit_err ( MisplacedImplTrait {
13921392 span : t. span ,
1393- position : DiagnosticArgFromDisplay ( & position) ,
1393+ position : DiagnosticArgFromDisplay ( position) ,
13941394 } ) ;
13951395 hir:: TyKind :: Err
13961396 }
@@ -1739,14 +1739,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17391739 } else {
17401740 match & decl. output {
17411741 FnRetTy :: Ty ( ty) => {
1742- let mut context = if kind. return_impl_trait_allowed ( self . tcx ) {
1742+ let context = if kind. return_impl_trait_allowed ( self . tcx ) {
17431743 let fn_def_id = self . local_def_id ( fn_node_id) ;
17441744 ImplTraitContext :: ReturnPositionOpaqueTy {
17451745 origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
17461746 in_trait : matches ! ( kind, FnDeclKind :: Trait ) ,
17471747 }
17481748 } else {
1749- ImplTraitContext :: Disallowed ( match kind {
1749+ let position = match kind {
17501750 FnDeclKind :: Fn | FnDeclKind :: Inherent => {
17511751 unreachable ! ( "fn should allow in-band lifetimes" )
17521752 }
@@ -1755,9 +1755,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17551755 FnDeclKind :: Pointer => ImplTraitPosition :: PointerReturn ,
17561756 FnDeclKind :: Trait => ImplTraitPosition :: TraitReturn ,
17571757 FnDeclKind :: Impl => ImplTraitPosition :: ImplReturn ,
1758- } )
1758+ } ;
1759+ match kind {
1760+ FnDeclKind :: Trait | FnDeclKind :: Impl => ImplTraitContext :: FeatureGated (
1761+ position,
1762+ sym:: return_position_impl_trait_in_trait,
1763+ ) ,
1764+ _ => ImplTraitContext :: Disallowed ( position) ,
1765+ }
17591766 } ;
1760- hir:: FnRetTy :: Return ( self . lower_ty ( ty, & mut context) )
1767+ hir:: FnRetTy :: Return ( self . lower_ty ( ty, & context) )
17611768 }
17621769 FnRetTy :: Default ( span) => hir:: FnRetTy :: DefaultReturn ( self . lower_span ( * span) ) ,
17631770 }
@@ -1938,7 +1945,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19381945 output,
19391946 span,
19401947 if in_trait && !this. tcx . features ( ) . return_position_impl_trait_in_trait {
1941- ImplTraitContext :: Disallowed ( ImplTraitPosition :: TraitReturn )
1948+ ImplTraitContext :: FeatureGated (
1949+ ImplTraitPosition :: TraitReturn ,
1950+ sym:: return_position_impl_trait_in_trait,
1951+ )
19421952 } else {
19431953 ImplTraitContext :: ReturnPositionOpaqueTy {
19441954 origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
0 commit comments