@@ -19,6 +19,7 @@ use rustc_data_structures::indexed_vec::Idx;
19
19
use rustc_data_structures:: sync:: Lrc ;
20
20
use rustc_errors:: { Applicability , DiagnosticBuilder } ;
21
21
use syntax_pos:: Span ;
22
+ use syntax:: source_map:: CompilerDesugaringKind ;
22
23
23
24
use super :: borrow_set:: BorrowData ;
24
25
use super :: { Context , MirBorrowckCtxt } ;
@@ -154,6 +155,18 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
154
155
span,
155
156
format ! ( "value moved{} here, in previous iteration of loop" , move_msg) ,
156
157
) ;
158
+ if Some ( CompilerDesugaringKind :: ForLoop ) == span. compiler_desugaring_kind ( ) {
159
+ if let Ok ( snippet) = self . infcx . tcx . sess . source_map ( )
160
+ . span_to_snippet ( span)
161
+ {
162
+ err. span_suggestion (
163
+ move_span,
164
+ "consider borrowing this to avoid moving it into the for loop" ,
165
+ format ! ( "&{}" , snippet) ,
166
+ Applicability :: MaybeIncorrect ,
167
+ ) ;
168
+ }
169
+ }
157
170
is_loop_move = true ;
158
171
} else if move_site. traversed_back_edge {
159
172
err. span_label (
@@ -291,8 +304,11 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
291
304
format ! ( "move occurs due to use{}" , move_spans. describe( ) )
292
305
) ;
293
306
294
- self . explain_why_borrow_contains_point ( context, borrow, None )
295
- . add_explanation_to_diagnostic ( self . infcx . tcx , self . mir , & mut err, "" ) ;
307
+ self . explain_why_borrow_contains_point (
308
+ context,
309
+ borrow,
310
+ None ,
311
+ ) . add_explanation_to_diagnostic ( self . infcx . tcx , self . mir , & mut err, "" , Some ( borrow_span) ) ;
296
312
err. buffer ( & mut self . errors_buffer ) ;
297
313
}
298
314
@@ -329,7 +345,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
329
345
} ) ;
330
346
331
347
self . explain_why_borrow_contains_point ( context, borrow, None )
332
- . add_explanation_to_diagnostic ( self . infcx . tcx , self . mir , & mut err, "" ) ;
348
+ . add_explanation_to_diagnostic ( self . infcx . tcx , self . mir , & mut err, "" , None ) ;
333
349
err. buffer ( & mut self . errors_buffer ) ;
334
350
}
335
351
@@ -542,8 +558,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
542
558
) ) ;
543
559
}
544
560
545
- explanation
546
- . add_explanation_to_diagnostic ( self . infcx . tcx , self . mir , & mut err, first_borrow_desc) ;
561
+ explanation. add_explanation_to_diagnostic (
562
+ self . infcx . tcx ,
563
+ self . mir ,
564
+ & mut err,
565
+ first_borrow_desc,
566
+ None ,
567
+ ) ;
547
568
548
569
err. buffer ( & mut self . errors_buffer ) ;
549
570
}
@@ -866,7 +887,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
866
887
867
888
if let BorrowExplanation :: MustBeValidFor { .. } = explanation {
868
889
} else {
869
- explanation. add_explanation_to_diagnostic ( self . infcx . tcx , self . mir , & mut err, "" ) ;
890
+ explanation. add_explanation_to_diagnostic (
891
+ self . infcx . tcx ,
892
+ self . mir ,
893
+ & mut err,
894
+ "" ,
895
+ None ,
896
+ ) ;
870
897
}
871
898
} else {
872
899
err. span_label ( borrow_span, "borrowed value does not live long enough" ) ;
@@ -886,7 +913,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
886
913
format ! ( "value captured here{}" , within) ,
887
914
) ;
888
915
889
- explanation. add_explanation_to_diagnostic ( self . infcx . tcx , self . mir , & mut err, "" ) ;
916
+ explanation. add_explanation_to_diagnostic ( self . infcx . tcx , self . mir , & mut err, "" , None ) ;
890
917
}
891
918
892
919
err
@@ -946,7 +973,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
946
973
_ => { }
947
974
}
948
975
949
- explanation. add_explanation_to_diagnostic ( self . infcx . tcx , self . mir , & mut err, "" ) ;
976
+ explanation. add_explanation_to_diagnostic ( self . infcx . tcx , self . mir , & mut err, "" , None ) ;
950
977
951
978
err. buffer ( & mut self . errors_buffer ) ;
952
979
}
@@ -1027,7 +1054,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
1027
1054
}
1028
1055
_ => { }
1029
1056
}
1030
- explanation. add_explanation_to_diagnostic ( self . infcx . tcx , self . mir , & mut err, "" ) ;
1057
+ explanation. add_explanation_to_diagnostic ( self . infcx . tcx , self . mir , & mut err, "" , None ) ;
1031
1058
1032
1059
let within = if borrow_spans. for_generator ( ) {
1033
1060
" by generator"
@@ -1367,7 +1394,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
1367
1394
) ;
1368
1395
1369
1396
self . explain_why_borrow_contains_point ( context, loan, None )
1370
- . add_explanation_to_diagnostic ( self . infcx . tcx , self . mir , & mut err, "" ) ;
1397
+ . add_explanation_to_diagnostic ( self . infcx . tcx , self . mir , & mut err, "" , None ) ;
1371
1398
1372
1399
err. buffer ( & mut self . errors_buffer ) ;
1373
1400
}
0 commit comments