improve error when CTFE does ptr-int-cast; update tests

This commit is contained in:
Ralf Jung 2019-07-25 00:06:47 +02:00
parent 26c55ec050
commit 7885fac7e9
8 changed files with 37 additions and 27 deletions

View file

@ -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,

View file

@ -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>;
}

View file

@ -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

View file

@ -8,13 +8,21 @@ 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

View file

@ -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
}

View file

@ -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

View file

@ -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,
}
}];

View file

@ -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