@@ -1146,24 +1146,22 @@ fn infer_return_ty_for_fn_sig<'tcx>(
1146
1146
1147
1147
let mut visitor = HirPlaceholderCollector :: default ( ) ;
1148
1148
visitor. visit_ty ( ty) ;
1149
+
1149
1150
let mut diag = bad_placeholder ( tcx, visitor. 0 , "return type" ) ;
1150
1151
let ret_ty = fn_sig. output ( ) ;
1152
+ // Don't leak types into signatures unless they're nameable!
1153
+ // For example, if a function returns itself, we don't want that
1154
+ // recursive function definition to leak out into the fn sig.
1155
+ let mut should_recover = false ;
1156
+
1151
1157
if let Some ( ret_ty) = ret_ty. make_suggestable ( tcx, false ) {
1152
1158
diag. span_suggestion (
1153
1159
ty. span ,
1154
1160
"replace with the correct return type" ,
1155
1161
ret_ty,
1156
1162
Applicability :: MachineApplicable ,
1157
1163
) ;
1158
- } else if matches ! ( ret_ty. kind( ) , ty:: FnDef ( ..) )
1159
- && let Some ( fn_sig) = ret_ty. fn_sig ( tcx) . make_suggestable ( tcx, false )
1160
- {
1161
- diag. span_suggestion (
1162
- ty. span ,
1163
- "replace with the correct return type" ,
1164
- fn_sig,
1165
- Applicability :: MachineApplicable ,
1166
- ) ;
1164
+ should_recover = true ;
1167
1165
} else if let Some ( sugg) = suggest_impl_trait ( tcx, ret_ty, ty. span , def_id) {
1168
1166
diag. span_suggestion (
1169
1167
ty. span ,
@@ -1181,9 +1179,20 @@ fn infer_return_ty_for_fn_sig<'tcx>(
1181
1179
https://doc.rust-lang.org/book/ch13-01-closures.html",
1182
1180
) ;
1183
1181
}
1184
- diag. emit ( ) ;
1185
1182
1186
- ty:: Binder :: dummy ( fn_sig)
1183
+ let guar = diag. emit ( ) ;
1184
+
1185
+ if should_recover {
1186
+ ty:: Binder :: dummy ( fn_sig)
1187
+ } else {
1188
+ ty:: Binder :: dummy ( tcx. mk_fn_sig (
1189
+ fn_sig. inputs ( ) . iter ( ) . copied ( ) ,
1190
+ tcx. ty_error ( guar) ,
1191
+ fn_sig. c_variadic ,
1192
+ fn_sig. unsafety ,
1193
+ fn_sig. abi ,
1194
+ ) )
1195
+ }
1187
1196
}
1188
1197
None => icx. astconv ( ) . ty_of_fn (
1189
1198
hir_id,
0 commit comments