about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorVeera <sveera.2001@gmail.com>2024-09-18 21:29:31 -0400
committerVeera <sveera.2001@gmail.com>2024-09-18 21:55:23 -0400
commit5d9b908571ed61679f2a12eaae7e032cdabbb8be (patch)
tree40c9401dbc26ec08f2b13ec3cb80de11d4d93208 /tests
parent0ee7cb5e3633502d9a90a85c3c367eccd59a0aba (diff)
downloadrust-5d9b908571ed61679f2a12eaae7e032cdabbb8be.tar.gz
rust-5d9b908571ed61679f2a12eaae7e032cdabbb8be.zip
Add Tests
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/consts/const-eval/ptr-to-int-transmute-in-consts-issue-87525.rs70
-rw-r--r--tests/ui/consts/const-eval/ptr-to-int-transmute-in-consts-issue-87525.stderr12
2 files changed, 82 insertions, 0 deletions
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>(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
+}
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`.