@@ -10,16 +10,18 @@ use rustc_middle::mir::{
1010 FakeReadCause , Local , LocalDecl , LocalInfo , LocalKind , Location , Operand , Place , PlaceRef ,
1111 ProjectionElem , Rvalue , Statement , StatementKind , Terminator , TerminatorKind , VarBindingForm ,
1212} ;
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 ;
1517
1618use crate :: dataflow:: drop_flag_effects;
1719use crate :: dataflow:: indexes:: { MoveOutIndex , MovePathIndex } ;
1820use crate :: util:: borrowck_errors;
1921
2022use 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 ,
2325} ;
2426
2527use super :: {
@@ -1267,6 +1269,29 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12671269
12681270 if return_span != borrow_span {
12691271 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+ }
12701295 }
12711296
12721297 Some ( err)
0 commit comments