-
Notifications
You must be signed in to change notification settings - Fork 583
Fields must fit in the type, even for repr(Rust) #2166
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -166,8 +166,15 @@ The `Rust` representation is the default representation for nominal types withou | |||||
| r[layout.repr.rust.layout] | ||||||
| The only data layout guarantees made by this representation are those required for soundness. These are: | ||||||
|
|
||||||
| 1. The offset of a field is divisible by that field's alignment. | ||||||
| 2. The alignment of the type is at least the maximum alignment of its fields. | ||||||
| 1. The offset of a constructible field is divisible by that field's alignment. | ||||||
| 2. The alignment of the type is at least the maximum alignment of its constructible fields. | ||||||
| 3. For any constructible field, its offset plus its size is at most the size of the type. | ||||||
|
|
||||||
| r[layout.repr.rust.layout.constructible] | ||||||
|
|
||||||
| A field is considered constructible if it is possible to create a value of the type containing the field. | ||||||
|
Member
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. How about:
Suggested change
Member
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. On second thought speaking in terms of individual fields feels weird, e.g. I could imagine: enum Foo {
A { x: u32 },
B { x: u32, y: ! },
}and I could imagine (I kinda want even) that we add a language feature whereby we can write So I'd rather say: a type has space for its inhabited variants, and an inhabited variant has space for its fields.
Member
Author
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. I think under this section of the reference, those xs have to be separate fields because otherwise a field might have multiple offsets, and this section talks about fields as if they have a single offset.
Member
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. I think this definition isn't quite right, because in the example Perhaps this should be talking about variants instead? (At least in the compiler even structs have variants, just exactly one variant, so the reference might want to adopt that approach.)
Member
Author
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. You can create values of type |
||||||
|
|
||||||
| For example, given `enum E { A { x: u32 }, B { y: u32, z: ! } }`, the field `x` is constructible because you can create the value `E::A { x: 0 }`, but the fields `y` and `z` are not constructible because the type of `z` is uninhabited, so it is impossible to create an `E::B` value. | ||||||
|
Member
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. It seems easier to define this as
Is there a definition of "inhabited" we can refer to?
Member
Author
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. Your proposed definition makes the guarantee apply to structs even if there are
Member
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. Oh, I didn't realize you meant to exclude structs with uninhabited fields. I think we do want to guarantee that fields are disjoint and fit in the type even for such structs -- that seems pretty much settled to me. |
||||||
|
|
||||||
| r[layout.repr.rust.layout.struct] | ||||||
| For [structs], it is further guaranteed that the fields do not overlap. That is, 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 fields are specified in the declaration of the type. | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.
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.
Below the type is
E; there is no typeE::B. So the next line doesn't follow from the existing definition.