Skip to content

Commit e504dd7

Browse files
committed
Allow unnamed fields with named types
1 parent c7961d8 commit e504dd7

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

text/0000-unnamed-fields.md

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,39 @@ struct S {
171171
}
172172
```
173173

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+
174207
## Mental model
175208

176209
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
344377
`_: struct { fields }` or `_: union { fields }`, where `fields` allows
345378
everything allowed within a `struct` or `union` declaration, respectively.
346379

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+
347384
The name `_` cannot currently appear as a field name, so this will not
348385
introduce any compatibility issues with existing code. The keyword `struct`
349386
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
423460
match the C syntax. However, this does not provide as natural an extension to
424461
support references to named structures.
425462

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-
450463
## Field aliases
451464

452465
Rather than introducing unnamed fields, we could introduce a mechanism to

0 commit comments

Comments
 (0)