@@ -4,13 +4,19 @@ use rustc_errors::{
44use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
55use rustc_span:: Span ;
66
7+ use crate :: session_diagnostics:: {
8+ AssignBorrowErr , BorrowAcrossDestructor , BorrowAcrossGeneratorYield , ClosureConstructLabel ,
9+ InteriorDropMoveErr , MoveBorrowedErr , PathShortLive , TwoClosuresUniquelyBorrowErr ,
10+ UseMutBorrowErr ,
11+ } ;
12+
713impl < ' cx , ' tcx > crate :: MirBorrowckCtxt < ' cx , ' tcx > {
814 pub ( crate ) fn cannot_move_when_borrowed (
915 & self ,
1016 span : Span ,
1117 desc : & str ,
1218 ) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
13- struct_span_err ! ( self , span , E0505 , "cannot move out of {} because it is borrowed" , desc , )
19+ self . infcx . tcx . sess . create_err ( MoveBorrowedErr { desc , span } )
1420 }
1521
1622 pub ( crate ) fn cannot_use_when_mutably_borrowed (
@@ -20,17 +26,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
2026 borrow_span : Span ,
2127 borrow_desc : & str ,
2228 ) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
23- let mut err = struct_span_err ! (
24- self ,
25- span,
26- E0503 ,
27- "cannot use {} because it was mutably borrowed" ,
28- desc,
29- ) ;
30-
31- err. span_label ( borrow_span, format ! ( "borrow of {} occurs here" , borrow_desc) ) ;
32- err. span_label ( span, format ! ( "use of borrowed {}" , borrow_desc) ) ;
33- err
29+ self . infcx . tcx . sess . create_err ( UseMutBorrowErr { desc, borrow_desc, span, borrow_span } )
3430 }
3531
3632 pub ( crate ) fn cannot_mutably_borrow_multiply (
@@ -90,26 +86,22 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
9086 old_loan_span : Span ,
9187 old_load_end_span : Option < Span > ,
9288 ) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
93- let mut err = struct_span_err ! (
94- self ,
95- new_loan_span,
96- E0524 ,
97- "two closures require unique access to {} at the same time" ,
98- desc,
99- ) ;
89+ let case: ClosureConstructLabel ;
90+ let diff_span: Option < Span > ;
10091 if old_loan_span == new_loan_span {
101- err. span_label (
102- old_loan_span,
103- "closures are constructed here in different iterations of loop" ,
104- ) ;
92+ case = ClosureConstructLabel :: Both { old_loan_span } ;
93+ diff_span = None ;
10594 } else {
106- err . span_label ( old_loan_span , "first closure is constructed here" ) ;
107- err . span_label ( new_loan_span , "second closure is constructed here" ) ;
95+ case = ClosureConstructLabel :: First { old_loan_span } ;
96+ diff_span = Some ( new_loan_span ) ;
10897 }
109- if let Some ( old_load_end_span) = old_load_end_span {
110- err. span_label ( old_load_end_span, "borrow from first closure ends here" ) ;
111- }
112- err
98+ self . infcx . tcx . sess . create_err ( TwoClosuresUniquelyBorrowErr {
99+ desc,
100+ case,
101+ new_loan_span,
102+ old_load_end_span,
103+ diff_span,
104+ } )
113105 }
114106
115107 pub ( crate ) fn cannot_uniquely_borrow_by_one_closure (
@@ -233,17 +225,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
233225 borrow_span : Span ,
234226 desc : & str ,
235227 ) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
236- let mut err = struct_span_err ! (
237- self ,
238- span,
239- E0506 ,
240- "cannot assign to {} because it is borrowed" ,
241- desc,
242- ) ;
243-
244- err. span_label ( borrow_span, format ! ( "borrow of {} occurs here" , desc) ) ;
245- err. span_label ( span, format ! ( "assignment to borrowed {} occurs here" , desc) ) ;
246- err
228+ self . infcx . tcx . sess . create_err ( AssignBorrowErr { desc, span, borrow_span } )
247229 }
248230
249231 pub ( crate ) fn cannot_reassign_immutable (
@@ -303,15 +285,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
303285 move_from_span : Span ,
304286 container_ty : Ty < ' _ > ,
305287 ) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
306- let mut err = struct_span_err ! (
307- self ,
308- move_from_span,
309- E0509 ,
310- "cannot move out of type `{}`, which implements the `Drop` trait" ,
311- container_ty,
312- ) ;
313- err. span_label ( move_from_span, "cannot move out of here" ) ;
314- err
288+ self . infcx . tcx . sess . create_err ( InteriorDropMoveErr { container_ty, move_from_span } )
315289 }
316290
317291 pub ( crate ) fn cannot_act_on_moved_value (
@@ -370,34 +344,22 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
370344 span : Span ,
371345 yield_span : Span ,
372346 ) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
373- let mut err = struct_span_err ! (
374- self ,
375- span,
376- E0626 ,
377- "borrow may still be in use when generator yields" ,
378- ) ;
379- err. span_label ( yield_span, "possible yield occurs here" ) ;
380- err
347+ self . infcx . tcx . sess . create_err ( BorrowAcrossGeneratorYield { span, yield_span } )
381348 }
382349
383350 pub ( crate ) fn cannot_borrow_across_destructor (
384351 & self ,
385352 borrow_span : Span ,
386353 ) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
387- struct_span_err ! (
388- self ,
389- borrow_span,
390- E0713 ,
391- "borrow may still be in use when destructor runs" ,
392- )
354+ self . infcx . tcx . sess . create_err ( BorrowAcrossDestructor { borrow_span } )
393355 }
394356
395357 pub ( crate ) fn path_does_not_live_long_enough (
396358 & self ,
397359 span : Span ,
398360 path : & str ,
399361 ) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
400- struct_span_err ! ( self , span , E0597 , "{} does not live long enough" , path, )
362+ self . infcx . tcx . sess . create_err ( PathShortLive { path, span } )
401363 }
402364
403365 pub ( crate ) fn cannot_return_reference_to_local (
0 commit comments