-
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
Add a literal
fragment specifier to macro_rules!
.
#1576
Conversation
5fa69f4
to
5c67bc0
Compare
|
||
# Drawbacks | ||
|
||
This is a bit inconsistent since it does not include all literal constants (in this context, I mean ‶literals that do not require any computation″): indeed it does not include compound literals, for example struct literals `Foo { x: some_literal, y: some_literal }` or arrays `[some_literal ; N]`, where `some_literal` can itself be a compound literal. See in alternatives why this is disallowed. |
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.
The Design section should be expanded to specify what is the set of literal constants that you are adding here.
(I.e. the fact that you are adding the clarifying aside "in this context, I mean ..." in the drawbacks section is a bad sign -- the design section should have enough information for someone to deduce what you mean by "literal".)
8d9a531
to
7e55f54
Compare
I've been missing this, for example in a macro that uses consecutive integer literals like |
Aye; there's an important alternative not mentioned in the RFC: use The problem I can see with this proposal is that it doesn't really simplify existing macros, nor open up new ones. The lack of backtracking means that even if this matcher is more specific than This will probably give better error messages on matching failures, but nothing else I can think of. No, wait, it does have one advantage over |
@DanielKeep The weight should be that big. And backtracking is coming with rust-lang/rust#33840. The better error messages are a big gain. :/ |
(Edited to mention the possibility of using |
cc @jseyfried in case you have an opinion on this |
also cc @cswords |
I'm in favour of this, though slightly (and vaguely) worried about the implementation details |
I'm also in favour of this. I don't think implementation will be a problem, but I'm not too familiar with the details of how fragments are implemented. n.b. the reparse trick is no longer needed, so that's no longer an advantage over |
Hear ye, hear ye! This RFC is now entering final comment period. We discussed this at the most recent @rust-lang/lang meeting and decided that we are currently inclined to accept (of course no final decision has been reached). To summarize the discussion so far:
|
I like this RFC. Would it make sense to also have more specific versions for specific kinds of literals? Several times, I've wanted to have a macro accept a string literal specifically. |
It's actually very simple, it just calls the parse_whatever() functions from libsyntax::parse::parser.
And the future-proofing is less good too. Using a more specific matcher restricts the FIRST sets, which is good.
I guess it would make sense, though the gain in terms of error messages is small. I'm not sure if it's possible though, since I think the libsyntax parser has a single parsing function for all literal types. It can surely be refactored and split into several functions, but I'm not sure it's worth it. |
Yeah, I wondered the same. Could be useful, but I guess there is value in "any literal" as well, and it's something we could easily add later. |
@nrc Absolutely! To clarify, I'd love to see the proposed RFC accepted, and I don't want to bikeshed it. I'd also love to see specifiers for specific types of literals, which could either go in this RFC or a future RFC. |
Huzzah! The @rust-lang/lang team has decided to accept this RFC. |
Tracking issue: rust-lang/rust#35625 If you'd like to keep following the development of this feature, please subscribe to that issue, thanks! :) |
Dear all, the implementation is now in |
Add a
literal
fragment specifier formacro_rules!
patterns that matches literal constants.Rendered