@@ -7,7 +7,12 @@ use rustc_errors::{
7
7
use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
8
8
use rustc_span:: Span ;
9
9
10
- use crate :: session_diagnostics:: { TemporaryDroppedErr , ThreadLocalOutliveErr } ;
10
+ use crate :: session_diagnostics:: {
11
+ ActMovedValueErr , AssignBorrowErr , AssignErr , BorrowAcrossDestructor ,
12
+ BorrowAcrossGeneratorYield , ClosureVarOutliveErr , ImmuteArgAssign , ImmuteVarReassign ,
13
+ InteriorDropMoveErr , MovedOutErr , MutateInImmute , PathShortLive , ReturnRefLocalErr ,
14
+ TemporaryDroppedErr , ThreadLocalOutliveErr ,
15
+ } ;
11
16
12
17
impl < ' cx , ' tcx > crate :: MirBorrowckCtxt < ' cx , ' tcx > {
13
18
pub ( crate ) fn cannot_move_when_borrowed (
@@ -238,17 +243,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
238
243
borrow_span : Span ,
239
244
desc : & str ,
240
245
) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
241
- let mut err = struct_span_err ! (
242
- self ,
243
- span,
244
- E0506 ,
245
- "cannot assign to {} because it is borrowed" ,
246
- desc,
247
- ) ;
248
-
249
- err. span_label ( borrow_span, format ! ( "borrow of {} occurs here" , desc) ) ;
250
- err. span_label ( span, format ! ( "assignment to borrowed {} occurs here" , desc) ) ;
251
- err
246
+ self . infcx . tcx . sess . create_err ( AssignBorrowErr { desc, span, borrow_span } )
252
247
}
253
248
254
249
pub ( crate ) fn cannot_reassign_immutable (
@@ -257,24 +252,27 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
257
252
desc : & str ,
258
253
is_arg : bool ,
259
254
) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
260
- let msg = if is_arg { "to immutable argument" } else { "twice to immutable variable" } ;
261
- struct_span_err ! ( self , span, E0384 , "cannot assign {} {}" , msg, desc)
255
+ if is_arg {
256
+ self . infcx . tcx . sess . create_err ( ImmuteArgAssign { desc, span } )
257
+ } else {
258
+ self . infcx . tcx . sess . create_err ( ImmuteVarReassign { desc, span } )
259
+ }
262
260
}
263
261
264
262
pub ( crate ) fn cannot_assign (
265
263
& self ,
266
264
span : Span ,
267
265
desc : & str ,
268
266
) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
269
- struct_span_err ! ( self , span , E0594 , "cannot assign to {}" , desc )
267
+ self . infcx . tcx . sess . create_err ( AssignErr { desc , span } )
270
268
}
271
269
272
270
pub ( crate ) fn cannot_move_out_of (
273
271
& self ,
274
272
move_from_span : Span ,
275
273
move_from_desc : & str ,
276
274
) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
277
- struct_span_err ! ( self , move_from_span , E0507 , "cannot move out of {}" , move_from_desc , )
275
+ self . infcx . tcx . sess . create_err ( MovedOutErr { move_from_desc , move_from_span } )
278
276
}
279
277
280
278
/// Signal an error due to an attempt to move out of the interior
@@ -308,15 +306,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
308
306
move_from_span : Span ,
309
307
container_ty : Ty < ' _ > ,
310
308
) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
311
- let mut err = struct_span_err ! (
312
- self ,
313
- move_from_span,
314
- E0509 ,
315
- "cannot move out of type `{}`, which implements the `Drop` trait" ,
316
- container_ty,
317
- ) ;
318
- err. span_label ( move_from_span, "cannot move out of here" ) ;
319
- err
309
+ self . infcx . tcx . sess . create_err ( InteriorDropMoveErr { container_ty, move_from_span } )
320
310
}
321
311
322
312
pub ( crate ) fn cannot_act_on_moved_value (
@@ -327,18 +317,15 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
327
317
moved_path : Option < String > ,
328
318
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
329
319
let moved_path = moved_path. map ( |mp| format ! ( ": `{}`" , mp) ) . unwrap_or_default ( ) ;
330
-
331
- struct_span_err ! (
332
- self ,
333
- use_span,
334
- E0382 ,
335
- "{} of {}moved value{}" ,
320
+ self . infcx . tcx . sess . create_err ( ActMovedValueErr {
336
321
verb,
337
322
optional_adverb_for_moved,
338
323
moved_path,
339
- )
324
+ use_span,
325
+ } )
340
326
}
341
327
328
+ //FIXME: nested with other file, replace reason with subdiag.
342
329
pub ( crate ) fn cannot_borrow_path_as_mutable_because (
343
330
& self ,
344
331
span : Span ,
@@ -356,53 +343,36 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
356
343
immutable_section : & str ,
357
344
action : & str ,
358
345
) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
359
- let mut err = struct_span_err ! (
360
- self ,
361
- mutate_span,
362
- E0510 ,
363
- "cannot {} {} in {}" ,
346
+ self . infcx . tcx . sess . create_err ( MutateInImmute {
364
347
action,
365
348
immutable_place,
366
349
immutable_section,
367
- ) ;
368
- err. span_label ( mutate_span, format ! ( "cannot {}" , action) ) ;
369
- err. span_label ( immutable_span, format ! ( "value is immutable in {}" , immutable_section) ) ;
370
- err
350
+ mutate_span,
351
+ immutable_span,
352
+ } )
371
353
}
372
354
373
355
pub ( crate ) fn cannot_borrow_across_generator_yield (
374
356
& self ,
375
357
span : Span ,
376
358
yield_span : Span ,
377
359
) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
378
- let mut err = struct_span_err ! (
379
- self ,
380
- span,
381
- E0626 ,
382
- "borrow may still be in use when generator yields" ,
383
- ) ;
384
- err. span_label ( yield_span, "possible yield occurs here" ) ;
385
- err
360
+ self . infcx . tcx . sess . create_err ( BorrowAcrossGeneratorYield { span, yield_span } )
386
361
}
387
362
388
363
pub ( crate ) fn cannot_borrow_across_destructor (
389
364
& self ,
390
365
borrow_span : Span ,
391
366
) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
392
- struct_span_err ! (
393
- self ,
394
- borrow_span,
395
- E0713 ,
396
- "borrow may still be in use when destructor runs" ,
397
- )
367
+ self . infcx . tcx . sess . create_err ( BorrowAcrossDestructor { borrow_span } )
398
368
}
399
369
400
370
pub ( crate ) fn path_does_not_live_long_enough (
401
371
& self ,
402
372
span : Span ,
403
373
path : & str ,
404
374
) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
405
- struct_span_err ! ( self , span , E0597 , "{} does not live long enough" , path, )
375
+ self . infcx . tcx . sess . create_err ( PathShortLive { path, span } )
406
376
}
407
377
408
378
pub ( crate ) fn cannot_return_reference_to_local (
@@ -412,22 +382,12 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
412
382
reference_desc : & str ,
413
383
path_desc : & str ,
414
384
) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
415
- let mut err = struct_span_err ! (
416
- self ,
385
+ self . infcx . tcx . sess . create_err ( ReturnRefLocalErr {
386
+ return_kind,
387
+ reference : reference_desc,
388
+ local : path_desc,
417
389
span,
418
- E0515 ,
419
- "cannot {RETURN} {REFERENCE} {LOCAL}" ,
420
- RETURN = return_kind,
421
- REFERENCE = reference_desc,
422
- LOCAL = path_desc,
423
- ) ;
424
-
425
- err. span_label (
426
- span,
427
- format ! ( "{}s a {} data owned by the current function" , return_kind, reference_desc) ,
428
- ) ;
429
-
430
- err
390
+ } )
431
391
}
432
392
433
393
pub ( crate ) fn cannot_capture_in_long_lived_closure (
@@ -437,18 +397,12 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
437
397
borrowed_path : & str ,
438
398
capture_span : Span ,
439
399
) -> DiagnosticBuilder < ' cx , ErrorGuaranteed > {
440
- let mut err = struct_span_err ! (
441
- self ,
442
- closure_span,
443
- E0373 ,
444
- "{} may outlive the current function, but it borrows {}, which is owned by the current \
445
- function",
400
+ self . infcx . tcx . sess . create_err ( ClosureVarOutliveErr {
446
401
closure_kind,
447
402
borrowed_path,
448
- ) ;
449
- err. span_label ( capture_span, format ! ( "{} is borrowed here" , borrowed_path) )
450
- . span_label ( closure_span, format ! ( "may outlive borrowed value {}" , borrowed_path) ) ;
451
- err
403
+ closure_span,
404
+ capture_span,
405
+ } )
452
406
}
453
407
454
408
pub ( crate ) fn thread_local_value_does_not_live_long_enough (
0 commit comments