about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-10-04 12:31:26 +0200
committerGitHub <noreply@github.com>2025-10-04 12:31:26 +0200
commit65577da49cb7b7db1cc9b3aa8a4790d8d46349d2 (patch)
treec40d6a461b67f55088ac7ffcafa68569baf8dd83
parenteb8e240de97e27807e1079239024c888968e2453 (diff)
parenta9ab29cdb45005d69738c3a7cbec4c5cd0fceb73 (diff)
downloadrust-65577da49cb7b7db1cc9b3aa8a4790d8d46349d2.tar.gz
rust-65577da49cb7b7db1cc9b3aa8a4790d8d46349d2.zip
Rollup merge of #146479 - Qelxiros:mem_conjure_zst, r=scottmcm,tgross35
add mem::conjure_zst

Tracking issue: rust-lang/rust#95383
-rw-r--r--library/core/src/mem/mod.rs58
-rw-r--r--tests/ui/consts/std/conjure_zst.rs10
-rw-r--r--tests/ui/consts/std/conjure_zst.stderr12
3 files changed, 80 insertions, 0 deletions
diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs
index db4c8e9e551..c484551187c 100644
--- a/library/core/src/mem/mod.rs
+++ b/library/core/src/mem/mod.rs
@@ -7,6 +7,7 @@
 
 use crate::alloc::Layout;
 use crate::marker::DiscriminantKind;
+use crate::panic::const_assert;
 use crate::{clone, cmp, fmt, hash, intrinsics, ptr};
 
 mod manually_drop;
@@ -1407,3 +1408,60 @@ pub macro offset_of($Container:ty, $($fields:expr)+ $(,)?) {
     // The `{}` is for better error messages
     {builtin # offset_of($Container, $($fields)+)}
 }
+
+/// Create a fresh instance of the inhabited ZST type `T`.
+///
+/// Prefer this to [`zeroed`] or [`uninitialized`] or [`transmute_copy`]
+/// in places where you know that `T` is zero-sized, but don't have a bound
+/// (such as [`Default`]) that would allow you to instantiate it using safe code.
+///
+/// If you're not sure whether `T` is an inhabited ZST, then you should be
+/// using [`MaybeUninit`], not this function.
+///
+/// # Panics
+///
+/// If `size_of::<T>() != 0`.
+///
+/// # Safety
+///
+/// - `T` must be *[inhabited]*, i.e. possible to construct. This means that types
+///   like zero-variant enums and [`!`] are unsound to conjure.
+/// - You must use the value only in ways which do not violate any *safety*
+///   invariants of the type.
+///
+/// While it's easy to create a *valid* instance of an inhabited ZST, since having
+/// no bits in its representation means there's only one possible value, that
+/// doesn't mean that it's always *sound* to do so.
+///
+/// For example, a library could design zero-sized tokens that are `!Default + !Clone`, limiting
+/// their creation to functions that initialize some state or establish a scope. Conjuring such a
+/// token could break invariants and lead to unsoundness.
+///
+/// # Examples
+///
+/// ```
+/// #![feature(mem_conjure_zst)]
+/// use std::mem::conjure_zst;
+///
+/// assert_eq!(unsafe { conjure_zst::<()>() }, ());
+/// assert_eq!(unsafe { conjure_zst::<[i32; 0]>() }, []);
+/// ```
+///
+/// [inhabited]: https://doc.rust-lang.org/reference/glossary.html#inhabited
+#[unstable(feature = "mem_conjure_zst", issue = "95383")]
+pub const unsafe fn conjure_zst<T>() -> T {
+    const_assert!(
+        size_of::<T>() == 0,
+        "mem::conjure_zst invoked on a nonzero-sized type",
+        "mem::conjure_zst invoked on type {t}, which is not zero-sized",
+        t: &str = stringify!(T)
+    );
+
+    // SAFETY: because the caller must guarantee that it's inhabited and zero-sized,
+    // there's nothing in the representation that needs to be set.
+    // `assume_init` calls `assert_inhabited`, so we don't need to here.
+    unsafe {
+        #[allow(clippy::uninit_assumed_init)]
+        MaybeUninit::uninit().assume_init()
+    }
+}
diff --git a/tests/ui/consts/std/conjure_zst.rs b/tests/ui/consts/std/conjure_zst.rs
new file mode 100644
index 00000000000..c04deae502b
--- /dev/null
+++ b/tests/ui/consts/std/conjure_zst.rs
@@ -0,0 +1,10 @@
+#![feature(mem_conjure_zst)]
+
+use std::{convert::Infallible, mem};
+
+const INVALID: Infallible = unsafe { mem::conjure_zst() };
+//~^ ERROR attempted to instantiate uninhabited type
+
+const VALID: () = unsafe { mem::conjure_zst() };
+
+fn main() {}
diff --git a/tests/ui/consts/std/conjure_zst.stderr b/tests/ui/consts/std/conjure_zst.stderr
new file mode 100644
index 00000000000..0c4a978b81e
--- /dev/null
+++ b/tests/ui/consts/std/conjure_zst.stderr
@@ -0,0 +1,12 @@
+error[E0080]: evaluation panicked: aborted execution: attempted to instantiate uninhabited type `Infallible`
+  --> $DIR/conjure_zst.rs:5:38
+   |
+LL | const INVALID: Infallible = unsafe { mem::conjure_zst() };
+   |                                      ^^^^^^^^^^^^^^^^^^ evaluation of `INVALID` failed inside this call
+   |
+note: inside `conjure_zst::<Infallible>`
+  --> $SRC_DIR/core/src/mem/mod.rs:LL:COL
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0080`.