@@ -259,6 +259,8 @@ enum ImplTraitContext {
259
259
} ,
260
260
/// Impl trait in type aliases.
261
261
TypeAliasesOpaqueTy ,
262
+ /// `impl Trait` is unstably accepted in this position.
263
+ FeatureGated ( ImplTraitPosition , Symbol ) ,
262
264
/// `impl Trait` is not accepted in this position.
263
265
Disallowed ( ImplTraitPosition ) ,
264
266
}
@@ -1372,25 +1374,23 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1372
1374
}
1373
1375
path
1374
1376
}
1375
- ImplTraitContext :: Disallowed (
1376
- position @ ( ImplTraitPosition :: TraitReturn | ImplTraitPosition :: ImplReturn ) ,
1377
- ) => {
1377
+ ImplTraitContext :: FeatureGated ( position, feature) => {
1378
1378
self . tcx
1379
1379
. sess
1380
1380
. create_feature_err (
1381
1381
MisplacedImplTrait {
1382
1382
span : t. span ,
1383
- position : DiagnosticArgFromDisplay ( & position) ,
1383
+ position : DiagnosticArgFromDisplay ( position) ,
1384
1384
} ,
1385
- sym :: return_position_impl_trait_in_trait ,
1385
+ * feature ,
1386
1386
)
1387
1387
. emit ( ) ;
1388
1388
hir:: TyKind :: Err
1389
1389
}
1390
1390
ImplTraitContext :: Disallowed ( position) => {
1391
1391
self . tcx . sess . emit_err ( MisplacedImplTrait {
1392
1392
span : t. span ,
1393
- position : DiagnosticArgFromDisplay ( & position) ,
1393
+ position : DiagnosticArgFromDisplay ( position) ,
1394
1394
} ) ;
1395
1395
hir:: TyKind :: Err
1396
1396
}
@@ -1739,14 +1739,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1739
1739
} else {
1740
1740
match & decl. output {
1741
1741
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 ) {
1743
1743
let fn_def_id = self . local_def_id ( fn_node_id) ;
1744
1744
ImplTraitContext :: ReturnPositionOpaqueTy {
1745
1745
origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1746
1746
in_trait : matches ! ( kind, FnDeclKind :: Trait ) ,
1747
1747
}
1748
1748
} else {
1749
- ImplTraitContext :: Disallowed ( match kind {
1749
+ let position = match kind {
1750
1750
FnDeclKind :: Fn | FnDeclKind :: Inherent => {
1751
1751
unreachable ! ( "fn should allow in-band lifetimes" )
1752
1752
}
@@ -1755,9 +1755,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1755
1755
FnDeclKind :: Pointer => ImplTraitPosition :: PointerReturn ,
1756
1756
FnDeclKind :: Trait => ImplTraitPosition :: TraitReturn ,
1757
1757
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
+ }
1759
1766
} ;
1760
- hir:: FnRetTy :: Return ( self . lower_ty ( ty, & mut context) )
1767
+ hir:: FnRetTy :: Return ( self . lower_ty ( ty, & context) )
1761
1768
}
1762
1769
FnRetTy :: Default ( span) => hir:: FnRetTy :: DefaultReturn ( self . lower_span ( * span) ) ,
1763
1770
}
@@ -1938,7 +1945,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1938
1945
output,
1939
1946
span,
1940
1947
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
+ )
1942
1952
} else {
1943
1953
ImplTraitContext :: ReturnPositionOpaqueTy {
1944
1954
origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
0 commit comments