From 5d9b908571ed61679f2a12eaae7e032cdabbb8be Mon Sep 17 00:00:00 2001 From: Veera Date: Wed, 18 Sep 2024 21:29:31 -0400 Subject: [PATCH] Add Tests --- ...-to-int-transmute-in-consts-issue-87525.rs | 70 +++++++++++++++++++ ...int-transmute-in-consts-issue-87525.stderr | 12 ++++ 2 files changed, 82 insertions(+) create mode 100644 tests/ui/consts/const-eval/ptr-to-int-transmute-in-consts-issue-87525.rs create mode 100644 tests/ui/consts/const-eval/ptr-to-int-transmute-in-consts-issue-87525.stderr diff --git a/tests/ui/consts/const-eval/ptr-to-int-transmute-in-consts-issue-87525.rs b/tests/ui/consts/const-eval/ptr-to-int-transmute-in-consts-issue-87525.rs new file mode 100644 index 00000000000..f8e97904e9e --- /dev/null +++ b/tests/ui/consts/const-eval/ptr-to-int-transmute-in-consts-issue-87525.rs @@ -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); + +impl Type { + 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 +} diff --git a/tests/ui/consts/const-eval/ptr-to-int-transmute-in-consts-issue-87525.stderr b/tests/ui/consts/const-eval/ptr-to-int-transmute-in-consts-issue-87525.stderr new file mode 100644 index 00000000000..a57bc8e1e70 --- /dev/null +++ b/tests/ui/consts/const-eval/ptr-to-int-transmute-in-consts-issue-87525.stderr @@ -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`.