@@ -1130,11 +1130,19 @@ impl<'a> LoweringContext<'a> {
1130
1130
} ) . collect ( )
1131
1131
}
1132
1132
1133
+
1133
1134
fn lower_fn_decl ( & mut self ,
1134
1135
decl : & FnDecl ,
1135
1136
fn_def_id : Option < DefId > ,
1136
1137
impl_trait_return_allow : bool )
1137
1138
-> P < hir:: FnDecl > {
1139
+ // NOTE: The two last paramters here have to do with impl Trait. If fn_def_id is Some,
1140
+ // then impl Trait arguments are lowered into generic paramters on the given
1141
+ // fn_def_id, otherwise impl Trait is disallowed. (for now)
1142
+ //
1143
+ // Furthermore, if impl_trait_return_allow is true, then impl Trait may be used in
1144
+ // return positions as well. This guards against trait declarations and their impls
1145
+ // where impl Trait is disallowed. (again for now)
1138
1146
P ( hir:: FnDecl {
1139
1147
inputs : decl. inputs . iter ( )
1140
1148
. map ( |arg| if let Some ( def_id) = fn_def_id {
@@ -1143,9 +1151,9 @@ impl<'a> LoweringContext<'a> {
1143
1151
self . lower_ty ( & arg. ty , ImplTraitContext :: Disallowed )
1144
1152
} ) . collect ( ) ,
1145
1153
output : match decl. output {
1146
- FunctionRetTy :: Ty ( ref ty) => match ( impl_trait_return_allow , fn_def_id) {
1147
- ( false , _) => hir :: Return ( self . lower_ty ( ty , ImplTraitContext :: Disallowed ) ) ,
1148
- ( _ , Some ( _ ) ) => hir:: Return ( self . lower_ty ( ty, ImplTraitContext :: Existential ) ) ,
1154
+ FunctionRetTy :: Ty ( ref ty) => match fn_def_id {
1155
+ Some ( _) if impl_trait_return_allow =>
1156
+ hir:: Return ( self . lower_ty ( ty, ImplTraitContext :: Existential ) ) ,
1149
1157
_ => hir:: Return ( self . lower_ty ( ty, ImplTraitContext :: Disallowed ) ) ,
1150
1158
} ,
1151
1159
FunctionRetTy :: Default ( span) => hir:: DefaultReturn ( span) ,
@@ -1730,7 +1738,8 @@ impl<'a> LoweringContext<'a> {
1730
1738
this. expr_block ( body, ThinVec :: new ( ) )
1731
1739
} ) ;
1732
1740
let impl_trait_return_allow = !this. is_in_trait_impl ;
1733
- hir:: ImplItemKind :: Method ( this. lower_method_sig ( sig, fn_def_id,
1741
+ hir:: ImplItemKind :: Method ( this. lower_method_sig ( sig,
1742
+ fn_def_id,
1734
1743
impl_trait_return_allow) ,
1735
1744
body_id)
1736
1745
}
@@ -1833,8 +1842,8 @@ impl<'a> LoweringContext<'a> {
1833
1842
attrs : this. lower_attrs ( & i. attrs ) ,
1834
1843
node : match i. node {
1835
1844
ForeignItemKind :: Fn ( ref fdec, ref generics) => {
1836
- let fn_def_id = this . resolver . definitions ( ) . opt_local_def_id ( i . id ) ;
1837
- hir:: ForeignItemFn ( this. lower_fn_decl ( fdec, fn_def_id , true ) ,
1845
+ // Disallow impl Trait in foreign items
1846
+ hir:: ForeignItemFn ( this. lower_fn_decl ( fdec, None , false ) ,
1838
1847
this. lower_fn_args_to_names ( fdec) ,
1839
1848
this. lower_generics ( generics) )
1840
1849
}
0 commit comments