Migrate PanicNonStr, RawPtrComparison, RawPtrToInt diagnostics
This commit is contained in:
parent
c48f482813
commit
584e5d4c4f
3 changed files with 41 additions and 23 deletions
|
@ -39,3 +39,27 @@ pub(crate) struct StaticAccessErr {
|
||||||
#[help(const_eval::teach_help)]
|
#[help(const_eval::teach_help)]
|
||||||
pub teach: Option<()>,
|
pub teach: Option<()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(SessionDiagnostic)]
|
||||||
|
#[error(const_eval::raw_ptr_to_int)]
|
||||||
|
#[note]
|
||||||
|
#[note(const_eval::note2)]
|
||||||
|
pub(crate) struct RawPtrToIntErr {
|
||||||
|
#[primary_span]
|
||||||
|
pub span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(SessionDiagnostic)]
|
||||||
|
#[error(const_eval::raw_ptr_comparison)]
|
||||||
|
#[note]
|
||||||
|
pub(crate) struct RawPtrComparisonErr {
|
||||||
|
#[primary_span]
|
||||||
|
pub span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(SessionDiagnostic)]
|
||||||
|
#[error(const_eval::panic_non_str)]
|
||||||
|
pub(crate) struct PanicNonStrErr {
|
||||||
|
#[primary_span]
|
||||||
|
pub span: Span,
|
||||||
|
}
|
||||||
|
|
|
@ -22,7 +22,9 @@ use rustc_span::{BytePos, Pos, Span, Symbol};
|
||||||
use rustc_trait_selection::traits::SelectionContext;
|
use rustc_trait_selection::traits::SelectionContext;
|
||||||
|
|
||||||
use super::ConstCx;
|
use super::ConstCx;
|
||||||
use crate::errors::{NonConstOpErr, StaticAccessErr};
|
use crate::errors::{
|
||||||
|
NonConstOpErr, PanicNonStrErr, RawPtrComparisonErr, RawPtrToIntErr, StaticAccessErr,
|
||||||
|
};
|
||||||
use crate::util::{call_kind, CallDesugaringKind, CallKind};
|
use crate::util::{call_kind, CallDesugaringKind, CallKind};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
@ -642,10 +644,7 @@ impl<'tcx> NonConstOp<'tcx> for PanicNonStr {
|
||||||
ccx: &ConstCx<'_, 'tcx>,
|
ccx: &ConstCx<'_, 'tcx>,
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
||||||
ccx.tcx.sess.struct_span_err(
|
ccx.tcx.sess.create_err(PanicNonStrErr { span })
|
||||||
span,
|
|
||||||
"argument to `panic!()` in a const context must have type `&str`",
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -660,15 +659,7 @@ impl<'tcx> NonConstOp<'tcx> for RawPtrComparison {
|
||||||
ccx: &ConstCx<'_, 'tcx>,
|
ccx: &ConstCx<'_, 'tcx>,
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
||||||
let mut err = ccx
|
ccx.tcx.sess.create_err(RawPtrComparisonErr { span })
|
||||||
.tcx
|
|
||||||
.sess
|
|
||||||
.struct_span_err(span, "pointers cannot be reliably compared during const eval");
|
|
||||||
err.note(
|
|
||||||
"see issue #53020 <https://github.com/rust-lang/rust/issues/53020> \
|
|
||||||
for more information",
|
|
||||||
);
|
|
||||||
err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -704,15 +695,7 @@ impl<'tcx> NonConstOp<'tcx> for RawPtrToIntCast {
|
||||||
ccx: &ConstCx<'_, 'tcx>,
|
ccx: &ConstCx<'_, 'tcx>,
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
||||||
let mut err = ccx
|
ccx.tcx.sess.create_err(RawPtrToIntErr { span })
|
||||||
.tcx
|
|
||||||
.sess
|
|
||||||
.struct_span_err(span, "pointers cannot be cast to integers during const eval");
|
|
||||||
err.note("at compile-time, pointers do not have an integer value");
|
|
||||||
err.note(
|
|
||||||
"avoiding this restriction via `transmute`, `union`, or raw pointers leads to compile-time undefined behavior",
|
|
||||||
);
|
|
||||||
err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,3 +15,14 @@ const-eval-static-access =
|
||||||
.help = consider extracting the value of the `static` to a `const`, and referring to that
|
.help = consider extracting the value of the `static` to a `const`, and referring to that
|
||||||
.teach-note = `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
|
.teach-note = `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
|
||||||
.teach-help = To fix this, the value can be extracted to a `const` and then used.
|
.teach-help = To fix this, the value can be extracted to a `const` and then used.
|
||||||
|
|
||||||
|
const-eval-raw-ptr-to-int =
|
||||||
|
pointers cannot be cast to integers during const eval
|
||||||
|
.note = at compile-time, pointers do not have an integer value
|
||||||
|
.note2 = avoiding this restriction via `transmute`, `union`, or raw pointers leads to compile-time undefined behavior
|
||||||
|
|
||||||
|
const-eval-raw-ptr-comparison =
|
||||||
|
pointers cannot be reliably compared during const eval
|
||||||
|
.note = see issue #53020 <https://github.com/rust-lang/rust/issues/53020> for more information
|
||||||
|
|
||||||
|
const-eval-panic-non-str = argument to `panic!()` in a const context must have type `&str`
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue