From ff315e34e8cfb9766b10f7740c8720e56f6d3475 Mon Sep 17 00:00:00 2001 From: Jeffrey Griffin Date: Thu, 4 Feb 2021 17:43:46 -0800 Subject: [PATCH] improve error message for disallowed ptr-to-int casts in const eval --- compiler/rustc_mir/src/const_eval/error.rs | 7 +++++++ compiler/rustc_mir/src/const_eval/machine.rs | 2 +- src/test/ui/const-ptr/ptr_to_usize_cast.rs | 13 +++++++++++++ src/test/ui/const-ptr/ptr_to_usize_cast.stderr | 14 ++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/const-ptr/ptr_to_usize_cast.rs create mode 100644 src/test/ui/const-ptr/ptr_to_usize_cast.stderr diff --git a/compiler/rustc_mir/src/const_eval/error.rs b/compiler/rustc_mir/src/const_eval/error.rs index 0e610e37552..88af9391cad 100644 --- a/compiler/rustc_mir/src/const_eval/error.rs +++ b/compiler/rustc_mir/src/const_eval/error.rs @@ -16,6 +16,7 @@ #[derive(Clone, Debug)] pub enum ConstEvalErrKind { NeedsRfc(String), + PtrToIntCast, ConstAccessesStatic, ModifiedGlobal, AssertFailure(AssertKind), @@ -39,6 +40,12 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { NeedsRfc(ref 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"), ModifiedGlobal => { write!(f, "modifying a static's initial value from another static's initializer") diff --git a/compiler/rustc_mir/src/const_eval/machine.rs b/compiler/rustc_mir/src/const_eval/machine.rs index 49126cfec6b..f6b950c08c7 100644 --- a/compiler/rustc_mir/src/const_eval/machine.rs +++ b/compiler/rustc_mir/src/const_eval/machine.rs @@ -352,7 +352,7 @@ fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>, msg: String) -> InterpResult<'tc } 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( diff --git a/src/test/ui/const-ptr/ptr_to_usize_cast.rs b/src/test/ui/const-ptr/ptr_to_usize_cast.rs new file mode 100644 index 00000000000..bf1e790b5dc --- /dev/null +++ b/src/test/ui/const-ptr/ptr_to_usize_cast.rs @@ -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 +} diff --git a/src/test/ui/const-ptr/ptr_to_usize_cast.stderr b/src/test/ui/const-ptr/ptr_to_usize_cast.stderr new file mode 100644 index 00000000000..48255860bb5 --- /dev/null +++ b/src/test/ui/const-ptr/ptr_to_usize_cast.stderr @@ -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 + +error: aborting due to previous error +