improve error message for disallowed ptr-to-int casts in const eval
This commit is contained in:
parent
6a388dcfbb
commit
ff315e34e8
4 changed files with 35 additions and 1 deletions
|
@ -16,6 +16,7 @@ use crate::interpret::{
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum ConstEvalErrKind {
|
pub enum ConstEvalErrKind {
|
||||||
NeedsRfc(String),
|
NeedsRfc(String),
|
||||||
|
PtrToIntCast,
|
||||||
ConstAccessesStatic,
|
ConstAccessesStatic,
|
||||||
ModifiedGlobal,
|
ModifiedGlobal,
|
||||||
AssertFailure(AssertKind<ConstInt>),
|
AssertFailure(AssertKind<ConstInt>),
|
||||||
|
@ -39,6 +40,12 @@ impl fmt::Display for ConstEvalErrKind {
|
||||||
NeedsRfc(ref msg) => {
|
NeedsRfc(ref msg) => {
|
||||||
write!(f, "\"{}\" needs an rfc before being allowed inside constants", msg)
|
write!(f, "\"{}\" needs an rfc before being allowed inside constants", msg)
|
||||||
}
|
}
|
||||||
|
PtrToIntCast => {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"cannot cast pointer to integer because it was not created by cast from integer"
|
||||||
|
)
|
||||||
|
}
|
||||||
ConstAccessesStatic => write!(f, "constant accesses static"),
|
ConstAccessesStatic => write!(f, "constant accesses static"),
|
||||||
ModifiedGlobal => {
|
ModifiedGlobal => {
|
||||||
write!(f, "modifying a static's initial value from another static's initializer")
|
write!(f, "modifying a static's initial value from another static's initializer")
|
||||||
|
|
|
@ -352,7 +352,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ptr_to_int(_mem: &Memory<'mir, 'tcx, Self>, _ptr: Pointer) -> InterpResult<'tcx, u64> {
|
fn ptr_to_int(_mem: &Memory<'mir, 'tcx, Self>, _ptr: Pointer) -> InterpResult<'tcx, u64> {
|
||||||
Err(ConstEvalErrKind::NeedsRfc("pointer-to-integer cast".to_string()).into())
|
Err(ConstEvalErrKind::PtrToIntCast.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn binary_ptr_op(
|
fn binary_ptr_op(
|
||||||
|
|
13
src/test/ui/const-ptr/ptr_to_usize_cast.rs
Normal file
13
src/test/ui/const-ptr/ptr_to_usize_cast.rs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#![feature(const_raw_ptr_to_usize_cast)]
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
const OK: usize = unsafe { 0 as *const i32 as usize };
|
||||||
|
|
||||||
|
const _ERROR: usize = unsafe { &0 as *const i32 as usize };
|
||||||
|
//~^ ERROR [const_err]
|
||||||
|
//~| NOTE cannot cast pointer to integer because it was not created by cast from integer
|
||||||
|
//~| NOTE
|
||||||
|
//~| NOTE `#[deny(const_err)]` on by default
|
||||||
|
//~| WARN this was previously accepted by the compiler but is being phased out
|
||||||
|
//~| NOTE see issue #71800
|
||||||
|
}
|
14
src/test/ui/const-ptr/ptr_to_usize_cast.stderr
Normal file
14
src/test/ui/const-ptr/ptr_to_usize_cast.stderr
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
error: any use of this value will cause an error
|
||||||
|
--> $DIR/ptr_to_usize_cast.rs:6:36
|
||||||
|
|
|
||||||
|
LL | const _ERROR: usize = unsafe { &0 as *const i32 as usize };
|
||||||
|
| -------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^---
|
||||||
|
| |
|
||||||
|
| cannot cast pointer to integer because it was not created by cast from integer
|
||||||
|
|
|
||||||
|
= note: `#[deny(const_err)]` on by default
|
||||||
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
|
||||||
|
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue