Skip to content

Commit 7885fac

Browse files
committed
improve error when CTFE does ptr-int-cast; update tests
1 parent 26c55ec commit 7885fac

File tree

8 files changed

+37
-27
lines changed

8 files changed

+37
-27
lines changed

src/librustc_mir/const_eval.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ use rustc_data_structures::fx::FxHashMap;
2020
use syntax::source_map::{Span, DUMMY_SP};
2121

2222
use crate::interpret::{self,
23-
PlaceTy, MPlaceTy, OpTy, ImmTy, Immediate, Scalar,
23+
PlaceTy, MPlaceTy, OpTy, ImmTy, Immediate, Scalar, Pointer,
2424
RawConst, ConstValue,
2525
InterpResult, InterpErrorInfo, GlobalId, InterpCx, StackPopCleanup,
26-
Allocation, AllocId, MemoryKind,
26+
Allocation, AllocId, MemoryKind, Memory,
2727
snapshot, RefTracking, intern_const_alloc_recursive,
2828
};
2929

@@ -397,6 +397,15 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
397397
)
398398
}
399399

400+
fn ptr_to_int(
401+
_mem: &Memory<'mir, 'tcx, Self>,
402+
_ptr: Pointer,
403+
) -> InterpResult<'tcx, u64> {
404+
Err(
405+
ConstEvalError::NeedsRfc("pointer-to-integer cast".to_string()).into(),
406+
)
407+
}
408+
400409
fn binary_ptr_op(
401410
_ecx: &InterpCx<'mir, 'tcx, Self>,
402411
_bin_op: mir::BinOp,

src/librustc_mir/interpret/machine.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ pub trait Machine<'mir, 'tcx>: Sized {
233233
extra: Self::FrameExtra,
234234
) -> InterpResult<'tcx>;
235235

236-
#[inline(always)]
237236
fn int_to_ptr(
238237
_mem: &Memory<'mir, 'tcx, Self>,
239238
int: u64,
@@ -245,11 +244,8 @@ pub trait Machine<'mir, 'tcx>: Sized {
245244
}).into())
246245
}
247246

248-
#[inline(always)]
249247
fn ptr_to_int(
250248
_mem: &Memory<'mir, 'tcx, Self>,
251249
_ptr: Pointer<Self::PointerTag>,
252-
) -> InterpResult<'tcx, u64> {
253-
throw_unsup!(ReadPointerAsBytes)
254-
}
250+
) -> InterpResult<'tcx, u64>;
255251
}

src/test/ui/consts/const-eval/const_raw_ptr_ops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ fn main() {}
44

55
// unconst and bad, will thus error in miri
66
const X: bool = unsafe { &1 as *const i32 == &2 as *const i32 }; //~ ERROR any use of this
7-
// unconst and fine
8-
const X2: bool = unsafe { 42 as *const i32 == 43 as *const i32 };
7+
// unconst and bad, will thus error in miri
8+
const X2: bool = unsafe { 42 as *const i32 == 43 as *const i32 }; //~ ERROR any use of this
99
// unconst and fine
1010
const Y: usize = unsafe { 42usize as *const i32 as usize + 1 };
1111
// unconst and bad, will thus error in miri

src/test/ui/consts/const-eval/const_raw_ptr_ops.stderr

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ LL | const X: bool = unsafe { &1 as *const i32 == &2 as *const i32 };
88
|
99
= note: `#[deny(const_err)]` on by default
1010

11+
error: any use of this value will cause an error
12+
--> $DIR/const_raw_ptr_ops.rs:8:27
13+
|
14+
LL | const X2: bool = unsafe { 42 as *const i32 == 43 as *const i32 };
15+
| --------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---
16+
| |
17+
| "pointer arithmetic or comparison" needs an rfc before being allowed inside constants
18+
1119
error: any use of this value will cause an error
1220
--> $DIR/const_raw_ptr_ops.rs:12:28
1321
|
1422
LL | const Y2: usize = unsafe { &1 as *const i32 as usize + 1 };
15-
| ---------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---
23+
| ---------------------------^^^^^^^^^^^^^^^^^^^^^^^^^-------
1624
| |
17-
| "pointer arithmetic or comparison" needs an rfc before being allowed inside constants
25+
| "pointer-to-integer cast" needs an rfc before being allowed inside constants
1826

1927
error: any use of this value will cause an error
2028
--> $DIR/const_raw_ptr_ops.rs:16:26
@@ -32,5 +40,5 @@ LL | const Z3: i32 = unsafe { *(44 as *const i32) };
3240
| |
3341
| a memory access tried to interpret some bytes as a pointer
3442

35-
error: aborting due to 4 previous errors
43+
error: aborting due to 5 previous errors
3644

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
22
[(); { &loop { break } as *const _ as usize } ];
33
//~^ ERROR casting pointers to integers in constants is unstable
4-
//~| ERROR it is undefined behavior to use this value
4+
//~| ERROR evaluation of constant value failed
55
}

src/test/ui/consts/const-eval/issue-52442.stderr

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ LL | [(); { &loop { break } as *const _ as usize } ];
77
= note: for more information, see https://github.com/rust-lang/rust/issues/51910
88
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable
99

10-
error[E0080]: it is undefined behavior to use this value
11-
--> $DIR/issue-52442.rs:2:11
10+
error[E0080]: evaluation of constant value failed
11+
--> $DIR/issue-52442.rs:2:13
1212
|
1313
LL | [(); { &loop { break } as *const _ as usize } ];
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected initialized plain (non-pointer) bytes
15-
|
16-
= 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
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants
1715

1816
error: aborting due to 2 previous errors
1917

src/test/ui/consts/const-eval/match-test-ptr-null.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
12
fn main() {
23
// Make sure match uses the usual pointer comparison code path -- i.e., it should complain
34
// that pointer comparison is disallowed, not that parts of a pointer are accessed as raw
45
// bytes.
56
let _: [u8; 0] = [4; {
67
match &1 as *const i32 as usize {
78
//~^ ERROR casting pointers to integers in constants
8-
//~| NOTE for more information, see
99
//~| ERROR constant contains unimplemented expression type
10-
0 => 42, //~ ERROR constant contains unimplemented expression type
11-
//~^ NOTE "pointer arithmetic or comparison" needs an rfc before being allowed
1210
//~| ERROR evaluation of constant value failed
11+
0 => 42, //~ ERROR constant contains unimplemented expression type
1312
n => n,
1413
}
1514
}];

src/test/ui/consts/const-eval/match-test-ptr-null.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: casting pointers to integers in constants is unstable
2-
--> $DIR/match-test-ptr-null.rs:6:15
2+
--> $DIR/match-test-ptr-null.rs:7:15
33
|
44
LL | match &1 as *const i32 as usize {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -8,22 +8,22 @@ LL | match &1 as *const i32 as usize {
88
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable
99

1010
error[E0019]: constant contains unimplemented expression type
11-
--> $DIR/match-test-ptr-null.rs:6:15
11+
--> $DIR/match-test-ptr-null.rs:7:15
1212
|
1313
LL | match &1 as *const i32 as usize {
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^
1515

1616
error[E0019]: constant contains unimplemented expression type
17-
--> $DIR/match-test-ptr-null.rs:10:13
17+
--> $DIR/match-test-ptr-null.rs:11:13
1818
|
1919
LL | 0 => 42,
2020
| ^
2121

2222
error[E0080]: evaluation of constant value failed
23-
--> $DIR/match-test-ptr-null.rs:10:13
23+
--> $DIR/match-test-ptr-null.rs:7:15
2424
|
25-
LL | 0 => 42,
26-
| ^ "pointer arithmetic or comparison" needs an rfc before being allowed inside constants
25+
LL | match &1 as *const i32 as usize {
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants
2727

2828
error: aborting due to 4 previous errors
2929

0 commit comments

Comments
 (0)