From 6702509ab57fea68a61ac402a769ea5b3009e2ed Mon Sep 17 00:00:00 2001 From: gnzlbg Date: Thu, 20 Jun 2019 17:50:42 +0200 Subject: [PATCH 1/3] Validity of unions with a zero-sized field --- reference/src/SUMMARY.md | 2 ++ reference/src/validity/unions.md | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 reference/src/validity/unions.md diff --git a/reference/src/SUMMARY.md b/reference/src/SUMMARY.md index 3dcb465b..bf52b9ee 100644 --- a/reference/src/SUMMARY.md +++ b/reference/src/SUMMARY.md @@ -11,5 +11,7 @@ - [Function pointers](./layout/function-pointers.md) - [Arrays and Slices](./layout/arrays-and-slices.md) - [Packed SIMD vectors](./layout/packed-simd-vectors.md) +- [Validity](./validity.md) + - [Unions](./validity/unions.md) - [Optimizations](./optimizations.md) - [Return value optimization](./optimizations/return_value_optimization.md) diff --git a/reference/src/validity/unions.md b/reference/src/validity/unions.md new file mode 100644 index 00000000..27743255 --- /dev/null +++ b/reference/src/validity/unions.md @@ -0,0 +1,25 @@ +# Validity of unions + +**Disclaimer**: This chapter is a work-in-progress. What's contained here +represents the consensus from issue [#73]. The statements in here are not (yet) +"guaranteed" not to change until an RFC ratifies them. + +The bit `i` of an union is allowed to have value `v` _iff_ there is a variant of +the union such that bit `i` of the variant is allowed to have value `v`. We +assume all variants to be "filled up" to the same size with padding, which may +have any value. + +## Validity of unions with zero-sized fields + +A union containing a zero-sized field can contain any value. An example of such +an union is [`MaybeUninit`]. + +
Rationale + +This follows from the definition of the values that `union` bits are allowed to +take. The zero-sized type has size `0`, and its variant is filled up to +the `union` size with padding. Since padding is allowed to take any value, any +bit in the `union` is allowed to take any value. + +[#73]: https://github.com/rust-lang/unsafe-code-guidelines/issues/73 +[`MaybeUninit`]: https://doc.rust-lang.org/std/mem/union.MaybeUninit.html From bbc2918435402ca10f435acdaefad148b8edec5d Mon Sep 17 00:00:00 2001 From: gnzlbg Date: Thu, 20 Jun 2019 19:08:52 +0200 Subject: [PATCH 2/3] Remove definition and rationale --- reference/src/validity/unions.md | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/reference/src/validity/unions.md b/reference/src/validity/unions.md index 27743255..743115a1 100644 --- a/reference/src/validity/unions.md +++ b/reference/src/validity/unions.md @@ -4,22 +4,10 @@ represents the consensus from issue [#73]. The statements in here are not (yet) "guaranteed" not to change until an RFC ratifies them. -The bit `i` of an union is allowed to have value `v` _iff_ there is a variant of -the union such that bit `i` of the variant is allowed to have value `v`. We -assume all variants to be "filled up" to the same size with padding, which may -have any value. - ## Validity of unions with zero-sized fields A union containing a zero-sized field can contain any value. An example of such an union is [`MaybeUninit`]. -
Rationale - -This follows from the definition of the values that `union` bits are allowed to -take. The zero-sized type has size `0`, and its variant is filled up to -the `union` size with padding. Since padding is allowed to take any value, any -bit in the `union` is allowed to take any value. - [#73]: https://github.com/rust-lang/unsafe-code-guidelines/issues/73 [`MaybeUninit`]: https://doc.rust-lang.org/std/mem/union.MaybeUninit.html From 64107bd29710cc20d85f54e3eac04696178777b4 Mon Sep 17 00:00:00 2001 From: gnzlbg Date: Thu, 20 Jun 2019 19:31:16 +0200 Subject: [PATCH 3/3] Update reference/src/validity/unions.md Co-Authored-By: Robin Kruppe --- reference/src/validity/unions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reference/src/validity/unions.md b/reference/src/validity/unions.md index 743115a1..86c95478 100644 --- a/reference/src/validity/unions.md +++ b/reference/src/validity/unions.md @@ -6,7 +6,7 @@ represents the consensus from issue [#73]. The statements in here are not (yet) ## Validity of unions with zero-sized fields -A union containing a zero-sized field can contain any value. An example of such +A union containing a zero-sized field can contain any bit pattern. An example of such an union is [`MaybeUninit`]. [#73]: https://github.com/rust-lang/unsafe-code-guidelines/issues/73