about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOli Scherer <github333195615777966@oli-obk.de>2025-01-28 11:59:29 +0000
committerOli Scherer <github333195615777966@oli-obk.de>2025-01-29 10:25:55 +0000
commit22f074129a27e229b30c69b8e8d600dee3593537 (patch)
treea4afec62f4dcc330cc5e74d688892245087b34a0
parentccc9ba5c30c675824e9ca62b960830ff4a1858ea (diff)
downloadrust-22f074129a27e229b30c69b8e8d600dee3593537.tar.gz
rust-22f074129a27e229b30c69b8e8d600dee3593537.zip
Add tests for transmuting pattern types
-rw-r--r--tests/ui/type/pattern_types/transmute.rs34
-rw-r--r--tests/ui/type/pattern_types/transmute.stderr39
2 files changed, 73 insertions, 0 deletions
diff --git a/tests/ui/type/pattern_types/transmute.rs b/tests/ui/type/pattern_types/transmute.rs
new file mode 100644
index 00000000000..54aab6e3cb2
--- /dev/null
+++ b/tests/ui/type/pattern_types/transmute.rs
@@ -0,0 +1,34 @@
+#![feature(pattern_types)]
+#![feature(pattern_type_macro)]
+
+use std::pat::pattern_type;
+
+// ok
+fn create<const S: u32, const E: u32>(x: u32) -> pattern_type!(u32 is S..=E) {
+    unsafe { std::mem::transmute(x) }
+    //~^ ERROR types of different sizes
+}
+
+// ok
+fn unwrap<const S: u32, const E: u32>(x: pattern_type!(u32 is S..=E)) -> u32 {
+    unsafe { std::mem::transmute(x) }
+    //~^ ERROR types of different sizes
+}
+
+// bad, only when S != u32::MIN or E != u32::MAX will this ok
+fn non_base_ty_transmute<const S: u32, const E: u32>(
+    x: Option<pattern_type!(u32 is S..=E)>,
+) -> u32 {
+    unsafe { std::mem::transmute(x) }
+    //~^ ERROR types of different sizes
+}
+
+// bad, only when S = u32::MIN and E = u32::MAX will this ok
+fn wrapped_transmute<const S: u32, const E: u32>(
+    x: Option<pattern_type!(u32 is S..=E)>,
+) -> Option<u32> {
+    unsafe { std::mem::transmute(x) }
+    //~^ ERROR types of different sizes
+}
+
+fn main() {}
diff --git a/tests/ui/type/pattern_types/transmute.stderr b/tests/ui/type/pattern_types/transmute.stderr
new file mode 100644
index 00000000000..f348740b105
--- /dev/null
+++ b/tests/ui/type/pattern_types/transmute.stderr
@@ -0,0 +1,39 @@
+error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
+  --> $DIR/transmute.rs:8:14
+   |
+LL |     unsafe { std::mem::transmute(x) }
+   |              ^^^^^^^^^^^^^^^^^^^
+   |
+   = note: source type: `u32` (32 bits)
+   = note: target type: `(u32) is S..=E` (size can vary because of u32)
+
+error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
+  --> $DIR/transmute.rs:14:14
+   |
+LL |     unsafe { std::mem::transmute(x) }
+   |              ^^^^^^^^^^^^^^^^^^^
+   |
+   = note: source type: `(u32) is S..=E` (size can vary because of u32)
+   = note: target type: `u32` (32 bits)
+
+error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
+  --> $DIR/transmute.rs:22:14
+   |
+LL |     unsafe { std::mem::transmute(x) }
+   |              ^^^^^^^^^^^^^^^^^^^
+   |
+   = note: source type: `Option<(u32) is S..=E>` (size can vary because of u32)
+   = note: target type: `u32` (32 bits)
+
+error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
+  --> $DIR/transmute.rs:30:14
+   |
+LL |     unsafe { std::mem::transmute(x) }
+   |              ^^^^^^^^^^^^^^^^^^^
+   |
+   = note: source type: `Option<(u32) is S..=E>` (size can vary because of u32)
+   = note: target type: `Option<u32>` (64 bits)
+
+error: aborting due to 4 previous errors
+
+For more information about this error, try `rustc --explain E0512`.