Skip to content

Commit

Permalink
clarify that the tautological assumption that a zero-sized field has …
Browse files Browse the repository at this point in the history
…zero-size is not that common
  • Loading branch information
gnzlbg committed Aug 11, 2019
1 parent b385179 commit 0f03acd
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion reference/src/layout/unions.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ assert_eq!(size_of::<U>(), 2);
#### Zero-sized fields

If a `#[repr(C)]` union contains a field of zero-size, that field does not
occupy space in the union. For example:
occupy space in Rust unions (as opposed to, e.g., in C++). For example:

This comment has been minimized.

Copy link
@RalfJung

RalfJung Aug 14, 2019

Member

Isn't it the case that in C++, there are no types of size 0? In that case, "if a field has size 0, X happens" is a true statement for C++ as well, and vacuously so. IOW, C++ would not be a counter-example to "fields of zero size have size zero".

This comment has been minimized.

Copy link
@gnzlbg

gnzlbg Aug 14, 2019

Author Contributor
struct S {};
struct A {
    char a;
    [[no_unique_address]] S s;
};
assert(sizeof(A) == 1);
struct B: S {
    char a;
};
assert(sizeof(B) == 1);
struct C {
    char a;
    S s;
};
assert(sizeof(C) == 2);

This comment has been minimized.

Copy link
@RalfJung

RalfJung Aug 14, 2019

Member

And what is sizeof(S)?

This comment has been minimized.

Copy link
@gnzlbg

gnzlbg Aug 14, 2019

Author Contributor

sizeof(S) is 1

This comment has been minimized.

Copy link
@RalfJung

RalfJung Aug 14, 2019

Member

Then your example does not involve a "field of zero-size".

This comment has been minimized.

Copy link
@gnzlbg

gnzlbg Aug 14, 2019

Author Contributor

Fixed

This comment has been minimized.

Copy link
@gnzlbg

gnzlbg Aug 14, 2019

Author Contributor

Also, two of the examples do involve a "field of zero-size" (if the field of type S wouldn't have size 0, then the asserts of size 1 would fail), what they do not involve is a zero-sized type because C++ does not have those. What C++ has it calls "empty types" and they have different sizes depending on how and where they are used.

This comment has been minimized.

Copy link
@RalfJung

RalfJung Aug 14, 2019

Member

That seems like a C++-only concept then (and one with a real bad name at that), not something that directly applies to Rust.

This comment has been minimized.

Copy link
@gnzlbg

gnzlbg Aug 14, 2019

Author Contributor

Sure, but it was decided in the struct discussion that raising these types of differences with C++ was worth the trouble, because a lot of users use C FFI to interface with C++ (up to the point that we define our enum layout for C++, and not C).

If you disagree with documenting these kinds of things, and others agree, we can remove them.

This comment has been minimized.

Copy link
@RalfJung

RalfJung Aug 14, 2019

Member

It is worth mentioning them in your "Note on C++ compat". But this is outside that note.

This comment has been minimized.

Copy link
@gnzlbg

gnzlbg Aug 14, 2019

Author Contributor

Indeed, removed the note.


```rust
# use std::mem::{size_of, align_of};
Expand Down

0 comments on commit 0f03acd

Please sign in to comment.