5
5
# Summary
6
6
7
7
When a struct type ` S ` has no fields (a so-called "empty struct"),
8
- allow it to be defined via either ` struct S; ` or ` struct S {} ` , and
9
- allow instances of it to be constructed and pattern-matched via either
10
- ` S ` or ` S {} ` .
8
+ allow it to be defined via either ` struct S; ` or ` struct S {} ` .
9
+ When defined via ` struct S; ` , allow instances of it to be constructed
10
+ and pattern-matched via either ` S ` or ` S {} ` .
11
+ When defined via ` struct S {} ` , require instances to be constructed
12
+ and pattern-matched solely via ` S {} ` .
11
13
12
14
# Motivation
13
15
@@ -46,7 +48,7 @@ and non-empty, and the associated revisions of changing removing and
46
48
adding curly braces is aggravating (both in effort revising the code,
47
49
and also in extra noise introduced into commit histories).
48
50
49
- This RFC proposes going back to the state we were in circa February
51
+ This RFC proposes an approach similar to the one we used circa February
50
52
2013, when both ` S0 ` and ` S0 { } ` were accepted syntaxes for an empty
51
53
struct. The parsing ambiguity that motivated removing support for
52
54
` S0 { } ` is no longer present (see the [ Ancient History] appendix).
@@ -55,34 +57,43 @@ in the language now.
55
57
56
58
# Detailed design
57
59
58
- * Allow ` S ` to be defined via either ` struct S; ` (as today)
59
- or ` struct S {} ` ("new")
60
+ There are two kinds of empty structs: Braced empty structs and
61
+ flexible empty structs. Flexible empty structs are a slight
62
+ generalization of the structs that we have today.
60
63
61
- * Allow instances of ` S ` to be constructed via either the
62
- expression ` S ` (as today) or the expression ` S {} ` ("new")
64
+ Flexible empty structs are defined via the syntax ` struct S; ` (as today).
63
65
64
- * Allow instances of ` S ` to be pattern matched via either the
65
- pattern ` S ` (as today) or the pattern ` S {} ` ("new").
66
+ Braced empty structs are defined via the syntax ` struct S { } ` ("new").
66
67
67
- Revise the grammar of struct item definitions so that one can write
68
- either ` struct S; ` or ` struct S { } ` . The two forms are synonymous.
69
- The first is preferred with respect to coding style; for example, the
70
- first is emitted by the pretty printer.
68
+ Both braced and flexible empty structs can be constructed via the
69
+ expression syntax ` S { } ` ("new"). Flexible empty structs, as today,
70
+ can also be constructed via the expression syntax ` S ` .
71
71
72
- Revise the grammar of expressions and patterns so that, when ` S ` is an
73
- empty struct, one can write either ` S ` or ` S { } ` . The two forms are
74
- synonymous. Again, the first is preferred with respect to coding
75
- style, and is emitted by the pretty printer.
72
+ Both braced and flexible empty structs can be pattern-matched via the
73
+ pattern syntax ` S { } ` ("new"). Flexible empty structs, as today,
74
+ can also be pattern-matched via the pattern syntax ` S ` .
76
75
77
- The format of the definiton has no bearing on the format of the
78
- expressions or pattern forms; either syntax can be used for any
79
- empty-struct, regardless of how it is defined.
76
+ Braced empty struct definitions solely affect the type namespace,
77
+ just like normal non-empty structs.
78
+ Flexible empty structs affect both the type and value namespaces.
79
+
80
+ As a matter of style, using braceless syntax is preferred for
81
+ constructing and pattern-matching flexible empty structs. For
82
+ example, pretty-printer tools are encouraged to emit braceless forms
83
+ if they know that the corresponding struct is a flexible empty struct.
84
+ (Note that pretty printers that handle incomplete fragments may not
85
+ have such information available.)
80
86
81
87
There is no ambiguity introduced by this change, because we have
82
88
already introduced a restriction to the Rust grammar to force the use
83
89
of parentheses to disambiguate struct literals in such contexts. (See
84
90
[ Rust RFC 25] ).
85
91
92
+ The expectation is that when migrating code from a flexible empty
93
+ struct to a non-empty struct, it can start by first migrating to a
94
+ braced empty struct (and then have a tool indicate all of the
95
+ locations where braces need to be added); after that step has been
96
+ completed, one can then take the next step of adding the actual field.
86
97
87
98
# Drawbacks
88
99
@@ -120,6 +131,32 @@ need to handle empty structs as a special case. We may continue
120
131
hitting bugs like [ CFG parse bug] . Some users will be annoyed but
121
132
most will probably cope.
122
133
134
+ ## Synonymous in all contexts
135
+
136
+ Alternative 3: An earlier version of this RFC proposed having `struct
137
+ S;` be entirely synonymous with ` struct S { }`, and the expression
138
+ ` S { } ` be synonymous with ` S ` .
139
+
140
+ This was deemed problematic, since it would mean that ` S { } ` would
141
+ put an entry into both the type and value namespaces, while
142
+ ` S { x: int } ` would only put an entry into the type namespace.
143
+ Thus the current draft of the RFC proposes the "flexible" versus
144
+ "braced" distinction for empty structs.
145
+
146
+ ## Never synonymous
147
+
148
+ Alternative 4: Treat ` struct S; ` as requiring ` S ` at the expression
149
+ and pattern sites, and ` struct S { } ` as requiring ` S { } ` at the
150
+ expression and pattern sites.
151
+
152
+ This in some ways follows a principle of least surprise, but it also
153
+ is really hard to justify having both syntaxes available for empty
154
+ structs with no flexibility about how they are used. (Note again that
155
+ one would have the option of choosing between
156
+ ` enum S { S } ` , ` struct S; ` , or ` struct S { } ` , each with their own
157
+ idiosyncrasies about whether you have to write ` S ` or ` S { } ` .)
158
+ I would rather adopt "Always Require Braces" than "Never Synonymous"
159
+
123
160
## Empty Tuple Structs
124
161
125
162
One might say "why are you including support for curly braces, but not
0 commit comments