@@ -19,6 +19,7 @@ use rustc_data_structures::indexed_vec::Idx;
1919use  rustc_data_structures:: sync:: Lrc ; 
2020use  rustc_errors:: { Applicability ,  DiagnosticBuilder } ; 
2121use  syntax_pos:: Span ; 
22+ use  syntax:: source_map:: CompilerDesugaringKind ; 
2223
2324use  super :: borrow_set:: BorrowData ; 
2425use  super :: { Context ,  MirBorrowckCtxt } ; 
@@ -154,6 +155,18 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
154155                        span, 
155156                        format ! ( "value moved{} here, in previous iteration of loop" ,  move_msg) , 
156157                    ) ; 
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+                     } 
157170                    is_loop_move = true ; 
158171                }  else  if  move_site. traversed_back_edge  { 
159172                    err. span_label ( 
@@ -291,8 +304,11 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
291304            format ! ( "move occurs due to use{}" ,  move_spans. describe( ) ) 
292305        ) ; 
293306
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) ) ; 
296312        err. buffer ( & mut  self . errors_buffer ) ; 
297313    } 
298314
@@ -329,7 +345,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
329345        } ) ; 
330346
331347        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 ) ; 
333349        err. buffer ( & mut  self . errors_buffer ) ; 
334350    } 
335351
@@ -542,8 +558,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
542558            ) ) ; 
543559        } 
544560
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+         ) ; 
547568
548569        err. buffer ( & mut  self . errors_buffer ) ; 
549570    } 
@@ -866,7 +887,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
866887
867888            if  let  BorrowExplanation :: MustBeValidFor  {  .. }  = explanation { 
868889            }  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+                 ) ; 
870897            } 
871898        }  else  { 
872899            err. span_label ( borrow_span,  "borrowed value does not live long enough" ) ; 
@@ -886,7 +913,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
886913                format ! ( "value captured here{}" ,  within) , 
887914            ) ; 
888915
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 ) ; 
890917        } 
891918
892919        err
@@ -946,7 +973,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
946973            _ => { } 
947974        } 
948975
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 ) ; 
950977
951978        err. buffer ( & mut  self . errors_buffer ) ; 
952979    } 
@@ -1027,7 +1054,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
10271054            } 
10281055            _ => { } 
10291056        } 
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 ) ; 
10311058
10321059        let  within = if  borrow_spans. for_generator ( )  { 
10331060            " by generator" 
@@ -1367,7 +1394,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
13671394        ) ; 
13681395
13691396        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 ) ; 
13711398
13721399        err. buffer ( & mut  self . errors_buffer ) ; 
13731400    } 
0 commit comments