Skip to content

Commit f44ac71

Browse files
committed
Update to deal with aforementioned type- vs value- namespace issue.
1 parent d569e5a commit f44ac71

File tree

1 file changed

+58
-21
lines changed

1 file changed

+58
-21
lines changed

active/0000-empty-structs-with-braces.md

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
# Summary
66

77
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 {}`.
1113

1214
# Motivation
1315

@@ -46,7 +48,7 @@ and non-empty, and the associated revisions of changing removing and
4648
adding curly braces is aggravating (both in effort revising the code,
4749
and also in extra noise introduced into commit histories).
4850

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
5052
2013, when both `S0` and `S0 { }` were accepted syntaxes for an empty
5153
struct. The parsing ambiguity that motivated removing support for
5254
`S0 { }` is no longer present (see the [Ancient History] appendix).
@@ -55,34 +57,43 @@ in the language now.
5557

5658
# Detailed design
5759

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.
6063

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).
6365

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").
6667

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`.
7171

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`.
7675

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.)
8086

8187
There is no ambiguity introduced by this change, because we have
8288
already introduced a restriction to the Rust grammar to force the use
8389
of parentheses to disambiguate struct literals in such contexts. (See
8490
[Rust RFC 25]).
8591

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.
8697

8798
# Drawbacks
8899

@@ -120,6 +131,32 @@ need to handle empty structs as a special case. We may continue
120131
hitting bugs like [CFG parse bug]. Some users will be annoyed but
121132
most will probably cope.
122133

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+
123160
## Empty Tuple Structs
124161

125162
One might say "why are you including support for curly braces, but not

0 commit comments

Comments
 (0)