-
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
RFC: Allow all literals in attributes #1559
Conversation
Unresolved questions:
|
@tsion: "All literals" are literals as are accepted by Rust today. That is, literals as defined in the reference and by the AST. This includes byte strings, floating point, etc. The range of these literals shouldn't differ from those in Rust. Parsing literals in attributes shouldn't differ from parsing literals elsewhere. |
I'm not very familiar with the current AST, but does 'all literals' include structs, slices, and closures? |
No. |
See the AST definition for what's defined as a literal. |
Just for the sake of posterity, the current AST definition of "literal" is this: pub enum LitKind {
Str(InternedString, StrStyle),
ByteStr(Rc<Vec<u8>>),
Byte(u8),
Char(char),
Int(u64, LitIntType),
Float(InternedString, FloatTy),
FloatUnsuffixed(InternedString),
Bool(bool),
} |
In the past I've heard musings that the alternative here, token trees, is a popular choice of how to extend the attribute grammar. I personally feel that so long as we're principled about what we do we should be fine. Some thoughts and questions I have, however, are:
From a "how I personally feel" perspective, I don't think that the motivation is particularly compelling enough to push through a syntactical change like this. I've occasionally felt from time to time it'd be nice to drop the quotes or something like that but never enough to want to actually change the language grammar. As an aside, this might be an awesome test case for @nikomatsakis's rustypop and put RFC 1331 into action. |
@alexcrichton Couldn't |
I would expect EDIT: and not equivalent to |
@eddyb yeah that's one possible route, I may personally prefer to err on the side of being strict, but either way seems ok to me |
I don’t see any other way to make “all” literals work in a meaningfully intuitive fashion than coercing those literals into strings (e.g. using literals’ i.e. #[cfg(foo=32)] would be exactly the same thing to the compiler as #[cfg(foo="32")]. |
I would prefer that attributes just take token trees, rather than have this key: value stuff going on. |
I've made some changes to the RFC. Primarily, I extended the grammar to allow crate attributes (the '!'), defined exactly what "literals" are with references, and included a couple of examples with different kinds of literals. I also added one more alternative: allow only unsuffixed literals.
The literals allowed by an attribute are totally up to that attribute's author. As other have noted, I would expect that the
I agree that the motivation as stated in the RFC is a bit one-footed. I think this is a case where the motivation for such a change will only become apparent in the future. In particular, I think that as attributes and syntax extensions become more mainstream, the need for flexibility in attributes will become apparent. Without this change, syntax extensions will simply narrow their interfaces to use only what's allowed. It's hard to imagine what will be done with a tool that doesn't exist. Anecdotally, I'm currently writing a library where attributes serve as one of its core interfaces. I have become increasingly frustrated that I can't write attributes of the form As far as simply allowing any token goes, i.e, the token tree alternative, I'm okay with this. My only concern is that it removes all structure from attributes, which appears to be one of the original goals as alluded to in the reference. |
@SergioBenitez yeah I'd definitely believe that once we start pushing harder on doing "everything with attributes" kinda that we'll want more flexibility in the definition syntax. Thanks for the examples :) |
#1566 proposes (somewhat implicitly) allowing all token trees in attribute args. I intend that libmacro will provide functionallity to make it easy to go from the token tree representation to key/value sets. That will be in a forthcoming RFC, but is sketched in this blog post, see the section 'parsing helper functions`. Since I would like to put all our energy behind the new procedural macro system, I would prefer not to accept this RFC. I hope that #1566 addresses the use cases here, if not, we should try and make sure it does. |
I'm in favor of this. I think it's super weird that we specify things as strings in attributes when they are clearly numbers, such as |
cc @cgswords Nominating for lang meeting discussion. My feeling is that we want this, but it is better addressed by the more general token trees approach. |
🔔 This RFC is now entering its final comment period 🔔 |
A thought from the language team: even if we do want to go with the token tree solution in the long term, if this RFC looks like a benefit (which I personally think it is), then we can accept it as long as it is forwards-compatible (I think it is, but I will put some additional thought into it too). |
As @nrc suggests, I think that while I would long-term prefer token-trees, accepting a wider variety of literals seems like a good idea in the shorter term. I am mildly nervous about things like suffixed literals and floating point values, but I don't think we should exclude them from what is permitted. I imagine we will want to work out guidelines (as part of follow-up RFCs). For example, while |
I agree with both of the above comments. While, token trees may be the right solution in the long term (though I'm not sure of this myself), implementing this RFC now would bring greater flexibility in the meantime. Implementing this RFC should be forwards and mostly backwards compatible. As far as which literals to allow: my personal preference is to allow all unsuffixed literals but not suffixed literals. This includes floating point values. The reason I believe this is the right way to go is because the attribute itself can always constrain the value of literals to that of unsigned, signed, or some width, or range of values, as it prefers. |
Huzzah! The @rust-lang/lang team has decided to accept this RFC. |
Tracking issue: rust-lang/rust#34981 If you'd like to keep following the development of this feature, please subscribe to that issue, thanks! :) |
Implement RFC#1559: allow all literals in attributes Implemented rust-lang/rfcs#1559, tracked by rust-lang#34981.
Implement RFC#1559: allow all literals in attributes Implemented rust-lang/rfcs#1559, tracked by #34981.
…nkov Stabilize 'attr_literals' feature. RFC Issue: rust-lang/rfcs#1559 Tracking Issue: #34981 Reference PR: rust-lang/reference#388.
I'd like to allow all types of literals to appear in attributes at both top-level positions in lists and as values in k/v pairs. This would, for example, make the following syntax valid:
Rendered