@@ -84,40 +84,43 @@ Structs can have various `#[repr]` flags that influence their layout:
84
84
85
85
### Default layout ("repr rust")
86
86
87
- The default layout of structs is not specified. Effectively, the
88
- compiler provdes a deterministic function per struct definition that
89
- defines its layout. This function may as principle take as input the
90
- entire input to the compiler. Therefore, any of the the following might
91
- influence layout:
92
-
93
- - the types of the struct's fields
94
- - the layout of other structs (including structs not included within this struct)
95
- - compiler settings
96
- - the results of profile information which are given to the compiler
97
- for the purpose of PGO etc
98
-
99
- As of this writing, we have not reached a full consensus on what
100
- limitations should exist on possible field struct layouts. Therefore,
101
- the default layout of structs is considered undefined and subject to
102
- change between individual compilations. This implies that (among other
103
- things) two structs with the same field types may not be laid out in
104
- the same way (for example, the hypothetical struct representing tuples
105
- may be laid out differently from user-declared structs).
106
-
107
- Further, the layout of structs is always defined relative to the
108
- ** struct definition** plus a substitution supplying values for each of
109
- the struct's generic types (in contrast to just considering the fully
110
- monomorphized field types). This is necessary because the presence or
111
- absence of generics can make a difference (e.g., `struct Foo { x: u16,
112
- y: u32 }` and ` struct Foo<T > { x: u16, y: T }` where ` T = u32` are not
113
- guaranteed to be identical), owing to the possibility of unsizing
114
- coercions.
87
+ ** The default layout of structs is not specified.** As of this
88
+ writing, we have not reached a full consensus on what limitations
89
+ should exist on possible field struct layouts, so effectively one must
90
+ assume that the compiler can select any layout it likes for each
91
+ struct on each compilation, and it is not required to select the same
92
+ layout across two compilations. This implies that (among other things)
93
+ two structs with the same field types may not be laid out in the same
94
+ way (for example, the hypothetical struct representing tuples may be
95
+ laid out differently from user-declared structs).
96
+
97
+ Known things that can influence layout (non-exhaustive):
98
+
99
+ - the type of the struct fields and the layout of those types
100
+ - compiler settings, including esoteric choices like optimization fuel
101
+
102
+ ** A note on determinism.** The definition above does not guarantee
103
+ determinism between executions of the compiler -- two executions may
104
+ select different layouts, even if all inputs are identical. Naturally,
105
+ in practice, the compiler aims to produce deterministic output for a
106
+ given set of inputs. However, it is difficult to produce a
107
+ comprehensive summary of the various factors that may affect the
108
+ layout of structs, and so for the time being we have opted for a
109
+ conservative definition.
115
110
116
111
** Compiler's current behavior.** As of the time of this writing, the
117
112
compiler will reorder struct fields to minimize the overall size of
118
113
the struct (and in particular to eliminate padding due to alignment
119
- restrictions). The final field, however, is not reordered if an
120
- unsizing coercion may be applied.
114
+ restrictions).
115
+
116
+ Layout is presently defined not in terms of a "fully monomorphized"
117
+ struct definition but rather in terms of its generic definition along
118
+ with a set of substitutions (values for each type parameter; lifetime
119
+ parameters do not affect layout). This distinction is important
120
+ because of * unsizing* -- if the final field has generic type, the
121
+ compiler will not reorder it, to allow for the possibility of
122
+ unsizing. E.g., ` struct Foo { x: u16, y: u32 } ` and `struct Foo<T > {
123
+ x: u16, y: T }` where ` T = u32` are not guaranteed to be identical.
121
124
122
125
#### Unresolved questions
123
126
0 commit comments