Skip to content

Commit 5d9b908

Browse files
Add Tests
1 parent 0ee7cb5 commit 5d9b908

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const fn foo(ptr: *const u8) -> usize {
2+
unsafe {
3+
std::mem::transmute(ptr)
4+
//~^ ERROR pointers cannot be transmuted to integers
5+
}
6+
}
7+
8+
trait Human {
9+
const ID: u64 = {
10+
let value = 10;
11+
let ptr: *const i32 = &value;
12+
unsafe {
13+
std::mem::transmute(ptr)
14+
//~^ ERROR pointers cannot be transmuted to integers
15+
}
16+
};
17+
18+
fn id_plus_one() -> u64 {
19+
Self::ID + 1
20+
}
21+
}
22+
23+
struct Type<T>(T);
24+
25+
impl<T> Type<T> {
26+
const ID: u64 = {
27+
let value = 10;
28+
let ptr: *const i32 = &value;
29+
unsafe {
30+
std::mem::transmute(ptr)
31+
//~^ ERROR pointers cannot be transmuted to integers
32+
}
33+
};
34+
35+
fn id_plus_one() -> u64 {
36+
Self::ID + 1
37+
}
38+
}
39+
40+
fn control(ptr: *const u8) -> usize {
41+
unsafe {
42+
std::mem::transmute(ptr)
43+
}
44+
}
45+
46+
struct ControlStruct;
47+
48+
impl ControlStruct {
49+
fn new() -> usize {
50+
let value = 10;
51+
let ptr: *const i32 = &value;
52+
unsafe {
53+
std::mem::transmute(ptr)
54+
}
55+
}
56+
}
57+
58+
59+
const fn zoom(ptr: *const u8) -> usize {
60+
unsafe {
61+
std::mem::transmute(ptr)
62+
//~^ ERROR pointers cannot be transmuted to integers
63+
}
64+
}
65+
66+
fn main() {
67+
const a: u8 = 10;
68+
const value: usize = zoom(&a);
69+
//~^ ERROR evaluation of constant value failed
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $DIR/ptr-to-int-transmute-in-consts-issue-87525.rs:68:26
3+
|
4+
LL | const value: usize = zoom(&a);
5+
| ^^^^^^^^ unable to turn pointer into integer
6+
|
7+
= help: this code performed an operation that depends on the underlying bytes representing a pointer
8+
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)