Uh oh!
There was an error while loading. Please reload this page.
syntax: enable attributes and cfg on struct fields - #38814
Conversation
rust-highfive
commented
Jan 4, 2017
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nrc (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
nrc
commented
Jan 4, 2017
cc @rust-lang/lang are we happy to add this? Given that we allow |
nrc
commented
Jan 4, 2017
Hmm, actually this allows all attributes on fields in struct lits. That still seems ok to me, but not as much of a slam dunk as I thought. |
nrc
commented
Jan 4, 2017
@Ralith the code looks OK to me, it does need a few tests though. |
Re: other attributes: Would it make sense to be more restrictive? Are there any other locations in the language where cfg is permitted but other attributes are not? Beginning work on tests now. |
aturon
commented
Jan 4, 2017
@nrc I'm in favor of this change. In general, we've been moving to add attributes everywhere unless there's ambiguity/hard to discern meaning (as we saw with expressions). |
Mark-Simulacrum
commented
Jan 4, 2017
Is using cfg on expressions and/or statements stable? It seems like this might cause problems if that isn't available; since that would require duplicating entire functions for each platform combination. In general, though, I'm in favor. I'm slightly concerned we're moving in the direction of C/C++, where libraries like OpenSSL have a large quantity of fields being "optional" and disabled on certain platforms and/or with certain features, which to some extent heightens complexity for users. I believe @sfackler might have some relevant thoughts on this as he manages the bindings for OpenSSL. |
sfackler
commented
Jan 4, 2017
This doesn't really matter for rust-openssl since we never actually create those structs. I definitely agree that conditionally defining public-facing fields is a pain, though doing that for enum variants is fairly common. On the other hand, conditionally defining internal fields can be pretty nice (e.g. https://github.com/brson/error-chain/blob/master/src/lib.rs#L458) |
For illustration, this patch was inspired by the awkwardness of my employing exactly that pattern in my experimental tokio-based window/input system binding, in which I'm trying to provide a uniform interface dispatching over a set of concrete implementations that differs between platforms. Perhaps there's a better way to accomplish this, but this certainly seems to be the most obvious. Notably, this change alone reduces the need for duplicating entire functions (e.g. |
nikomatsakis
commented
Jan 4, 2017
I am in favor of this change. |
nikomatsakis
commented
Jan 4, 2017
@rfcbot fcp merge The proposal here is to allow attributes on struct fields. This is a relatively small extension to the general trend of allowing attributes on statements and so forth. @nrc is proposing that we approve this (with a feature gate, naturally) without an FCP. I'm in favor since an RFC feels like overkill and we have a big enough backlog as it is. What do you think, @rust-lang/lang? |
Team member @nikomatsakis has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
nrc
commented
Jan 5, 2017
I do not think this makes sense, we should allow all attributes, including cfg (sorry for the confusion in my earlier comments). @Ralith could you add a feature gate to this PR please? |
Ralith
commented
Jan 5, 2017
Added a feature gate |
There was a problem hiding this comment.
I believe field.attrs.iter() can be just field.attrs.
There was a problem hiding this comment.
It cannot. ThinVec does not implement IntoIterator. &ThinVec also does not, leaving only this approach.
There was a problem hiding this comment.
Oh right -- field.attrs.deref() should work then, or just leave as is.
pnkfelix
commented
Jan 5, 2017
@rfcbot concern struct-fields-in-patterns Is the proposed extension intended to cover fields in patterns as well? I cannot tell from skimming the code, and in any case I do not see any test coverage of fields in patterns. By that "fields in patterns" mean the following: structS{x:i32,y:i32}fnmain(){let s = S{x:10,y:20};match s {S{x:11,// this is a field in a pattern ...y: the_y // ... and this is a field in a binding pattern} => println!("the_y: {}", the_y),S{x: the_x,y:20} => println!("the_x: {}", the_x),S{ .. } => {}}}(and of course such patterns can also be used with |
nikomatsakis
commented
Jan 5, 2017
@pnkfelix good question! Seems like the answer should be "yes, those ought to be supported too", right? I guess I expect fields to be configurable at all places where fields appear (definition, struct literal, patterns) |
Ralith
commented
Jan 6, 2017
@pnkfelix That was not the intention, but I agree that it makes sense. I'm working on implementing it now. Getting |
There was a problem hiding this comment.
I believe you could derive HasAttrs for Spanned<ast::FieldPat>, remove configure_struct_pat_field, and gated feature check in this closure.
There was a problem hiding this comment.
I think this would be easier to use as configure_pat.
There was a problem hiding this comment.
It's more efficient and idiomatic to do the following here:
pattern.map(|mut pattern| {// ...
pattern
})(if you did configure_pat instead of configure_pat_kind, the .map would be there)
There was a problem hiding this comment.
Same here, also would be solved by having configure_pat.
jseyfried
left a comment
There was a problem hiding this comment.
@Ralith thanks!
Could you add a feature gate test?
Other than that, r=me pending #38814 (comment).
There was a problem hiding this comment.
Perhaps we could refactor out this gated feature check (and the one below) into self.visit_struct_field_attrs() (c.f. self.visit_expr_attrs()) and then refactor away self.configure_struct_expr_field() and self.configure_struct_pat_field()?
There was a problem hiding this comment.
nit: _ => {} is more idiomatic (or if let)
jseyfried
commented
Jan 11, 2017
This PR is blocked on #38814 (comment). |
pnkfelix
commented
Jan 11, 2017
@rfcbot resolved struct-fields-in-patterns |
pnkfelix
commented
Jan 11, 2017
@rfcbot reviewed |
jseyfried
commented
Jan 12, 2017
@bors r+ |
bors
commented
Jan 12, 2017
📌 Commit 7972c19 has been approved by |
bors
commented
Jan 12, 2017
⌛ Testing commit 7972c19 with merge 78ccca8... |
bors
commented
Jan 12, 2017
💔 Test failed - status-travis |
alexcrichton
commented
Jan 12, 2017
via email
| … On Thu, Jan 12, 2017 at 10:31 AM, bors ***@***.***> wrote:
💔 Test failed - status-travis
<https://travis-ci.org/rust-lang/rust/builds/191366421>
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#38814 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAD95A9rlDn0bPkS9WXIyx2_6agW51Wtks5rRnGbgaJpZM4LaSVF>
.
|
bors
commented
Jan 12, 2017
syntax: enable attributes and cfg on struct fields
This enables conditional compilation of field initializers in a struct literal, simplifying construction of structs whose fields are themselves conditionally present. For example, the intializer for the constant in the following becomes legal, and has the intuitive effect:
```rust
struct Foo {
#[cfg(unix)]
bar: (),
}
const FOO: Foo = Foo {
#[cfg(unix)]
bar: (),
};
```
It's not clear to me whether this calls for the full RFC process, but the implementation was simple enough that I figured I'd begin the conversation with code.bors
commented
Jan 12, 2017
☀️ Test successful - status-appveyor, status-travis |
colin-kiegel
commented
Apr 7, 2017
I couldn't find a tracking issue to stabilise this. How is the procedure for stabilisation? This would be really nice to have! :-) |
mbrubeck
commented
May 1, 2017
Submitted #41681 as a tracking issue for stability. |
This enables conditional compilation of field initializers in a struct literal, simplifying construction of structs whose fields are themselves conditionally present. For example, the intializer for the constant in the following becomes legal, and has the intuitive effect:
It's not clear to me whether this calls for the full RFC process, but the implementation was simple enough that I figured I'd begin the conversation with code.