Skip to content

Commit b263981

Browse files
authored
Rollup merge of #81779 - geogriff:const-ptr-to-int-error, r=lcnr
improve error message for disallowed ptr-to-int casts in const eval Improves an error message as [suggested](#80875 (comment)) in #80875. Does the wording make enough sense? I tried to follow precedent for error message style while maintaining brevity. It seems like the rest of the `ConstEvalErrKind::NeedsRfc` error messages could be improved as well. I could give that a go if this approach works. Closes #80875
2 parents 2c8d1c8 + af28c2e commit b263981

File tree

11 files changed

+44
-10
lines changed

11 files changed

+44
-10
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+

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: any use of this value will cause an error
44
LL | const Y2: usize = unsafe { &1 as *const i32 as usize + 1 };
55
| ---------------------------^^^^^^^^^^^^^^^^^^^^^^^^^-------
66
| |
7-
| "pointer-to-integer cast" needs an rfc before being allowed inside constants
7+
| cannot cast pointer to integer because it was not created by cast from integer
88
|
99
= note: `#[deny(const_err)]` on by default
1010
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

src/test/ui/consts/issue-51559.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: any use of this value will cause an error
44
LL | pub const FOO: usize = unsafe { BAR as usize };
55
| --------------------------------^^^^^^^^^^^^---
66
| |
7-
| "pointer-to-integer cast" needs an rfc before being allowed inside constants
7+
| cannot cast pointer to integer because it was not created by cast from integer
88
|
99
= note: `#[deny(const_err)]` on by default
1010
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

src/test/ui/consts/issue-52432.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ error[E0080]: evaluation of constant value failed
2020
--> $DIR/issue-52432.rs:7:10
2121
|
2222
LL | [(); &(static || {}) as *const _ as usize];
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot cast pointer to integer because it was not created by cast from integer
2424

2525
error: aborting due to 4 previous errors
2626

src/test/ui/consts/miri_unleashed/ptr_arith.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static INT_PTR_ARITH: () = unsafe {
1515
let x: usize = std::mem::transmute(&0);
1616
let _v = x + 0;
1717
//~^ ERROR could not evaluate static initializer
18-
//~| NOTE pointer-to-integer cast
18+
//~| NOTE cannot cast pointer to integer
1919
};
2020

2121
fn main() {}

src/test/ui/consts/miri_unleashed/ptr_arith.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ error[E0080]: could not evaluate static initializer
88
--> $DIR/ptr_arith.rs:16:14
99
|
1010
LL | let _v = x + 0;
11-
| ^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants
11+
| ^^^^^ cannot cast pointer to integer because it was not created by cast from integer
1212

1313
warning: skipping const checks
1414
|

src/test/ui/consts/ptr_comparisons.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ const _: *const u8 =
7171

7272
const _: usize = unsafe { std::mem::transmute::<*const usize, usize>(FOO) + 4 };
7373
//~^ ERROR any use of this value will cause an error
74-
//~| NOTE "pointer-to-integer cast" needs an rfc
74+
//~| NOTE cannot cast pointer to integer
7575
//~| NOTE
7676
//~| WARN this was previously accepted by the compiler but is being phased out
7777
//~| NOTE
7878

7979
const _: usize = unsafe { *std::mem::transmute::<&&usize, &usize>(&FOO) + 4 };
8080
//~^ ERROR any use of this value will cause an error
81-
//~| NOTE "pointer-to-integer cast" needs an rfc
81+
//~| NOTE cannot cast pointer to integer
8282
//~| NOTE
8383
//~| WARN this was previously accepted by the compiler but is being phased out
8484
//~| NOTE

src/test/ui/consts/ptr_comparisons.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ error: any use of this value will cause an error
3636
LL | const _: usize = unsafe { std::mem::transmute::<*const usize, usize>(FOO) + 4 };
3737
| --------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---
3838
| |
39-
| "pointer-to-integer cast" needs an rfc before being allowed inside constants
39+
| cannot cast pointer to integer because it was not created by cast from integer
4040
|
4141
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4242
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
@@ -47,7 +47,7 @@ error: any use of this value will cause an error
4747
LL | const _: usize = unsafe { *std::mem::transmute::<&&usize, &usize>(&FOO) + 4 };
4848
| --------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---
4949
| |
50-
| "pointer-to-integer cast" needs an rfc before being allowed inside constants
50+
| cannot cast pointer to integer because it was not created by cast from integer
5151
|
5252
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
5353
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

0 commit comments

Comments
 (0)