about summary refs log tree commit diff
path: root/tests/ui/layout
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-07-24 18:00:35 +0200
committerGitHub <noreply@github.com>2024-07-24 18:00:35 +0200
commit130d15e23e2f06e0d777ecdc078cc2b49fefb543 (patch)
tree3d25b0df06f4546d8fd22e796158bfaa100ec781 /tests/ui/layout
parent2ccafed862f6906707a390caf180449dd64cad2e (diff)
parent287b66b0b5cc06b9c0acb01d8a7b0db9ddf764ee (diff)
downloadrust-130d15e23e2f06e0d777ecdc078cc2b49fefb543.tar.gz
rust-130d15e23e2f06e0d777ecdc078cc2b49fefb543.zip
Rollup merge of #126152 - RalfJung:size_of_val_raw, r=saethlin
size_of_val_raw: for length 0 this is safe to call

For motivation, see https://github.com/rust-lang/unsafe-code-guidelines/issues/465, specifically around [here](https://github.com/rust-lang/unsafe-code-guidelines/issues/465#issuecomment-2136401114).
Cc `@rust-lang/opsem`
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
+