diff options
| author | Ralf Jung <post@ralfj.de> | 2019-07-25 00:06:47 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2019-08-02 23:01:53 +0200 |
| commit | 7885fac7e904568d58eae4ef34239a3c41241aa8 (patch) | |
| tree | 32345200550e2dd3744d8b28ab6f920e6bf61f6f | |
| parent | 26c55ec0505e1337780b1ee2bad58dc672714bb5 (diff) | |
| download | rust-7885fac7e904568d58eae4ef34239a3c41241aa8.tar.gz rust-7885fac7e904568d58eae4ef34239a3c41241aa8.zip | |
improve error when CTFE does ptr-int-cast; update tests
| -rw-r--r-- | src/librustc_mir/const_eval.rs | 13 | ||||
| -rw-r--r-- | src/librustc_mir/interpret/machine.rs | 6 | ||||
| -rw-r--r-- | src/test/ui/consts/const-eval/const_raw_ptr_ops.rs | 4 | ||||
| -rw-r--r-- | src/test/ui/consts/const-eval/const_raw_ptr_ops.stderr | 14 | ||||
| -rw-r--r-- | src/test/ui/consts/const-eval/issue-52442.rs | 2 | ||||
| -rw-r--r-- | src/test/ui/consts/const-eval/issue-52442.stderr | 8 | ||||
| -rw-r--r-- | src/test/ui/consts/const-eval/match-test-ptr-null.rs | 5 | ||||
| -rw-r--r-- | src/test/ui/consts/const-eval/match-test-ptr-null.stderr | 12 |
8 files changed, 37 insertions, 27 deletions
diff --git a/src/librustc_mir/const_eval.rs b/src/librustc_mir/const_eval.rs index 813b7f7fa9c..54cf56258f9 100644 --- a/src/librustc_mir/const_eval.rs +++ b/src/librustc_mir/const_eval.rs @@ -20,10 +20,10 @@ use rustc_data_structures::fx::FxHashMap; use syntax::source_map::{Span, DUMMY_SP}; use crate::interpret::{self, - PlaceTy, MPlaceTy, OpTy, ImmTy, Immediate, Scalar, + PlaceTy, MPlaceTy, OpTy, ImmTy, Immediate, Scalar, Pointer, RawConst, ConstValue, InterpResult, InterpErrorInfo, GlobalId, InterpCx, StackPopCleanup, - Allocation, AllocId, MemoryKind, + Allocation, AllocId, MemoryKind, Memory, snapshot, RefTracking, intern_const_alloc_recursive, }; @@ -397,6 +397,15 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir, ) } + fn ptr_to_int( + _mem: &Memory<'mir, 'tcx, Self>, + _ptr: Pointer, + ) -> InterpResult<'tcx, u64> { + Err( + ConstEvalError::NeedsRfc("pointer-to-integer cast".to_string()).into(), + ) + } + fn binary_ptr_op( _ecx: &InterpCx<'mir, 'tcx, Self>, _bin_op: mir::BinOp, diff --git a/src/librustc_mir/interpret/machine.rs b/src/librustc_mir/interpret/machine.rs index 4a0cc24cf1e..a9794982fe5 100644 --- a/src/librustc_mir/interpret/machine.rs +++ b/src/librustc_mir/interpret/machine.rs @@ -233,7 +233,6 @@ pub trait Machine<'mir, 'tcx>: Sized { extra: Self::FrameExtra, ) -> InterpResult<'tcx>; - #[inline(always)] fn int_to_ptr( _mem: &Memory<'mir, 'tcx, Self>, int: u64, @@ -245,11 +244,8 @@ pub trait Machine<'mir, 'tcx>: Sized { }).into()) } - #[inline(always)] fn ptr_to_int( _mem: &Memory<'mir, 'tcx, Self>, _ptr: Pointer<Self::PointerTag>, - ) -> InterpResult<'tcx, u64> { - throw_unsup!(ReadPointerAsBytes) - } + ) -> InterpResult<'tcx, u64>; } diff --git a/src/test/ui/consts/const-eval/const_raw_ptr_ops.rs b/src/test/ui/consts/const-eval/const_raw_ptr_ops.rs index 44266682a5c..9be1374f85d 100644 --- a/src/test/ui/consts/const-eval/const_raw_ptr_ops.rs +++ b/src/test/ui/consts/const-eval/const_raw_ptr_ops.rs @@ -4,8 +4,8 @@ fn main() {} // unconst and bad, will thus error in miri const X: bool = unsafe { &1 as *const i32 == &2 as *const i32 }; //~ ERROR any use of this -// unconst and fine -const X2: bool = unsafe { 42 as *const i32 == 43 as *const i32 }; +// unconst and bad, will thus error in miri +const X2: bool = unsafe { 42 as *const i32 == 43 as *const i32 }; //~ ERROR any use of this // unconst and fine const Y: usize = unsafe { 42usize as *const i32 as usize + 1 }; // unconst and bad, will thus error in miri diff --git a/src/test/ui/consts/const-eval/const_raw_ptr_ops.stderr b/src/test/ui/consts/const-eval/const_raw_ptr_ops.stderr index a12575b3975..2cba833a748 100644 --- a/src/test/ui/consts/const-eval/const_raw_ptr_ops.stderr +++ b/src/test/ui/consts/const-eval/const_raw_ptr_ops.stderr @@ -9,12 +9,20 @@ LL | const X: bool = unsafe { &1 as *const i32 == &2 as *const i32 }; = note: `#[deny(const_err)]` on by default error: any use of this value will cause an error + --> $DIR/const_raw_ptr_ops.rs:8:27 + | +LL | const X2: bool = unsafe { 42 as *const i32 == 43 as *const i32 }; + | --------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--- + | | + | "pointer arithmetic or comparison" needs an rfc before being allowed inside constants + +error: any use of this value will cause an error --> $DIR/const_raw_ptr_ops.rs:12:28 | LL | const Y2: usize = unsafe { &1 as *const i32 as usize + 1 }; - | ---------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--- + | ---------------------------^^^^^^^^^^^^^^^^^^^^^^^^^------- | | - | "pointer arithmetic or comparison" needs an rfc before being allowed inside constants + | "pointer-to-integer cast" needs an rfc before being allowed inside constants error: any use of this value will cause an error --> $DIR/const_raw_ptr_ops.rs:16:26 @@ -32,5 +40,5 @@ LL | const Z3: i32 = unsafe { *(44 as *const i32) }; | | | a memory access tried to interpret some bytes as a pointer -error: aborting due to 4 previous errors +error: aborting due to 5 previous errors diff --git a/src/test/ui/consts/const-eval/issue-52442.rs b/src/test/ui/consts/const-eval/issue-52442.rs index 2989b200b2f..ea24578c7dd 100644 --- a/src/test/ui/consts/const-eval/issue-52442.rs +++ b/src/test/ui/consts/const-eval/issue-52442.rs @@ -1,5 +1,5 @@ fn main() { [(); { &loop { break } as *const _ as usize } ]; //~^ ERROR casting pointers to integers in constants is unstable - //~| ERROR it is undefined behavior to use this value + //~| ERROR evaluation of constant value failed } diff --git a/src/test/ui/consts/const-eval/issue-52442.stderr b/src/test/ui/consts/const-eval/issue-52442.stderr index 88c94d917fe..5bd4979bdb3 100644 --- a/src/test/ui/consts/const-eval/issue-52442.stderr +++ b/src/test/ui/consts/const-eval/issue-52442.stderr @@ -7,13 +7,11 @@ LL | [(); { &loop { break } as *const _ as usize } ]; = note: for more information, see https://github.com/rust-lang/rust/issues/51910 = help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable -error[E0080]: it is undefined behavior to use this value - --> $DIR/issue-52442.rs:2:11 +error[E0080]: evaluation of constant value failed + --> $DIR/issue-52442.rs:2:13 | LL | [(); { &loop { break } as *const _ as usize } ]; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected initialized plain (non-pointer) bytes - | - = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants error: aborting due to 2 previous errors diff --git a/src/test/ui/consts/const-eval/match-test-ptr-null.rs b/src/test/ui/consts/const-eval/match-test-ptr-null.rs index 50757afaf56..9c930221e73 100644 --- a/src/test/ui/consts/const-eval/match-test-ptr-null.rs +++ b/src/test/ui/consts/const-eval/match-test-ptr-null.rs @@ -1,3 +1,4 @@ + fn main() { // Make sure match uses the usual pointer comparison code path -- i.e., it should complain // that pointer comparison is disallowed, not that parts of a pointer are accessed as raw @@ -5,11 +6,9 @@ fn main() { let _: [u8; 0] = [4; { match &1 as *const i32 as usize { //~^ ERROR casting pointers to integers in constants - //~| NOTE for more information, see //~| ERROR constant contains unimplemented expression type - 0 => 42, //~ ERROR constant contains unimplemented expression type - //~^ NOTE "pointer arithmetic or comparison" needs an rfc before being allowed //~| ERROR evaluation of constant value failed + 0 => 42, //~ ERROR constant contains unimplemented expression type n => n, } }]; diff --git a/src/test/ui/consts/const-eval/match-test-ptr-null.stderr b/src/test/ui/consts/const-eval/match-test-ptr-null.stderr index d8a3bac5ce6..9bb561f31eb 100644 --- a/src/test/ui/consts/const-eval/match-test-ptr-null.stderr +++ b/src/test/ui/consts/const-eval/match-test-ptr-null.stderr @@ -1,5 +1,5 @@ error[E0658]: casting pointers to integers in constants is unstable - --> $DIR/match-test-ptr-null.rs:6:15 + --> $DIR/match-test-ptr-null.rs:7:15 | LL | match &1 as *const i32 as usize { | ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -8,22 +8,22 @@ LL | match &1 as *const i32 as usize { = help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable error[E0019]: constant contains unimplemented expression type - --> $DIR/match-test-ptr-null.rs:6:15 + --> $DIR/match-test-ptr-null.rs:7:15 | LL | match &1 as *const i32 as usize { | ^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0019]: constant contains unimplemented expression type - --> $DIR/match-test-ptr-null.rs:10:13 + --> $DIR/match-test-ptr-null.rs:11:13 | LL | 0 => 42, | ^ error[E0080]: evaluation of constant value failed - --> $DIR/match-test-ptr-null.rs:10:13 + --> $DIR/match-test-ptr-null.rs:7:15 | -LL | 0 => 42, - | ^ "pointer arithmetic or comparison" needs an rfc before being allowed inside constants +LL | match &1 as *const i32 as usize { + | ^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants error: aborting due to 4 previous errors |
