Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement empty structs with braces (RFC 218) #28336

Merged
merged 3 commits into from
Sep 18, 2015

Conversation

petrochenkov
Copy link
Contributor

Closes #24266
Closes #16819

@rust-highfive
Copy link
Collaborator

r? @huonw

(rust_highfive has picked a reviewer for you, use r? to override)

@@ -8,13 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: -Z parse-only
// Empty struct defined with braces shouldn't add names into value namespace
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to have a test for it, but.

Isn't this a surprising gotcha? struct S {} and struct S; being noticeably different?.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the RFC is quite explicit about this design, so that was just something new to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a part of the RFC and a result of long discussion.
Personally, I find this quite logical - struct S {} is a usual struct and it doesn't add anything to the value namespace, it just happens to have an empty list of fields. struct S; in its turn is syntactically special and represents usual struct + some special magic.

@huonw
Copy link
Member

huonw commented Sep 13, 2015

r? @pnkfelix (random member of the compiler team)

@rust-highfive rust-highfive assigned pnkfelix and unassigned huonw Sep 13, 2015
@@ -1178,11 +1178,20 @@ let px: i32 = match p { Point(x, _) => x };
```

A _unit-like struct_ is a structure without any fields, defined by leaving off
the list of fields entirely. Such types will have a single value. For example:
the list of fields entirely. Such structure implicitly defines a constant of
its type with the same name. For example:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I like this way of phrasing the semantics better than the RFC text itself (which had that odd "flexible" versus "braced" distinction). Bravo
  2. Nit: I think the correct grammar here would be "Such a structure implicitly defines"; but honestly I won't let this alone block the PR. (Let's see where the review goes.)

@pnkfelix
Copy link
Member

This looks great.

I noticed the RFC didn't specify a feature gate. I think that is due to that RFC being originally authored before we had a policy of by-default attaching some feature gate to every RFC.

I personally would not mind landing this without a feature gate, but perhaps it is worth double-checking that this is what was intended. I'll add a note to the tracking issue about this.

@petrochenkov
Copy link
Contributor Author

I thought about a feature gate, but decided not to add it. It's not like the semantics of struct S {} is going to change and there's not much need in "gaining experience" in using this feature.

@nikomatsakis
Copy link
Contributor

Feature gate does feel a bit like overkill. I threw this on the agenda for lang mtg tomorrow.

@nrc
Copy link
Member

nrc commented Sep 17, 2015

I would probably prefer a feature gate - the whole point of them is that we don't expect things to be bad but want to be careful. OTOH, I'm not too bothered.

@aturon
Copy link
Member

aturon commented Sep 17, 2015

I think we should gate as a matter of course. It'd be good to lay down an explicit policy about this.

FWIW, the libs team policy is that any nontrivial new API has to undergo implementation FCP for at least a cycle before being stabilized -- the same as being gated for a cycle here. We do stabilization FCPs in a batch at the beginning of a cycle, and collect 6 weeks of comments before deciding just prior to the next beta. It'd be a bit odd to be more lax with language features :P

@nikomatsakis
Copy link
Contributor

Yeah, makes sense. Basically every time I've skipped a feature gate I've
regretted it.

On Thu, Sep 17, 2015 at 6:51 PM, Aaron Turon notifications@github.com
wrote:

I think we should gate as a matter of course. It'd be good to lay down an
explicit policy about this.

FWIW, the libs team policy is that any nontrivial new API has to undergo
implementation FCP for at least a cycle before being stabilized -- the same
as being gated for a cycle here. We do stabilization FCPs in a batch at the
beginning of a cycle, and collect 6 weeks of comments before deciding just
prior to the next beta. It'd be a bit odd to be more lax with language
features :P


Reply to this email directly or view it on GitHub
#28336 (comment).

@petrochenkov
Copy link
Contributor Author

Updated with a feature gate.

@pnkfelix
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Sep 18, 2015

📌 Commit 1eb42f1 has been approved by pnkfelix

@bors
Copy link
Contributor

bors commented Sep 18, 2015

⌛ Testing commit 1eb42f1 with merge dc1c797...

bors added a commit that referenced this pull request Sep 18, 2015
@alexcrichton alexcrichton added the relnotes Marks issues that should be documented in the release notes of the next release. label Sep 18, 2015
@bors bors merged commit 1eb42f1 into rust-lang:master Sep 18, 2015
@petrochenkov petrochenkov deleted the empstr branch September 21, 2015 13:03
bors added a commit that referenced this pull request Oct 14, 2015
This patch uses the same data structures for structs and enum variants in AST and HIR. These changes in data structures lead to noticeable simplification in most of code dealing with them.
I didn't touch the top level, i.e. `ItemStruct` is still `ItemStruct` and not `ItemEnum` with one variant, like in the type checker.
As part of this patch, structures and variants get the `kind` field making distinction between "normal" structs, tuple structs and unit structs explicit instead of relying on the number of fields and presence of constructor `NodeId`. In particular, we can now distinguish empty tuple structs from unit structs, which was impossible before! Comprehensive tests for empty structs are added and some improvements to empty struct feature gates are made. Some tests don't pass due to issue #28692 , they are still there for completeness, but are commented out.
This patch fixes issue mentioned in #16819 (comment), now emptiness of tuple structs is checked after expansion.
It also touches #28750 by providing span for visit_struct_def
cc #28336

r? @nrc
@petrochenkov petrochenkov mentioned this pull request Sep 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes Marks issues that should be documented in the release notes of the next release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants