Skip to content

Commit 584e5d4

Browse files
Migrate PanicNonStr, RawPtrComparison, RawPtrToInt diagnostics
1 parent c48f482 commit 584e5d4

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

compiler/rustc_const_eval/src/errors.rs

+24
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,27 @@ pub(crate) struct StaticAccessErr {
3939
#[help(const_eval::teach_help)]
4040
pub teach: Option<()>,
4141
}
42+
43+
#[derive(SessionDiagnostic)]
44+
#[error(const_eval::raw_ptr_to_int)]
45+
#[note]
46+
#[note(const_eval::note2)]
47+
pub(crate) struct RawPtrToIntErr {
48+
#[primary_span]
49+
pub span: Span,
50+
}
51+
52+
#[derive(SessionDiagnostic)]
53+
#[error(const_eval::raw_ptr_comparison)]
54+
#[note]
55+
pub(crate) struct RawPtrComparisonErr {
56+
#[primary_span]
57+
pub span: Span,
58+
}
59+
60+
#[derive(SessionDiagnostic)]
61+
#[error(const_eval::panic_non_str)]
62+
pub(crate) struct PanicNonStrErr {
63+
#[primary_span]
64+
pub span: Span,
65+
}

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+6-23
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ use rustc_span::{BytePos, Pos, Span, Symbol};
2222
use rustc_trait_selection::traits::SelectionContext;
2323

2424
use super::ConstCx;
25-
use crate::errors::{NonConstOpErr, StaticAccessErr};
25+
use crate::errors::{
26+
NonConstOpErr, PanicNonStrErr, RawPtrComparisonErr, RawPtrToIntErr, StaticAccessErr,
27+
};
2628
use crate::util::{call_kind, CallDesugaringKind, CallKind};
2729

2830
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -642,10 +644,7 @@ impl<'tcx> NonConstOp<'tcx> for PanicNonStr {
642644
ccx: &ConstCx<'_, 'tcx>,
643645
span: Span,
644646
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
645-
ccx.tcx.sess.struct_span_err(
646-
span,
647-
"argument to `panic!()` in a const context must have type `&str`",
648-
)
647+
ccx.tcx.sess.create_err(PanicNonStrErr { span })
649648
}
650649
}
651650

@@ -660,15 +659,7 @@ impl<'tcx> NonConstOp<'tcx> for RawPtrComparison {
660659
ccx: &ConstCx<'_, 'tcx>,
661660
span: Span,
662661
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
663-
let mut err = ccx
664-
.tcx
665-
.sess
666-
.struct_span_err(span, "pointers cannot be reliably compared during const eval");
667-
err.note(
668-
"see issue #53020 <https://github.com/rust-lang/rust/issues/53020> \
669-
for more information",
670-
);
671-
err
662+
ccx.tcx.sess.create_err(RawPtrComparisonErr { span })
672663
}
673664
}
674665

@@ -704,15 +695,7 @@ impl<'tcx> NonConstOp<'tcx> for RawPtrToIntCast {
704695
ccx: &ConstCx<'_, 'tcx>,
705696
span: Span,
706697
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
707-
let mut err = ccx
708-
.tcx
709-
.sess
710-
.struct_span_err(span, "pointers cannot be cast to integers during const eval");
711-
err.note("at compile-time, pointers do not have an integer value");
712-
err.note(
713-
"avoiding this restriction via `transmute`, `union`, or raw pointers leads to compile-time undefined behavior",
714-
);
715-
err
698+
ccx.tcx.sess.create_err(RawPtrToIntErr { span })
716699
}
717700
}
718701

compiler/rustc_error_messages/locales/en-US/const_eval.ftl

+11
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,14 @@ const-eval-static-access =
1515
.help = consider extracting the value of the `static` to a `const`, and referring to that
1616
.teach-note = `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
1717
.teach-help = To fix this, the value can be extracted to a `const` and then used.
18+
19+
const-eval-raw-ptr-to-int =
20+
pointers cannot be cast to integers during const eval
21+
.note = at compile-time, pointers do not have an integer value
22+
.note2 = avoiding this restriction via `transmute`, `union`, or raw pointers leads to compile-time undefined behavior
23+
24+
const-eval-raw-ptr-comparison =
25+
pointers cannot be reliably compared during const eval
26+
.note = see issue #53020 <https://github.com/rust-lang/rust/issues/53020> for more information
27+
28+
const-eval-panic-non-str = argument to `panic!()` in a const context must have type `&str`

0 commit comments

Comments
 (0)