-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
#[cfg_attr] expanding to multiple attributes #2539
Conversation
Usually we wait a bit before the debate settles, but in this case this seems to me a very small change with obvious wins to ergonomics, for those it matters to, with few, if any, drawbacks. @rfcbot merge |
Team member @Centril has proposed to merge this. The next step is review by the rest of the tagged teams:
No concerns currently listed. Once a majority of reviewers approve (and none object), 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. |
Oh, cc @petrochenkov who asked me to open an RFC for this. |
I kind of wish the predicate was syntactically distinguished but I don't think its possible without introducing a novel syntax pattern that attributes don't have or being a breaking change (or having a weird exception for when there's only 1 attribute). |
@withoutboats I sorta agree here; but given the status quo, I think there's already an understanding that the second bit is the attribute-to-apply, so it is natural to assume that what follows also are attributes; therefore, there shouldn't be much new to learn. |
I wondered about |
I'd be okay with requiring square brackets there. Though if we do that, is |
First, I wanted the RFC to specify some kind of attribute syntax before stating that attributes can be put into a comma-separated list, because right now there's no official documentation on what the syntax is and whether it can be put into a list. Until very recently attributes accepted arbitrary token streams, so Right now the implemented syntax is
Everything from that list (with exception of key-value for now) can be passed to attribute proc macros without any feature gates. So, this RFC may want to specify what is implemented as supported syntax, or it may stay more conservative and only guarantee that token streams supported by attributes must never contain the comma token |
Second, |
Small or not, I don't get the push for insta-FCP? There's a question in design space about whether to support 0 attributes. The proposal clearly takes the stance that it should not. I would object to it as well, because it feels like a potential footgun. (you can write the condition, get distracted, come back and forget to add the rest). However, supporting 0 arguments would be useful for macros and codegen, and would seem to be consistent with many of other rust's decisions:
|
For the delimited The problematic case here would be an attribute such as To me, there are three ways that can be solved.
Personally, I think the attribute I wrote looks weird, so I'd rather go for option 2. As for also doing it for attribute syntax in general, that's a bigger change, but you are right that it's basically the same change. Heck, just changing the meaning of But if we're going to do that, we're going to have to cancel the FCP. |
@ExpHP This RFC gives no opinion on it. I considered on that point, and decided to punt. I think we should allow it, but issue a warning if it's found like that in non-macro-generated code. Although this means that |
@Havvy |
With the change to more precisely specify attribute syntax, I think this is ready to merge. |
Recent updates: Two changes have been made to this RFC today. The first outlined the restrictions on future attribute syntax that is a byproduct of this RFC. The second was a material change to allow zero attributes in the list and trailing commas. I will not be expanding this RFC to also affect general syntax attribute, but rather leave that to its own RFC. It has more drawbacks, and is scope creep. |
text/0000-cfg_attr-multiple-attrs.md
Outdated
* `cfg_attr(predicate, attr)` | ||
* `cfg_attr(predicate, attr_1, attr_2)` | ||
* `cfg_attr(predicate, attr,)` | ||
* `cfg_attr(preciate, attr_1, attr_2,)` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/preciate/predicate/
For the record, I am in favor of the general idea, and supportive of basically any of the various options that been proposed. Happy to let the thread reach a consensus on the minor points. I would mildly favor permitting zero attributes: it seems like the sort of thing that a code generator might want to be able to do. It seems like just adding more arguments to |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
text/0000-cfg_attr-multiple-attrs.md
Outdated
* `cfg_attr(predicate, attr_1, attr_2)` | ||
* `cfg_attr(predicate, attr,)` | ||
* `cfg_attr(predicate, attr_1, attr_2,)` | ||
* `cfg_attr(predicate)` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: this is better prohibited for uniformity.
Imagine any other syntax for the predicate/attrs separator, e.g. cfg_attr(predicate: attr1, attr2)
, then empty attribute list would look like cfg_attr(predicate:)
with cfg_attr(predicate)
being invalid, i.e. the first comma is not like the others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually go for zero-or-more instead of one-or-more in most places where we allow -or-more? so I'm not sure restricting this to one-or-more is the uniform thing to do here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean cfg_attr(predicate, /* no attrs */)
- OK, cfg_attr(predicate /* no attrs */)
- ERROR, i.e. zero-or-more exactly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh; I see now :)
I have no strong feelings here.
Are there any future compat hazards with allowing the omission of the ,
?
I don't think macros benefit from omitting ,
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I agree here. Allowing cfg_attr(p)
is like allowing both leading and trailing commas; we don't need it, and blocking it simplifies the grammar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Havvy can you make the restriction based on @petrochenkov's and @scottmcm's comments?
We can always relax in the future if we feel so inclined but for now we can be more conservative here until we have a good justification to do something else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
I added a commit that specifies that the emitted warning for |
The final comment period, with a disposition to merge, as per the review above, is now complete. |
Huzzah! This RFC has been merged! Tracking issue: rust-lang/rust#54881 |
|
||
## Warning When Zero Attributes | ||
|
||
This RFC allows `#[cfg_attr(predicate)]`. This is so that macros can generate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have been #[cfg_attr(predicate, )]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the RFC states that #[cfg_attr(predicate)]
is not permitted before this. A fix PR would be welcome.
🖼 Rendered
⏭ Tracking issue
📝Summary
#[cfg_attr(predicate, attr1, attr2, ...)]
expands to each attribute instead of only taking one attribute.💖 Thanks
To @Centril for a quick review of the RFC before publishing.
🗒 I'm working on implementing this right now.