Skip to content

Commit

Permalink
Add Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
veera-sivarajan committed Sep 19, 2024
1 parent 0ee7cb5 commit 5d9b908
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const fn foo(ptr: *const u8) -> usize {
unsafe {
std::mem::transmute(ptr)
//~^ ERROR pointers cannot be transmuted to integers
}
}

trait Human {
const ID: u64 = {
let value = 10;
let ptr: *const i32 = &value;
unsafe {
std::mem::transmute(ptr)
//~^ ERROR pointers cannot be transmuted to integers
}
};

fn id_plus_one() -> u64 {
Self::ID + 1
}
}

struct Type<T>(T);

impl<T> Type<T> {
const ID: u64 = {
let value = 10;
let ptr: *const i32 = &value;
unsafe {
std::mem::transmute(ptr)
//~^ ERROR pointers cannot be transmuted to integers
}
};

fn id_plus_one() -> u64 {
Self::ID + 1
}
}

fn control(ptr: *const u8) -> usize {
unsafe {
std::mem::transmute(ptr)
}
}

struct ControlStruct;

impl ControlStruct {
fn new() -> usize {
let value = 10;
let ptr: *const i32 = &value;
unsafe {
std::mem::transmute(ptr)
}
}
}


const fn zoom(ptr: *const u8) -> usize {
unsafe {
std::mem::transmute(ptr)
//~^ ERROR pointers cannot be transmuted to integers
}
}

fn main() {
const a: u8 = 10;
const value: usize = zoom(&a);
//~^ ERROR evaluation of constant value failed
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0080]: evaluation of constant value failed
--> $DIR/ptr-to-int-transmute-in-consts-issue-87525.rs:68:26
|
LL | const value: usize = zoom(&a);
| ^^^^^^^^ unable to turn pointer into integer
|
= help: this code performed an operation that depends on the underlying bytes representing a pointer
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0080`.

0 comments on commit 5d9b908

Please sign in to comment.