Skip to content

Commit ff315e3

Browse files
author
Jeffrey Griffin
committed
improve error message for disallowed ptr-to-int casts in const eval
1 parent 6a388dc commit ff315e3

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

compiler/rustc_mir/src/const_eval/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::interpret::{
1616
#[derive(Clone, Debug)]
1717
pub enum ConstEvalErrKind {
1818
NeedsRfc(String),
19+
PtrToIntCast,
1920
ConstAccessesStatic,
2021
ModifiedGlobal,
2122
AssertFailure(AssertKind<ConstInt>),
@@ -39,6 +40,12 @@ impl fmt::Display for ConstEvalErrKind {
3940
NeedsRfc(ref msg) => {
4041
write!(f, "\"{}\" needs an rfc before being allowed inside constants", msg)
4142
}
43+
PtrToIntCast => {
44+
write!(
45+
f,
46+
"cannot cast pointer to integer because it was not created by cast from integer"
47+
)
48+
}
4249
ConstAccessesStatic => write!(f, "constant accesses static"),
4350
ModifiedGlobal => {
4451
write!(f, "modifying a static's initial value from another static's initializer")

compiler/rustc_mir/src/const_eval/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
352352
}
353353

354354
fn ptr_to_int(_mem: &Memory<'mir, 'tcx, Self>, _ptr: Pointer) -> InterpResult<'tcx, u64> {
355-
Err(ConstEvalErrKind::NeedsRfc("pointer-to-integer cast".to_string()).into())
355+
Err(ConstEvalErrKind::PtrToIntCast.into())
356356
}
357357

358358
fn binary_ptr_op(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(const_raw_ptr_to_usize_cast)]
2+
3+
fn main() {
4+
const OK: usize = unsafe { 0 as *const i32 as usize };
5+
6+
const _ERROR: usize = unsafe { &0 as *const i32 as usize };
7+
//~^ ERROR [const_err]
8+
//~| NOTE cannot cast pointer to integer because it was not created by cast from integer
9+
//~| NOTE
10+
//~| NOTE `#[deny(const_err)]` on by default
11+
//~| WARN this was previously accepted by the compiler but is being phased out
12+
//~| NOTE see issue #71800
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: any use of this value will cause an error
2+
--> $DIR/ptr_to_usize_cast.rs:6:36
3+
|
4+
LL | const _ERROR: usize = unsafe { &0 as *const i32 as usize };
5+
| -------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^---
6+
| |
7+
| cannot cast pointer to integer because it was not created by cast from integer
8+
|
9+
= note: `#[deny(const_err)]` on by default
10+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
11+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)