-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Allow all literals in attributes (Tracking Issue for RFC #1559) #34981
Comments
@cgswords Should this be assigned to you? |
Just checking, will this apply to align and packed (when they are finally implemented) so that I can do |
@retep998 That is the intention AFAIK, yes. |
I've started implementing this and have made headway. Should have a working implementation soon. |
Implement RFC#1559: allow all literals in attributes Implemented rust-lang/rfcs#1559, tracked by rust-lang#34981.
closes #856 See also rust-lang/rust#34981
Any news about stabilisation of that feature? |
I would also like to know if there are any thoughts on stabilisation, this would have been great to use with Macros 1.1, but I guess it's too late for that. (also, for searchability this is the |
I think I've found a bug (or at least a big limitation) in the current implementation, negative integer and float literals are not supported:
|
There are no negative literals (only negation expressions). |
😞 damn, I sort of expected something like that. It is a limitation for procedural (derive) macros though, any negative numbers will still need to be passed through as a string and parsed manually. I guess the next step would be to allow constant expressions in attributes, but that sounds like a much bigger step than this one. |
The plan ha been full token trees, for a while, really, so you'd just parse it as an expression. |
Will this allow type names in attributes? |
I've been looking at implementing this properly. My belief is that we intend the grammar for attributes to be one of:
My (ill-informed) read of libsyntax is that we parse all attributes into the Attribute struct. After parsing their contents correctly, everything after the initial path gets unparsed into a While validating that the attributes are grammatical (as part of constructing an enum MetaItemKind {
Word,
List(Vec<NestedMetaItem>),
NameValue(Lit),
}
enum NestedMetaItemKind {
MetaItem(MetaItem),
Literal(Lit)
} It seems to me the correct thing to do is eliminated NestedMetaItem(Kind) and rewrite MetaItemKind as: pub enum MetaItemKind {
Word,
List(Vec<Expr>),
NameValue(Expr),
} Then change the code in libsyntax/attr.rs to correctly implement the conversion to/from tokens for MetaItemKind. Corrections appreciated. |
I believe the intent was to have token streams inside at least cc @jseyfried @nrc |
Yes, expanding it to an arbitrary token stream seems reasonable. |
Anything I could do to help stabilise the current literals? |
* Remove reliance on unstable feature int_to_from_bytes * Add a fallback method until feature becomes stable in Rust 1.29 * See rust-lang/rust#51835 * Remove reliance on experimental feature attr_literals * Only used for `structopt`, but it provides `raw()` * See rust-lang/rust#34981 * Make tests run again
Stabilization ReportOverviewThe
The following tests in
StatusThe To date, no issues or bugs have been opened about the feature despite its fairly widespread use. ProposalI propose that we stabilize this feature in its entirety, allowing unsuffixed literals to be used in attributes. This change is backwards compatible. This change is also forwards compatible with the proposed change to make attribute inputs be an arbitrary To this end, I submit #53044 and rust-lang/reference#388, stabilizing the feature. |
@rfcbot fcp merge |
Team member @cramertj 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. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. |
Have there been any recent changes here that might be responsible for #53298?
|
…nkov Stabilize 'attr_literals' feature. RFC Issue: rust-lang/rfcs#1559 Tracking Issue: #34981 Reference PR: rust-lang/reference#388.
The feature has been stabilized! Looks like this issue can finally be closed. |
Hmm, it's not clear: are all those literals just a syntax sugar and they are converted to strings internally (as it used to be without this RFC) or attribute "consumers" will expect integer literals too? |
@pravic The consumers can see the real literal tokens, and in the |
Implement basic input validation for built-in attributes + Stabilize `unrestricted_attribute_tokens` Based on #57272 --- In accordance with the plan in https://internals.rust-lang.org/t/unrestricted-attribute-tokens-feature-status/8561: - The correct top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is enforced for built-in attributes. - For non-built-in non-macro attributes: - The key-value form is restricted to bare minimum required to support what we support on stable - unsuffixed literals (#34981). - Arbitrary token streams in remaining forms (`#[attr(token_stream)]`, `#[attr{token_stream}]`, `#[attr[token_stream]]` ) are now allowed without a feature gate, like in macro attributes. This will close #55208 once completed. Need to go through crater first.
Stabilize `unrestricted_attribute_tokens` In accordance with a plan described in https://internals.rust-lang.org/t/unrestricted-attribute-tokens-feature-status/8561/3. Delimited non-macro non-builtin attributes now support the same syntax as macro attributes: ``` PATH PATH `(` TOKEN_STREAM `)` PATH `[` TOKEN_STREAM `]` PATH `{` TOKEN_STREAM `}` ``` Such attributes mostly serve as inert proc macro helpers or tool attributes. To some extent these attributes are de-facto stable due to a hole in feature gate checking (feature gating is done too late - after macro expansion.) So if macro *removes* such helper attributes during expansion (and it must remove them, unless it's a derive macro), then the code will work on stable. Key-value non-macro non-builtin attributes are now restricted to bare minimum required to support what we support on stable - unsuffixed literals (#34981). ``` PATH `=` LITERAL ``` (Key-value macro attributes are not supported at all right now.) Crater run in #57321 found no regressions for this change. There are multiple possible ways to extend key-value attributes (#57321 (comment)), but I'd expect an RFC for that and it's not a pressing enough issue to block stabilization of delimited attributes. Built-in attributes are still restricted to the "classic" meta-item syntax, nothing changes here. #57321 goes further and adds some additional restrictions (more consistent input checking) to built-in attributes. Closes #55208
Tracking issue for rust-lang/rfcs#1559 -- allow all literals in attributes.
The text was updated successfully, but these errors were encountered: