Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upClarify the relationships between various kinds of structs and variants #1506
Conversation
arielb1
reviewed
Feb 22, 2016
|
|
||
| As a basic struct, a unit struct can participate in struct expressions `US{}`, including FRU | ||
| `US{..s}` and in struct patterns `US{}`/`US{..}`. In both cases the path `US` of the expression | ||
| or pattern is looked up in the type namespace. |
This comment has been minimized.
This comment has been minimized.
arielb1
Feb 22, 2016
Contributor
A unit struct/variant pattern is a struct/variant pattern, not a constant pattern, and therefore is exhaustive.
This comment has been minimized.
This comment has been minimized.
arielb1
reviewed
Feb 22, 2016
| Fields of a braced struct can be accessed with dot syntax `s.field1`. | ||
|
|
||
| Note: struct *variants* are currently defined in the value namespace in addition to type namespace, | ||
| there are no particular reasons for this and this is probably temporary. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
petrochenkov
Feb 22, 2016
Author
Contributor
No. (Not now at least.)
See rust-lang/rust#30882 (comment)
oli-obk
reviewed
Feb 22, 2016
|
|
||
| New: Permit tuple structs and tuple variants with 0 fields. This restriction is artificial and can | ||
| be lifted trivially. Macro writers dealing with tuple structs/variants will be happy to get rid of | ||
| this one special case. |
This comment has been minimized.
This comment has been minimized.
oli-obk
Feb 22, 2016
Contributor
examples of crates that had to implement workarounds because of this limitation?
This comment has been minimized.
This comment has been minimized.
petrochenkov
Feb 22, 2016
Author
Contributor
I have an anecdotal evidence from @DanielKeep but no concrete examples so far.
This comment has been minimized.
This comment has been minimized.
durka
Feb 22, 2016
Contributor
A more subtle question might be crates that are broken, but haven't noticed yet, because nobody tried to make it generate a struct with no fields.
This comment has been minimized.
This comment has been minimized.
|
I really like these ideas. If they're not taken up by Rust, I may steal them for my own language :) |
petrochenkov
force-pushed the
petrochenkov:adtkinds
branch
from
dfbf8cb
to
433edd9
Feb 28, 2016
alexcrichton
added
the
T-lang
label
Mar 7, 2016
nikomatsakis
assigned
pnkfelix
Mar 10, 2016
nrc
reviewed
Apr 27, 2016
| Fields of a braced struct can be accessed with dot syntax `s.field1`. | ||
|
|
||
| Note: struct *variants* are currently defined in the value namespace in addition to type namespace, | ||
| there are no particular reasons for this and this is probably temporary. |
This comment has been minimized.
This comment has been minimized.
nrc
Apr 27, 2016
Member
I guess this is because regular variants are in both namespaces and braced variants are a mistake. However, if we one day get named fields for functions, then I guess we could use struct names as ctor functions, so it might be useful. Too bad that ship has sailed for regular structs.
nrc
reviewed
Apr 27, 2016
| and a constant with the same name<sup>Note 1</sup> | ||
|
|
||
| ``` | ||
| const US: US = US{}; |
This comment has been minimized.
This comment has been minimized.
nrc
Apr 27, 2016
Member
c.f., an actual braced struct with zero fields, which is not in the value namespace
This comment has been minimized.
This comment has been minimized.
durka
Apr 27, 2016
Contributor
I think that's the point. An empty struct-defined-with-without-braces is like an empty struct-defined-with-braces, plus a const like this.
nrc
reviewed
Apr 27, 2016
|
|
||
| Everything related to braced structs and unit structs is already implemented. | ||
|
|
||
| New: Permit tuple structs and tuple variants with 0 fields. This restriction is artificial and can |
This comment has been minimized.
This comment has been minimized.
nrc
Apr 27, 2016
Member
Could you expand on the exact semantics here, I assume there is no interoperability between the different kinds of zero-sized structs. Whereas, a unit struct can be thought of as a single constant object, I would think of zero-field tuple structs generating a potentially infinite number of unique instances, but I can't think of how this makes an actual difference in code.
This comment has been minimized.
This comment has been minimized.
durka
Apr 27, 2016
Contributor
a unit struct can be thought of as a single constant object, I would think of zero-field tuple structs generating a potentially infinite number of unique instances
Can you expand on your intuition here?
This comment has been minimized.
This comment has been minimized.
petrochenkov
Apr 27, 2016
Author
Contributor
struct TS(); is roughly equivalent to
struct TS {
}
fn TS() -> TS {
TS{}
}
, it's described in the section about tuple structs above.
nrc
reviewed
Apr 27, 2016
| This also means that `S{..}` patterns can be used to match structures and variants of any kind. | ||
| The desire to have such "match everything" patterns is sometimes expressed given | ||
| that number of fields in structures and variants can change from zero to non-zero and back during | ||
| development. |
This comment has been minimized.
This comment has been minimized.
nrc
reviewed
Apr 27, 2016
| for braces structs (`ExprStruct`/`PatKind::Struct`), tuple structs | ||
| (`ExprCall`/`PatKind::TupleStruct`) and unit structs (`ExprPath`/`PatKind::Path`). With proposed | ||
| changes `#[derive]` could simplify its logic and always generate braced forms for expressions and | ||
| patterns. |
This comment has been minimized.
This comment has been minimized.
nrc
Apr 27, 2016
Member
This makes me uncomfortable since it weakens the correspondence between the declaration, pattern, and creation syntaxes. And makes the bracing more fuzzy, where we've been trying to be stricter. Since the only motivation is in procedural macros, I wonder if there is a solution using clever libraries or tooling, rather than changing the language?
aturon
added
the
I-nominated
label
Jul 28, 2016
petrochenkov
referenced this pull request
Jul 31, 2016
Merged
Implement RFC 1506 "Clarify the relationships between various kinds of structs and variants" #35138
This comment has been minimized.
This comment has been minimized.
|
Implementation: rust-lang/rust#35138 |
This comment has been minimized.
This comment has been minimized.
|
Note that everything the implementation does (modulo tests and feature gates) is removing non-fatal errors and accepting a new grammar production in the parser, i.e. the RFC only legitimizes things that already work internally. |
Mark-Simulacrum
reviewed
Jul 31, 2016
| function calls, but the compiler reserves the right to make observable improvements to them based | ||
| on the additional knowledge, that `TS` is a constructor. | ||
|
|
||
| Note 1: the automatically assigned field names are quite interesting, they are not identifiers |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Hear ye, hear ye! This RFC is now entering final comment period. In the most recent meeting, @nrc and I (the only two in attendance) both felt inclined to accept (though no final decision has been reached). In general, the feeling is that this RFC is not needed in any strict sense, though enabling |
nikomatsakis
added
the
final-comment-period
label
Aug 8, 2016
This comment has been minimized.
This comment has been minimized.
vitiral
commented
Aug 11, 2016
|
I think making the model easier to understand and more consistent (and without breaking anything) is worth it in its own right. Making macros simpler is pretty awesome too. The only worry is that there is some future feature that would need this syntax. On the other hand, this RFC makes a pretty clear case that such new syntax would be confusing and non intuitive. I'm |
This comment has been minimized.
This comment has been minimized.
vitiral
commented
Aug 11, 2016
|
Also, making TupleStruct just shorthand for NamedStruct but with numeric names makes a lot of sense imo. Not having to do |
This comment has been minimized.
This comment has been minimized.
vitiral
commented
Aug 11, 2016
|
I'm not sure I totally understand the "pattern matching a function" definition though. You can't pattern match to a function, so it is really confusing. I would change that description to something more accurate with how things actually work |
This comment has been minimized.
This comment has been minimized.
|
I think this is a great change. I agree it's not necessary, but it also seems "good", without much, if any, downside. |
nrc
removed
the
I-nominated
label
Aug 11, 2016
This comment has been minimized.
This comment has been minimized.
|
I'm very much in favor as well. This is exactly the mental model I want, and I'd love to get rid of the various pedantic restrictions we have today. |
This comment has been minimized.
This comment has been minimized.
|
Huzzah! The @rust-lang/lang team has decided to accept this RFC. |
nikomatsakis
referenced this pull request
Aug 12, 2016
Closed
Tracking issue for "clarified ADT kinds" (RFC 1506) #35626
This comment has been minimized.
This comment has been minimized.
|
Tracking issue: rust-lang/rust#35626 If you'd like to keep following the development of this feature, please subscribe to that issue, thanks! :) |
nikomatsakis
merged commit 433edd9
into
rust-lang:master
Aug 12, 2016
petrochenkov
referenced this pull request
Aug 17, 2016
Merged
Fix #[derive] for empty tuple structs/variants #35728
petrochenkov
deleted the
petrochenkov:adtkinds
branch
Sep 21, 2016
matthewjasper
referenced this pull request
Apr 7, 2017
Merged
Document rfc 1506 - clarified adt kinds #37
Centril
referenced this pull request
Jan 18, 2018
Merged
RFC: Tuple struct construction with `Self(v1, v2, ..)` #2302
Centril
added
A-syntax
A-patterns
A-expressions
labels
Nov 23, 2018
This comment has been minimized.
This comment has been minimized.
Frederik-Baetens
commented
Dec 27, 2018
•
|
Hello, sorry to bother you with this, but the rendered proposal link is broken, I think it should be: https://github.com/rust-lang/rfcs/blob/master/text/1506-adt-kinds.md |
petrochenkov commentedFeb 22, 2016
Provide a simple model describing three kinds of structs and variants and their relationships + some small language extensions following from this model.
Rendered
cc rust-lang/rust#31757 (comment)