@@ -24,8 +24,11 @@ use rustc_trait_selection::traits::SelectionContext;
24
24
25
25
use super :: ConstCx ;
26
26
use crate :: errors:: {
27
- MutDerefErr , NonConstOpErr , PanicNonStrErr , RawPtrToIntErr , StaticAccessErr ,
28
- TransientMutBorrowErr , TransientMutBorrowErrRaw ,
27
+ InteriorMutabilityBorrow , InteriorMutableDataRefer , MutDerefErr , NonConstFmtMacroCall ,
28
+ NonConstFnCall , NonConstOpErr , PanicNonStrErr , RawPtrToIntErr , StaticAccessErr ,
29
+ TransientMutBorrowErr , TransientMutBorrowErrRaw , UnallowedFnPointerCall ,
30
+ UnallowedHeapAllocations , UnallowedInlineAsm , UnallowedMutableRefs , UnallowedMutableRefsRaw ,
31
+ UnallowedOpInConstContext , UnstableConstFn ,
29
32
} ;
30
33
use crate :: util:: { call_kind, CallDesugaringKind , CallKind } ;
31
34
@@ -97,10 +100,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallIndirect {
97
100
ccx : & ConstCx < ' _ , ' tcx > ,
98
101
span : Span ,
99
102
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
100
- ccx. tcx . sess . struct_span_err (
101
- span,
102
- & format ! ( "function pointer calls are not allowed in {}s" , ccx. const_kind( ) ) ,
103
- )
103
+ ccx. tcx . sess . create_err ( UnallowedFnPointerCall { span, kind : ccx. const_kind ( ) } )
104
104
}
105
105
}
106
106
@@ -308,22 +308,13 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
308
308
err
309
309
}
310
310
_ if tcx. opt_parent ( callee) == tcx. get_diagnostic_item ( sym:: ArgumentV1Methods ) => {
311
- struct_span_err ! (
312
- ccx. tcx. sess,
313
- span,
314
- E0015 ,
315
- "cannot call non-const formatting macro in {}s" ,
316
- ccx. const_kind( ) ,
317
- )
311
+ ccx. tcx . sess . create_err ( NonConstFmtMacroCall { span, kind : ccx. const_kind ( ) } )
318
312
}
319
- _ => struct_span_err ! (
320
- ccx. tcx. sess,
313
+ _ => ccx. tcx . sess . create_err ( NonConstFnCall {
321
314
span,
322
- E0015 ,
323
- "cannot call non-const fn `{}` in {}s" ,
324
- ccx. tcx. def_path_str_with_substs( callee, substs) ,
325
- ccx. const_kind( ) ,
326
- ) ,
315
+ def_path_str : ccx. tcx . def_path_str_with_substs ( callee, substs) ,
316
+ kind : ccx. const_kind ( ) ,
317
+ } ) ,
327
318
} ;
328
319
329
320
err. note ( & format ! (
@@ -354,10 +345,10 @@ impl<'tcx> NonConstOp<'tcx> for FnCallUnstable {
354
345
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
355
346
let FnCallUnstable ( def_id, feature) = * self ;
356
347
357
- let mut err = ccx. tcx . sess . struct_span_err (
358
- span ,
359
- & format ! ( "`{}` is not yet stable as a const fn" , ccx . tcx . def_path_str ( def_id ) ) ,
360
- ) ;
348
+ let mut err = ccx
349
+ . tcx
350
+ . sess
351
+ . create_err ( UnstableConstFn { span , def_path : ccx . tcx . def_path_str ( def_id ) } ) ;
361
352
362
353
if ccx. is_const_stable_const_fn ( ) {
363
354
err. help ( "const-stable functions can only call other const-stable functions" ) ;
@@ -392,9 +383,12 @@ impl<'tcx> NonConstOp<'tcx> for Generator {
392
383
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
393
384
let msg = format ! ( "{}s are not allowed in {}s" , self . 0 , ccx. const_kind( ) ) ;
394
385
if let hir:: GeneratorKind :: Async ( hir:: AsyncGeneratorKind :: Block ) = self . 0 {
395
- feature_err ( & ccx. tcx . sess . parse_sess , sym:: const_async_blocks, span, & msg)
386
+ ccx. tcx . sess . create_feature_err (
387
+ UnallowedOpInConstContext { span, msg } ,
388
+ sym:: const_async_blocks,
389
+ )
396
390
} else {
397
- ccx. tcx . sess . struct_span_err ( span, & msg)
391
+ ccx. tcx . sess . create_err ( UnallowedOpInConstContext { span, msg } )
398
392
}
399
393
}
400
394
}
@@ -407,23 +401,11 @@ impl<'tcx> NonConstOp<'tcx> for HeapAllocation {
407
401
ccx : & ConstCx < ' _ , ' tcx > ,
408
402
span : Span ,
409
403
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
410
- let mut err = struct_span_err ! (
411
- ccx. tcx. sess,
404
+ ccx. tcx . sess . create_err ( UnallowedHeapAllocations {
412
405
span,
413
- E0010 ,
414
- "allocations are not allowed in {}s" ,
415
- ccx. const_kind( )
416
- ) ;
417
- err. span_label ( span, format ! ( "allocation not allowed in {}s" , ccx. const_kind( ) ) ) ;
418
- if ccx. tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
419
- err. note (
420
- "The value of statics and constants must be known at compile time, \
421
- and they live for the entire lifetime of a program. Creating a boxed \
422
- value allocates memory on the heap at runtime, and therefore cannot \
423
- be done at compile time.",
424
- ) ;
425
- }
426
- err
406
+ kind : ccx. const_kind ( ) ,
407
+ teach : ccx. tcx . sess . teach ( & error_code ! ( E0010 ) ) . then_some ( ( ) ) ,
408
+ } )
427
409
}
428
410
}
429
411
@@ -435,13 +417,7 @@ impl<'tcx> NonConstOp<'tcx> for InlineAsm {
435
417
ccx : & ConstCx < ' _ , ' tcx > ,
436
418
span : Span ,
437
419
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
438
- struct_span_err ! (
439
- ccx. tcx. sess,
440
- span,
441
- E0015 ,
442
- "inline assembly is not allowed in {}s" ,
443
- ccx. const_kind( )
444
- )
420
+ ccx. tcx . sess . create_err ( UnallowedInlineAsm { span, kind : ccx. const_kind ( ) } )
445
421
}
446
422
}
447
423
@@ -487,12 +463,7 @@ impl<'tcx> NonConstOp<'tcx> for TransientCellBorrow {
487
463
ccx : & ConstCx < ' _ , ' tcx > ,
488
464
span : Span ,
489
465
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
490
- feature_err (
491
- & ccx. tcx . sess . parse_sess ,
492
- sym:: const_refs_to_cell,
493
- span,
494
- "cannot borrow here, since the borrowed element may contain interior mutability" ,
495
- )
466
+ ccx. tcx . sess . create_feature_err ( InteriorMutabilityBorrow { span } , sym:: const_refs_to_cell)
496
467
}
497
468
}
498
469
@@ -507,32 +478,22 @@ impl<'tcx> NonConstOp<'tcx> for CellBorrow {
507
478
ccx : & ConstCx < ' _ , ' tcx > ,
508
479
span : Span ,
509
480
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
510
- let mut err = struct_span_err ! (
511
- ccx. tcx. sess,
512
- span,
513
- E0492 ,
514
- "{}s cannot refer to interior mutable data" ,
515
- ccx. const_kind( ) ,
516
- ) ;
517
- err. span_label (
518
- span,
519
- "this borrow of an interior mutable value may end up in the final value" ,
520
- ) ;
481
+ // FIXME: Maybe a more elegant solution to this if else case
521
482
if let hir:: ConstContext :: Static ( _) = ccx. const_kind ( ) {
522
- err. help (
523
- "to fix this, the value can be extracted to a separate \
524
- `static` item and then referenced",
525
- ) ;
526
- }
527
- if ccx. tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
528
- err. note (
529
- "A constant containing interior mutable data behind a reference can allow you
530
- to modify that data. This would make multiple uses of a constant to be able to
531
- see different values and allow circumventing the `Send` and `Sync` requirements
532
- for shared mutable data, which is unsound." ,
533
- ) ;
483
+ ccx. tcx . sess . create_err ( InteriorMutableDataRefer {
484
+ span,
485
+ opt_help : Some ( ( ) ) ,
486
+ kind : ccx. const_kind ( ) ,
487
+ teach : ccx. tcx . sess . teach ( & error_code ! ( E0492 ) ) . then_some ( ( ) ) ,
488
+ } )
489
+ } else {
490
+ ccx. tcx . sess . create_err ( InteriorMutableDataRefer {
491
+ span,
492
+ opt_help : None ,
493
+ kind : ccx. const_kind ( ) ,
494
+ teach : ccx. tcx . sess . teach ( & error_code ! ( E0492 ) ) . then_some ( ( ) ) ,
495
+ } )
534
496
}
535
- err
536
497
}
537
498
}
538
499
@@ -558,33 +519,18 @@ impl<'tcx> NonConstOp<'tcx> for MutBorrow {
558
519
ccx : & ConstCx < ' _ , ' tcx > ,
559
520
span : Span ,
560
521
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
561
- let raw = match self . 0 {
562
- hir:: BorrowKind :: Raw => "raw " ,
563
- hir:: BorrowKind :: Ref => "" ,
564
- } ;
565
-
566
- let mut err = struct_span_err ! (
567
- ccx. tcx. sess,
568
- span,
569
- E0764 ,
570
- "{}mutable references are not allowed in the final value of {}s" ,
571
- raw,
572
- ccx. const_kind( ) ,
573
- ) ;
574
-
575
- if ccx. tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
576
- err. note (
577
- "References in statics and constants may only refer \
578
- to immutable values.\n \n \
579
- Statics are shared everywhere, and if they refer to \
580
- mutable data one might violate memory safety since \
581
- holding multiple mutable references to shared data \
582
- is not allowed.\n \n \
583
- If you really want global mutable state, try using \
584
- static mut or a global UnsafeCell.",
585
- ) ;
522
+ match self . 0 {
523
+ hir:: BorrowKind :: Raw => ccx. tcx . sess . create_err ( UnallowedMutableRefsRaw {
524
+ span,
525
+ kind : ccx. const_kind ( ) ,
526
+ teach : ccx. tcx . sess . teach ( & error_code ! ( E0764 ) ) . then_some ( ( ) ) ,
527
+ } ) ,
528
+ hir:: BorrowKind :: Ref => ccx. tcx . sess . create_err ( UnallowedMutableRefs {
529
+ span,
530
+ kind : ccx. const_kind ( ) ,
531
+ teach : ccx. tcx . sess . teach ( & error_code ! ( E0764 ) ) . then_some ( ( ) ) ,
532
+ } ) ,
586
533
}
587
- err
588
534
}
589
535
}
590
536
0 commit comments