Skip to content

Commit

Permalink
improve error when CTFE does ptr-int-cast; update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Jul 24, 2019
1 parent 8713a7b commit 8890624
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 27 deletions.
13 changes: 11 additions & 2 deletions src/librustc_mir/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, InterpError, GlobalId, InterpCx, StackPopCleanup,
Allocation, AllocId, MemoryKind,
Allocation, AllocId, MemoryKind, Memory,
snapshot, RefTracking, intern_const_alloc_recursive,
};

Expand Down Expand Up @@ -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,
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_mir/interpret/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,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,
Expand All @@ -235,11 +234,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> {
err!(ReadPointerAsBytes)
}
) -> InterpResult<'tcx, u64>;
}
4 changes: 2 additions & 2 deletions src/test/ui/consts/const-eval/const_raw_ptr_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions src/test/ui/consts/const-eval/const_raw_ptr_ops.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

2 changes: 1 addition & 1 deletion src/test/ui/consts/const-eval/issue-52442.rs
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 3 additions & 5 deletions src/test/ui/consts/const-eval/issue-52442.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 2 additions & 3 deletions src/test/ui/consts/const-eval/match-test-ptr-null.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@

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
// bytes.
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,
}
}];
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/consts/const-eval/match-test-ptr-null.stderr
Original file line number Diff line number Diff line change
@@ -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 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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

Expand Down

0 comments on commit 8890624

Please sign in to comment.