@@ -171,6 +171,39 @@ struct S {
171
171
}
172
172
```
173
173
174
+ ## Unnamed fields with named types
175
+
176
+ An unnamed field may also use a named ` struct ` or ` union ` type. For instance:
177
+
178
+ ``` rust
179
+ union U {
180
+ x : i64 ,
181
+ y : f64 ,
182
+ }
183
+
184
+ struct S {
185
+ _ : U ,
186
+ z : usize ,
187
+ }
188
+ ```
189
+
190
+ Given these declarations, ` S ` would contain fields ` x ` , ` y ` , and ` z ` , with ` x `
191
+ and ` y ` overlapping. Such a declaration behaves in every way like the
192
+ equivalent declaration with an unnamed type declared within ` S ` , except that
193
+ this version of the declaration also defines a named union type ` U ` .
194
+
195
+ This syntax makes it possible to give a name to the intermediate type, while
196
+ still leaving the field unnamed. While C11 does not directly support inlining
197
+ of separately defined structures, compilers do support it as an extension, and
198
+ this addition allows the translation of such code.
199
+
200
+ This syntax allows for the common definition of sets of fields inlined into
201
+ several structures, such as a common header.
202
+
203
+ This syntax would also support an obvious translation of inline-declared
204
+ structures with names, by moving the declaration out-of-line; a macro could
205
+ easily perform such a translation.
206
+
174
207
## Mental model
175
208
176
209
In the memory layout of a structure, the alternating uses of ` struct { ... } `
@@ -344,6 +377,10 @@ Within a struct or union's fields, in place of a field name and value, allow
344
377
` _: struct { fields } ` or ` _: union { fields } ` , where ` fields ` allows
345
378
everything allowed within a ` struct ` or ` union ` declaration, respectively.
346
379
380
+ Additionally, allow ` _ ` as the name of a field whose type refers to a ` struct `
381
+ or ` union ` . All of the fields of that ` struct ` or ` union ` must be visible to
382
+ the current module.
383
+
347
384
The name ` _ ` cannot currently appear as a field name, so this will not
348
385
introduce any compatibility issues with existing code. The keyword ` struct `
349
386
cannot appear as a field type, making it entirely unambiguous. The contextual
@@ -423,30 +460,6 @@ and `_: struct { fields }`, we could omit the field name entirely, and write
423
460
match the C syntax. However, this does not provide as natural an extension to
424
461
support references to named structures.
425
462
426
- ## Unnamed fields with named types
427
-
428
- In addition to allowing the inline definition of the contents of unnamed
429
- ` struct ` or ` union ` fields, we could also allow declaring an unnamed field with
430
- a named type. For instance:
431
-
432
- ``` rust
433
- union U { x : i64 , y : f64 }
434
- struct S { _ : U , z : usize }
435
- ```
436
-
437
- Given these declarations, ` S ` would contain fields ` x ` , ` y ` , and ` z ` , with ` x `
438
- and ` y ` overlapping.
439
-
440
- This syntax makes it possible to give a name to the intermediate type, while
441
- still leaving the field unnamed. While C11 does not directly support inlining
442
- of separately defined structures, compilers do support it as an extension. This
443
- syntax would allow for the common definition of sets of fields inlined into
444
- several structures, such as a common header.
445
-
446
- This syntax would also support an obvious translation of inline-declared
447
- structures with names, by moving the declaration out-of-line; a macro could
448
- perform such a translation.
449
-
450
463
## Field aliases
451
464
452
465
Rather than introducing unnamed fields, we could introduce a mechanism to
0 commit comments