11//! Concrete error types for all operations which may be invalid in a certain const context.
22
33use 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+ } ;
57use rustc_hir as hir;
68use rustc_hir:: def_id:: DefId ;
79use rustc_infer:: infer:: TyCtxtInferExt ;
@@ -20,6 +22,10 @@ use rustc_span::{BytePos, Pos, Span, Symbol};
2022use rustc_trait_selection:: traits:: SelectionContext ;
2123
2224use super :: ConstCx ;
25+ use crate :: errors:: {
26+ MutDerefErr , NonConstOpErr , PanicNonStrErr , RawPtrComparisonErr , RawPtrToIntErr ,
27+ StaticAccessErr , TransientMutBorrowErr , TransientMutBorrowErrRaw ,
28+ } ;
2329use crate :: util:: { call_kind, CallDesugaringKind , CallKind } ;
2430
2531#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
@@ -590,17 +596,17 @@ impl<'tcx> NonConstOp<'tcx> for TransientMutBorrow {
590596 ccx : & ConstCx < ' _ , ' tcx > ,
591597 span : Span ,
592598 ) -> 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+ }
604610 }
605611}
606612
@@ -621,12 +627,9 @@ impl<'tcx> NonConstOp<'tcx> for MutDeref {
621627 ccx : & ConstCx < ' _ , ' tcx > ,
622628 span : Span ,
623629 ) -> 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)
630633 }
631634}
632635
@@ -639,10 +642,7 @@ impl<'tcx> NonConstOp<'tcx> for PanicNonStr {
639642 ccx : & ConstCx < ' _ , ' tcx > ,
640643 span : Span ,
641644 ) -> 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 } )
646646 }
647647}
648648
@@ -657,15 +657,7 @@ impl<'tcx> NonConstOp<'tcx> for RawPtrComparison {
657657 ccx : & ConstCx < ' _ , ' tcx > ,
658658 span : Span ,
659659 ) -> 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 } )
669661 }
670662}
671663
@@ -701,15 +693,7 @@ impl<'tcx> NonConstOp<'tcx> for RawPtrToIntCast {
701693 ccx : & ConstCx < ' _ , ' tcx > ,
702694 span : Span ,
703695 ) -> 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 } )
713697 }
714698}
715699
@@ -730,24 +714,11 @@ impl<'tcx> NonConstOp<'tcx> for StaticAccess {
730714 ccx : & ConstCx < ' _ , ' tcx > ,
731715 span : Span ,
732716 ) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
733- let mut err = struct_span_err ! (
734- ccx. tcx. sess,
717+ ccx. tcx . sess . create_err ( StaticAccessErr {
735718 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+ } )
751722 }
752723}
753724
@@ -760,13 +731,7 @@ impl<'tcx> NonConstOp<'tcx> for ThreadLocalAccess {
760731 ccx : & ConstCx < ' _ , ' tcx > ,
761732 span : Span ,
762733 ) -> 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 } )
770735 }
771736}
772737
0 commit comments