Skip to content

Commit b1ad1fb

Browse files
committed
make sure raw ptr casts in 'const' context are unsafe
1 parent 43126f3 commit b1ad1fb

4 files changed

+76
-25
lines changed
+18-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
// gate-test-const_raw_ptr_to_usize_cast
2+
// revisions: with_feature without_feature
3+
4+
#![cfg_attr(with_feature, feature(const_raw_ptr_to_usize_cast))]
25

36
fn main() {
4-
const X: u32 = unsafe {
5-
main as u32 //~ ERROR casting pointers to integers in constants is unstable
7+
const X: usize = unsafe {
8+
main as usize //[without_feature]~ ERROR casting pointers to integers in constants is unstable
69
};
710
const Y: u32 = 0;
8-
const Z: u32 = unsafe {
9-
&Y as *const u32 as u32 //~ ERROR is unstable
11+
const Z: usize = unsafe {
12+
&Y as *const u32 as usize //[without_feature]~ ERROR is unstable
13+
};
14+
// Cast in `const` without `unsafe` block
15+
const SAFE: usize = {
16+
&Y as *const u32 as usize //[without_feature]~ ERROR is unstable
17+
//[with_feature]~^ ERROR cast of pointer to int is unsafe and requires unsafe
1018
};
1119
}
20+
21+
// Cast in `const fn` without `unsafe` block
22+
const fn test() -> usize {
23+
&0 as *const i32 as usize //[without_feature]~ ERROR is unstable
24+
//[with_feature]~^ ERROR cast of pointer to int is unsafe and requires unsafe
25+
}

src/test/ui/cast/cast-ptr-to-int-const.stderr

-21
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0133]: cast of pointer to int is unsafe and requires unsafe function or block
2+
--> $DIR/cast-ptr-to-int-const.rs:16:9
3+
|
4+
LL | &Y as *const u32 as usize
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cast of pointer to int
6+
|
7+
= note: casting pointers to integers in constants
8+
9+
error[E0133]: cast of pointer to int is unsafe and requires unsafe function or block
10+
--> $DIR/cast-ptr-to-int-const.rs:23:5
11+
|
12+
LL | &0 as *const i32 as usize
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cast of pointer to int
14+
|
15+
= note: casting pointers to integers in constants
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0133`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0658]: casting pointers to integers in constants is unstable
2+
--> $DIR/cast-ptr-to-int-const.rs:8:9
3+
|
4+
LL | main as usize
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information
8+
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable
9+
10+
error[E0658]: casting pointers to integers in constants is unstable
11+
--> $DIR/cast-ptr-to-int-const.rs:12:9
12+
|
13+
LL | &Y as *const u32 as usize
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information
17+
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable
18+
19+
error[E0658]: casting pointers to integers in constants is unstable
20+
--> $DIR/cast-ptr-to-int-const.rs:16:9
21+
|
22+
LL | &Y as *const u32 as usize
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
24+
|
25+
= note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information
26+
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable
27+
28+
error[E0658]: casting pointers to integers in constant functions is unstable
29+
--> $DIR/cast-ptr-to-int-const.rs:23:5
30+
|
31+
LL | &0 as *const i32 as usize
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
33+
|
34+
= note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information
35+
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable
36+
37+
error: aborting due to 4 previous errors
38+
39+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)