@@ -10,16 +10,18 @@ use rustc_middle::mir::{
10
10
FakeReadCause , Local , LocalDecl , LocalInfo , LocalKind , Location , Operand , Place , PlaceRef ,
11
11
ProjectionElem , Rvalue , Statement , StatementKind , Terminator , TerminatorKind , VarBindingForm ,
12
12
} ;
13
- use rustc_middle:: ty:: { self , suggest_constraining_type_param, Instance , Ty } ;
14
- use rustc_span:: { source_map:: DesugaringKind , symbol:: sym, Span } ;
13
+ use rustc_middle:: ty:: { self , suggest_constraining_type_param, Ty , TypeFoldable } ;
14
+ use rustc_span:: source_map:: DesugaringKind ;
15
+ use rustc_span:: symbol:: sym;
16
+ use rustc_span:: Span ;
15
17
16
18
use crate :: dataflow:: drop_flag_effects;
17
19
use crate :: dataflow:: indexes:: { MoveOutIndex , MovePathIndex } ;
18
20
use crate :: util:: borrowck_errors;
19
21
20
22
use crate :: borrow_check:: {
21
- borrow_set:: BorrowData , prefixes :: IsPrefixOf , InitializationRequiringAction , MirBorrowckCtxt ,
22
- PrefixSet , WriteKind ,
23
+ borrow_set:: BorrowData , diagnostics :: Instance , prefixes :: IsPrefixOf ,
24
+ InitializationRequiringAction , MirBorrowckCtxt , PrefixSet , WriteKind ,
23
25
} ;
24
26
25
27
use super :: {
@@ -1267,6 +1269,29 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
1267
1269
1268
1270
if return_span != borrow_span {
1269
1271
err. span_label ( borrow_span, note) ;
1272
+
1273
+ let tcx = self . infcx . tcx ;
1274
+ let ty_params = ty:: List :: empty ( ) ;
1275
+
1276
+ let return_ty = self . regioncx . universal_regions ( ) . unnormalized_output_ty ;
1277
+ let return_ty = tcx. erase_regions ( return_ty) ;
1278
+
1279
+ // to avoid panics
1280
+ if !return_ty. has_infer_types ( ) {
1281
+ if let Some ( iter_trait) = tcx. get_diagnostic_item ( sym:: Iterator ) {
1282
+ if tcx. type_implements_trait ( ( iter_trait, return_ty, ty_params, self . param_env ) )
1283
+ {
1284
+ if let Ok ( snippet) = tcx. sess . source_map ( ) . span_to_snippet ( return_span) {
1285
+ err. span_suggestion_hidden (
1286
+ return_span,
1287
+ "use `.collect()` to allocate the iterator" ,
1288
+ format ! ( "{}{}" , snippet, ".collect::<Vec<_>>()" ) ,
1289
+ Applicability :: MaybeIncorrect ,
1290
+ ) ;
1291
+ }
1292
+ }
1293
+ }
1294
+ }
1270
1295
}
1271
1296
1272
1297
Some ( err)
0 commit comments