@@ -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 , UnallowedFnPointerCall , UnstableConstFn ,
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
@@ -305,22 +308,13 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
305
308
err
306
309
}
307
310
_ if tcx. opt_parent ( callee) == tcx. get_diagnostic_item ( sym:: ArgumentV1Methods ) => {
308
- struct_span_err ! (
309
- ccx. tcx. sess,
310
- span,
311
- E0015 ,
312
- "cannot call non-const formatting macro in {}s" ,
313
- ccx. const_kind( ) ,
314
- )
311
+ ccx. tcx . sess . create_err ( NonConstFmtMacroCall { span, kind : ccx. const_kind ( ) } )
315
312
}
316
- _ => struct_span_err ! (
317
- ccx. tcx. sess,
313
+ _ => ccx. tcx . sess . create_err ( NonConstFnCall {
318
314
span,
319
- E0015 ,
320
- "cannot call non-const fn `{}` in {}s" ,
321
- ccx. tcx. def_path_str_with_substs( callee, substs) ,
322
- ccx. const_kind( ) ,
323
- ) ,
315
+ def_path_str : ccx. tcx . def_path_str_with_substs ( callee, substs) ,
316
+ kind : ccx. const_kind ( ) ,
317
+ } ) ,
324
318
} ;
325
319
326
320
err. note ( & format ! (
@@ -387,9 +381,12 @@ impl<'tcx> NonConstOp<'tcx> for Generator {
387
381
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
388
382
let msg = format ! ( "{}s are not allowed in {}s" , self . 0 , ccx. const_kind( ) ) ;
389
383
if let hir:: GeneratorKind :: Async ( hir:: AsyncGeneratorKind :: Block ) = self . 0 {
390
- feature_err ( & ccx. tcx . sess . parse_sess , sym:: const_async_blocks, span, & msg)
384
+ ccx. tcx . sess . create_feature_err (
385
+ UnallowedOpInConstContext { span, msg } ,
386
+ sym:: const_async_blocks,
387
+ )
391
388
} else {
392
- ccx. tcx . sess . struct_span_err ( span, & msg)
389
+ ccx. tcx . sess . create_err ( UnallowedOpInConstContext { span, msg } )
393
390
}
394
391
}
395
392
}
@@ -402,23 +399,11 @@ impl<'tcx> NonConstOp<'tcx> for HeapAllocation {
402
399
ccx : & ConstCx < ' _ , ' tcx > ,
403
400
span : Span ,
404
401
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
405
- let mut err = struct_span_err ! (
406
- ccx. tcx. sess,
402
+ ccx. tcx . sess . create_err ( UnallowedHeapAllocations {
407
403
span,
408
- E0010 ,
409
- "allocations are not allowed in {}s" ,
410
- ccx. const_kind( )
411
- ) ;
412
- err. span_label ( span, format ! ( "allocation not allowed in {}s" , ccx. const_kind( ) ) ) ;
413
- if ccx. tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
414
- err. note (
415
- "The value of statics and constants must be known at compile time, \
416
- and they live for the entire lifetime of a program. Creating a boxed \
417
- value allocates memory on the heap at runtime, and therefore cannot \
418
- be done at compile time.",
419
- ) ;
420
- }
421
- err
404
+ kind : ccx. const_kind ( ) ,
405
+ teach : ccx. tcx . sess . teach ( & error_code ! ( E0010 ) ) . then_some ( ( ) ) ,
406
+ } )
422
407
}
423
408
}
424
409
@@ -430,13 +415,7 @@ impl<'tcx> NonConstOp<'tcx> for InlineAsm {
430
415
ccx : & ConstCx < ' _ , ' tcx > ,
431
416
span : Span ,
432
417
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
433
- struct_span_err ! (
434
- ccx. tcx. sess,
435
- span,
436
- E0015 ,
437
- "inline assembly is not allowed in {}s" ,
438
- ccx. const_kind( )
439
- )
418
+ ccx. tcx . sess . create_err ( UnallowedInlineAsm { span, kind : ccx. const_kind ( ) } )
440
419
}
441
420
}
442
421
@@ -482,12 +461,7 @@ impl<'tcx> NonConstOp<'tcx> for TransientCellBorrow {
482
461
ccx : & ConstCx < ' _ , ' tcx > ,
483
462
span : Span ,
484
463
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
485
- feature_err (
486
- & ccx. tcx . sess . parse_sess ,
487
- sym:: const_refs_to_cell,
488
- span,
489
- "cannot borrow here, since the borrowed element may contain interior mutability" ,
490
- )
464
+ ccx. tcx . sess . create_feature_err ( InteriorMutabilityBorrow { span } , sym:: const_refs_to_cell)
491
465
}
492
466
}
493
467
@@ -502,32 +476,22 @@ impl<'tcx> NonConstOp<'tcx> for CellBorrow {
502
476
ccx : & ConstCx < ' _ , ' tcx > ,
503
477
span : Span ,
504
478
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
505
- let mut err = struct_span_err ! (
506
- ccx. tcx. sess,
507
- span,
508
- E0492 ,
509
- "{}s cannot refer to interior mutable data" ,
510
- ccx. const_kind( ) ,
511
- ) ;
512
- err. span_label (
513
- span,
514
- "this borrow of an interior mutable value may end up in the final value" ,
515
- ) ;
479
+ // FIXME: Maybe a more elegant solution to this if else case
516
480
if let hir:: ConstContext :: Static ( _) = ccx. const_kind ( ) {
517
- err. help (
518
- "to fix this, the value can be extracted to a separate \
519
- `static` item and then referenced",
520
- ) ;
521
- }
522
- if ccx. tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
523
- err. note (
524
- "A constant containing interior mutable data behind a reference can allow you
525
- to modify that data. This would make multiple uses of a constant to be able to
526
- see different values and allow circumventing the `Send` and `Sync` requirements
527
- for shared mutable data, which is unsound." ,
528
- ) ;
481
+ ccx. tcx . sess . create_err ( InteriorMutableDataRefer {
482
+ span,
483
+ opt_help : Some ( ( ) ) ,
484
+ kind : ccx. const_kind ( ) ,
485
+ teach : ccx. tcx . sess . teach ( & error_code ! ( E0492 ) ) . then_some ( ( ) ) ,
486
+ } )
487
+ } else {
488
+ ccx. tcx . sess . create_err ( InteriorMutableDataRefer {
489
+ span,
490
+ opt_help : None ,
491
+ kind : ccx. const_kind ( ) ,
492
+ teach : ccx. tcx . sess . teach ( & error_code ! ( E0492 ) ) . then_some ( ( ) ) ,
493
+ } )
529
494
}
530
- err
531
495
}
532
496
}
533
497
@@ -553,33 +517,29 @@ impl<'tcx> NonConstOp<'tcx> for MutBorrow {
553
517
ccx : & ConstCx < ' _ , ' tcx > ,
554
518
span : Span ,
555
519
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
556
- let raw = match self . 0 {
557
- hir:: BorrowKind :: Raw => "raw " ,
558
- hir:: BorrowKind :: Ref => "" ,
559
- } ;
560
-
561
- let mut err = struct_span_err ! (
562
- ccx. tcx. sess,
563
- span,
564
- E0764 ,
565
- "{}mutable references are not allowed in the final value of {}s" ,
566
- raw,
567
- ccx. const_kind( ) ,
568
- ) ;
569
-
570
- if ccx. tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
571
- err. note (
572
- "References in statics and constants may only refer \
573
- to immutable values.\n \n \
574
- Statics are shared everywhere, and if they refer to \
575
- mutable data one might violate memory safety since \
576
- holding multiple mutable references to shared data \
577
- is not allowed.\n \n \
578
- If you really want global mutable state, try using \
579
- static mut or a global UnsafeCell.",
580
- ) ;
520
+ // let raw = match self.0 {
521
+ // hir::BorrowKind::Raw => "raw ",
522
+ // hir::BorrowKind::Ref => "",
523
+ // };
524
+
525
+ // ccx.tcx.sess.create_err(UnallowedMutableRefs {
526
+ // span,
527
+ // raw,
528
+ // kind: ccx.const_kind(),
529
+ // teach: ccx.tcx.sess.teach(&error_code!(E0764)).then_some(()),
530
+ // })
531
+ match self . 0 {
532
+ hir:: BorrowKind :: Raw => ccx. tcx . sess . create_err ( UnallowedMutableRefsRaw {
533
+ span,
534
+ kind : ccx. const_kind ( ) ,
535
+ teach : ccx. tcx . sess . teach ( & error_code ! ( E0764 ) ) . then_some ( ( ) ) ,
536
+ } ) ,
537
+ hir:: BorrowKind :: Ref => ccx. tcx . sess . create_err ( UnallowedMutableRefs {
538
+ span,
539
+ kind : ccx. const_kind ( ) ,
540
+ teach : ccx. tcx . sess . teach ( & error_code ! ( E0764 ) ) . then_some ( ( ) ) ,
541
+ } ) ,
581
542
}
582
- err
583
543
}
584
544
}
585
545
0 commit comments