1
1
//! Concrete error types for all operations which may be invalid in a certain const context.
2
2
3
3
use hir:: def_id:: LocalDefId ;
4
- use rustc_errors:: { struct_span_err, Applicability , DiagnosticBuilder , ErrorGuaranteed } ;
4
+ use rustc_errors:: {
5
+ error_code, struct_span_err, Applicability , DiagnosticBuilder , ErrorGuaranteed ,
6
+ } ;
5
7
use rustc_hir as hir;
6
8
use rustc_hir:: def_id:: DefId ;
7
9
use rustc_infer:: infer:: TyCtxtInferExt ;
@@ -20,6 +22,10 @@ use rustc_span::{BytePos, Pos, Span, Symbol};
20
22
use rustc_trait_selection:: traits:: SelectionContext ;
21
23
22
24
use super :: ConstCx ;
25
+ use crate :: errors:: {
26
+ MutDerefErr , NonConstOpErr , PanicNonStrErr , RawPtrComparisonErr , RawPtrToIntErr ,
27
+ StaticAccessErr , TransientMutBorrowErr , TransientMutBorrowErrRaw ,
28
+ } ;
23
29
use crate :: util:: { call_kind, CallDesugaringKind , CallKind } ;
24
30
25
31
#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
@@ -590,17 +596,17 @@ impl<'tcx> NonConstOp<'tcx> for TransientMutBorrow {
590
596
ccx : & ConstCx < ' _ , ' tcx > ,
591
597
span : Span ,
592
598
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
593
- let raw = match self . 0 {
594
- hir :: BorrowKind :: Raw => "raw " ,
595
- hir:: BorrowKind :: Ref => "" ,
596
- } ;
597
-
598
- feature_err (
599
- & ccx. tcx . sess . parse_sess ,
600
- sym :: const_mut_refs ,
601
- span ,
602
- & format ! ( "{}mutable references are not allowed in {}s" , raw , ccx . const_kind ( ) ) ,
603
- )
599
+ let kind = ccx . const_kind ( ) ;
600
+ match self . 0 {
601
+ hir:: BorrowKind :: Raw => ccx
602
+ . tcx
603
+ . sess
604
+ . create_feature_err ( TransientMutBorrowErrRaw { span , kind } , sym :: const_mut_refs ) ,
605
+ hir :: BorrowKind :: Ref => ccx
606
+ . tcx
607
+ . sess
608
+ . create_feature_err ( TransientMutBorrowErr { span , kind } , sym :: const_mut_refs ) ,
609
+ }
604
610
}
605
611
}
606
612
@@ -621,12 +627,9 @@ impl<'tcx> NonConstOp<'tcx> for MutDeref {
621
627
ccx : & ConstCx < ' _ , ' tcx > ,
622
628
span : Span ,
623
629
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
624
- feature_err (
625
- & ccx. tcx . sess . parse_sess ,
626
- sym:: const_mut_refs,
627
- span,
628
- & format ! ( "mutation through a reference is not allowed in {}s" , ccx. const_kind( ) ) ,
629
- )
630
+ ccx. tcx
631
+ . sess
632
+ . create_feature_err ( MutDerefErr { span, kind : ccx. const_kind ( ) } , sym:: const_mut_refs)
630
633
}
631
634
}
632
635
@@ -639,10 +642,7 @@ impl<'tcx> NonConstOp<'tcx> for PanicNonStr {
639
642
ccx : & ConstCx < ' _ , ' tcx > ,
640
643
span : Span ,
641
644
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
642
- ccx. tcx . sess . struct_span_err (
643
- span,
644
- "argument to `panic!()` in a const context must have type `&str`" ,
645
- )
645
+ ccx. tcx . sess . create_err ( PanicNonStrErr { span } )
646
646
}
647
647
}
648
648
@@ -657,15 +657,7 @@ impl<'tcx> NonConstOp<'tcx> for RawPtrComparison {
657
657
ccx : & ConstCx < ' _ , ' tcx > ,
658
658
span : Span ,
659
659
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
660
- let mut err = ccx
661
- . tcx
662
- . sess
663
- . struct_span_err ( span, "pointers cannot be reliably compared during const eval" ) ;
664
- err. note (
665
- "see issue #53020 <https://github.com/rust-lang/rust/issues/53020> \
666
- for more information",
667
- ) ;
668
- err
660
+ ccx. tcx . sess . create_err ( RawPtrComparisonErr { span } )
669
661
}
670
662
}
671
663
@@ -701,15 +693,7 @@ impl<'tcx> NonConstOp<'tcx> for RawPtrToIntCast {
701
693
ccx : & ConstCx < ' _ , ' tcx > ,
702
694
span : Span ,
703
695
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
704
- let mut err = ccx
705
- . tcx
706
- . sess
707
- . struct_span_err ( span, "pointers cannot be cast to integers during const eval" ) ;
708
- err. note ( "at compile-time, pointers do not have an integer value" ) ;
709
- err. note (
710
- "avoiding this restriction via `transmute`, `union`, or raw pointers leads to compile-time undefined behavior" ,
711
- ) ;
712
- err
696
+ ccx. tcx . sess . create_err ( RawPtrToIntErr { span } )
713
697
}
714
698
}
715
699
@@ -730,24 +714,11 @@ impl<'tcx> NonConstOp<'tcx> for StaticAccess {
730
714
ccx : & ConstCx < ' _ , ' tcx > ,
731
715
span : Span ,
732
716
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
733
- let mut err = struct_span_err ! (
734
- ccx. tcx. sess,
717
+ ccx. tcx . sess . create_err ( StaticAccessErr {
735
718
span,
736
- E0013 ,
737
- "{}s cannot refer to statics" ,
738
- ccx. const_kind( )
739
- ) ;
740
- err. help (
741
- "consider extracting the value of the `static` to a `const`, and referring to that" ,
742
- ) ;
743
- if ccx. tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
744
- err. note (
745
- "`static` and `const` variables can refer to other `const` variables. \
746
- A `const` variable, however, cannot refer to a `static` variable.",
747
- ) ;
748
- err. help ( "To fix this, the value can be extracted to a `const` and then used." ) ;
749
- }
750
- err
719
+ kind : ccx. const_kind ( ) ,
720
+ teach : ccx. tcx . sess . teach ( & error_code ! ( E0013 ) ) . then_some ( ( ) ) ,
721
+ } )
751
722
}
752
723
}
753
724
@@ -760,13 +731,7 @@ impl<'tcx> NonConstOp<'tcx> for ThreadLocalAccess {
760
731
ccx : & ConstCx < ' _ , ' tcx > ,
761
732
span : Span ,
762
733
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
763
- struct_span_err ! (
764
- ccx. tcx. sess,
765
- span,
766
- E0625 ,
767
- "thread-local statics cannot be \
768
- accessed at compile-time"
769
- )
734
+ ccx. tcx . sess . create_err ( NonConstOpErr { span } )
770
735
}
771
736
}
772
737
0 commit comments