From 2d896fa29e44ccc98e220257157899475d6eb089 Mon Sep 17 00:00:00 2001 From: Matthew Woodcraft Date: Tue, 15 Oct 2024 20:25:55 +0100 Subject: [PATCH 1/2] Say that dereferencing a pointer to a ZST is no longer undefined The new rules were tracked in https://github.com/rust-lang/rust/issues/117945 The corresponding update to the Reference was https://github.com/rust-lang/reference/pull/1541 --- src/exotic-sizes.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/exotic-sizes.md b/src/exotic-sizes.md index 5e6a3956..a93ab4a7 100644 --- a/src/exotic-sizes.md +++ b/src/exotic-sizes.md @@ -106,9 +106,9 @@ consequence of types with no size. In particular, pointer offsets are no-ops, and allocators typically [require a non-zero size][alloc]. Note that references to ZSTs (including empty slices), just like all other -references, must be non-null and suitably aligned. Dereferencing a null or -unaligned pointer to a ZST is [undefined behavior][ub], just like for any other -type. +references, must be non-null and suitably aligned. However, dereferencing a +null pointer to a ZST is not [undefined behavior][ub], unlike pointers to +other types. [alloc]: ../std/alloc/trait.GlobalAlloc.html#tymethod.alloc [ub]: what-unsafe-does.html From db1fac783b56132f5a0fa9e1a94c5a606a25ff29 Mon Sep 17 00:00:00 2001 From: Matthew Woodcraft Date: Sun, 27 Oct 2024 17:21:53 +0000 Subject: [PATCH 2/2] Zero Sized Types: stop saying that "dereferencing" a non-ZST is UB From discussion at https://rust-lang.zulipchat.com/#narrow/channel/136281-t-opsem/topic/What.20operations.20are.20allowed.20on.20ZST.20null.20pointers.3F - applying the dereference operator to a null pointer stopped being UB a few months ago - it's better to be explicit about whether we're talking about loads and stores or about applying the dereference operator I think we don't need to mention field projections here. --- src/exotic-sizes.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/exotic-sizes.md b/src/exotic-sizes.md index a93ab4a7..1de37f84 100644 --- a/src/exotic-sizes.md +++ b/src/exotic-sizes.md @@ -106,9 +106,9 @@ consequence of types with no size. In particular, pointer offsets are no-ops, and allocators typically [require a non-zero size][alloc]. Note that references to ZSTs (including empty slices), just like all other -references, must be non-null and suitably aligned. However, dereferencing a -null pointer to a ZST is not [undefined behavior][ub], unlike pointers to -other types. +references, must be non-null and suitably aligned. However, loading or storing +through a null pointer to a ZST is not [undefined behavior][ub], unlike +pointers to other types. [alloc]: ../std/alloc/trait.GlobalAlloc.html#tymethod.alloc [ub]: what-unsafe-does.html