about summary refs log tree commit diff
path: root/tests/ui/layout
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2024-06-08 11:26:10 +0200
committerRalf Jung <post@ralfj.de>2024-07-10 18:01:06 +0200
commit287b66b0b5cc06b9c0acb01d8a7b0db9ddf764ee (patch)
treeb117ac8233cfb690f6dd89ccd8df83a6e052bfc2 /tests/ui/layout
parentcd3d98b3bef709c1f42b1964ba7a3ea371f95773 (diff)
downloadrust-287b66b0b5cc06b9c0acb01d8a7b0db9ddf764ee.tar.gz
rust-287b66b0b5cc06b9c0acb01d8a7b0db9ddf764ee.zip
size_of_val_raw: for length 0 this is safe to call
Diffstat (limited to 'tests/ui/layout')
-rw-r--r--tests/ui/layout/size-of-val-raw-too-big.rs18
-rw-r--r--tests/ui/layout/size-of-val-raw-too-big.stderr4
2 files changed, 22 insertions, 0 deletions
diff --git a/tests/ui/layout/size-of-val-raw-too-big.rs b/tests/ui/layout/size-of-val-raw-too-big.rs
new file mode 100644
index 00000000000..8d82c78d953
--- /dev/null
+++ b/tests/ui/layout/size-of-val-raw-too-big.rs
@@ -0,0 +1,18 @@
+//@ build-fail
+//@ compile-flags: --crate-type lib
+//@ only-32bit Layout computation rejects this layout for different reasons on 64-bit.
+//@ error-pattern: too big for the current architecture
+#![feature(core_intrinsics)]
+#![allow(internal_features)]
+
+// isize::MAX is fine, but with the padding for the unsized tail it is too big.
+#[repr(C)]
+pub struct Example([u8; isize::MAX as usize], [u16]);
+
+// We guarantee that with length 0, `size_of_val_raw` (which calls the `size_of_val` intrinsic)
+// is safe to call. The compiler aborts compilation if a length of 0 would overflow.
+// So let's construct a case where length 0 just barely overflows, and ensure that
+// does abort compilation.
+pub fn check(x: *const Example) -> usize {
+    unsafe { std::intrinsics::size_of_val(x) }
+}
diff --git a/tests/ui/layout/size-of-val-raw-too-big.stderr b/tests/ui/layout/size-of-val-raw-too-big.stderr
new file mode 100644
index 00000000000..aa9abd644fa
--- /dev/null
+++ b/tests/ui/layout/size-of-val-raw-too-big.stderr
@@ -0,0 +1,4 @@
+error: values of the type `Example` are too big for the current architecture
+
+error: aborting due to 1 previous error
+