@@ -4,13 +4,19 @@ use rustc_errors::{
4
4
use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
5
5
use rustc_span:: Span ;
6
6
7
+ use crate :: session_diagnostics:: {
8
+ AssignBorrowErr , BorrowAcrossDestructor , BorrowAcrossGeneratorYield , ClosureConstructLabel ,
9
+ InteriorDropMoveErr , MoveBorrowedErr , PathShortLive , TwoClosuresUniquelyBorrowErr ,
10
+ UseMutBorrowErr ,
11
+ } ;
12
+
7
13
impl < ' cx , ' tcx > crate :: MirBorrowckCtxt < ' cx , ' tcx > {
8
14
pub ( crate ) fn cannot_move_when_borrowed (
9
15
& self ,
10
16
span : Span ,
11
17
desc : & str ,
12
18
) -> 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 } )
14
20
}
15
21
16
22
pub ( crate ) fn cannot_use_when_mutably_borrowed (
@@ -20,17 +26,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
20
26
borrow_span : Span ,
21
27
borrow_desc : & str ,
22
28
) -> 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 } )
34
30
}
35
31
36
32
pub ( crate ) fn cannot_mutably_borrow_multiply (
@@ -90,26 +86,22 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
90
86
old_loan_span : Span ,
91
87
old_load_end_span : Option < Span > ,
92
88
) -> 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 > ;
100
91
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 ;
105
94
} 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 ) ;
108
97
}
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
+ } )
113
105
}
114
106
115
107
pub ( crate ) fn cannot_uniquely_borrow_by_one_closure (
@@ -233,17 +225,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
233
225
borrow_span : Span ,
234
226
desc : & str ,
235
227
) -> 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 } )
247
229
}
248
230
249
231
pub ( crate ) fn cannot_reassign_immutable (
@@ -303,15 +285,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
303
285
move_from_span : Span ,
304
286
container_ty : Ty < ' _ > ,
305
287
) -> 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 } )
315
289
}
316
290
317
291
pub ( crate ) fn cannot_act_on_moved_value (
@@ -370,34 +344,22 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
370
344
span : Span ,
371
345
yield_span : Span ,
372
346
) -> 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 } )
381
348
}
382
349
383
350
pub ( crate ) fn cannot_borrow_across_destructor (
384
351
& self ,
385
352
borrow_span : Span ,
386
353
) -> 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 } )
393
355
}
394
356
395
357
pub ( crate ) fn path_does_not_live_long_enough (
396
358
& self ,
397
359
span : Span ,
398
360
path : & str ,
399
361
) -> 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 } )
401
363
}
402
364
403
365
pub ( crate ) fn cannot_return_reference_to_local (
0 commit comments