-
Notifications
You must be signed in to change notification settings - Fork 501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Specify guarantees for repr(rust) structs #1152
Changes from 2 commits
5a8a664
e9a1ddf
377c524
f9ec1d2
1275bcc
3ecc681
1ae7c2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,9 +86,10 @@ String slices are a UTF-8 representation of characters that have the same layout | |
|
||
## Tuple Layout | ||
|
||
Tuples do not have any guarantees about their layout. | ||
Tuples have the same layout guarantees as a struct with the same fields when | ||
laid out according to the default struct representation. | ||
|
||
The exception to this is the unit tuple (`()`) which is guaranteed as a | ||
The exception to this is the unit tuple (`()`), which is guaranteed as a | ||
zero-sized type to have a size of 0 and an alignment of 1. | ||
Comment on lines
+91
to
92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the former statement imply this statement? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not as I wrote it here, but you could certainly argue that it should be changed so it does. |
||
|
||
## Trait Object Layout | ||
|
@@ -162,7 +163,26 @@ representation will not change the layout of `Inner`. | |
Nominal types without a `repr` attribute have the default representation. | ||
Informally, this representation is also called the `rust` representation. | ||
|
||
There are no guarantees of data layout made by this representation. | ||
The only data layout guarantees made by this representation are those required | ||
for soundness. They are: | ||
|
||
1. The fields of the struct are properly aligned. | ||
Darksonn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
2. The fields do not overlap. | ||
3. The minimum alignment of the struct is at least the maximum alignment of its | ||
fields. | ||
Darksonn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Formally, the first guarantee means that the offset of any field in the struct | ||
is divisible by that field's alignment. The second guarantee means that the | ||
fields can be ordered such that the offset plus the size of any field is less | ||
than or equal to the offset of the next field in the ordering. The ordering does | ||
not have to be the same as the order in which the field are specified in the | ||
Darksonn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
declaration of the struct. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (also can be ignored) If non-normative statements that provide motivation/context are permitted, it would also be worth noting here that only the compiler has enough information to optimally lay out a generic type for all possible type substitutions. i.e. |
||
|
||
Be aware that the second guarantee does not imply that the fields have distinct | ||
addresses: zero-sized types may have the same address as other fields in the | ||
same struct. | ||
|
||
Darksonn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
There are no other guarantees of data layout made by this representation. | ||
|
||
### The `C` Representation | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm opposed to documenting this, as it precludes us from tuple variadic approaches which require "tail borrowing."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rfcbot concern tuple-tail-borrowing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate on what that is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There have been a number of discussions in relation to variadic generics about wanting the ability to write code which could recurse over the structure of a tuple, HList-style. In order for this to work, the "tail" of a tuple must be represented contiguously. That is, for tuple
(A, B, C, D)
, there must be a sub-tuple(B, C, D)
in memory. Allowing arbitrary ordering as structs do would cause layouts like(A, D, C, B)
, from which it's impossible to create a reference to a tuple of a sub-structure like(B, C, D)
.Note that hlist-compatible representations of tuples need not be strictly ordered-- it's fine for each new element to be placed at either the front, back, or even inside of the tuple so long as the tail tuple remains a part of the representation.
I'd like our representation documentation to stay forward-compatible with tuple tail-borrowing until we've explicitly decided not to do it so that users aren't expecting to be able to transmute between
struct S(A, B, C, D);
and tuple(A, B, C, D)
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I intended the guarantees I have written, what you described here would still be backwards-compatible to introduce, because that particular ordering is just one among the many different orderings that the guarantees allow the compiler to choose. As for the struct, you also can't transmute between different
#[repr(Rust)]
structs with the same fields either.I would be happy to elaborate on this in the text though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, I misunderstood "have the same layout guarantees as a struct with the same fields". I over-read this to mean "have the same layout as a struct with the same fields". Thanks for clarifying! Still, I can imagine this documentation going stale: if we provide more guarantees for structs in the future, we wouldn't necessarily match those guarantees for tuples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to think that tuples and tuple structs ought to have analogous layout. The UCG conversation at least came to that conclusion (we do discuss "tail references" as well). There was talk, as I recall, of making
(T, T, T)
sort of special case, to permit casting that to[T; 3]
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of the new phrasing?