-
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
Tracking issue for literal
fragment specifier (RFC 1576)
#35625
Comments
Is anyone working on implementation already? If not I can take a crack at it. |
I'll probably do it. |
Huh oh, we have an issue already: do we mean to include potentially minus-prefixed literals? If yes, we have a problem because 1. it means we have to return an Expr instead of a Lit, which means I'm not sure one will be able to use the bound fragment in a non-expr context (for example in a static array type?) and 2. it's no longer always a single-TT, so the benefits in terms of future-proofing are a bit less good. If we don't... well, we loose expressivity. I have no idea what is more intuitive though... Would you, as a Rust user, expect |
@LeoTestard good point. I think I would have expected it, yeah...but I also agree with the complications. |
Can't we upgrade Lit to support negative literals? |
@LeoTestard I'd expect "literal" (or a future integer-literal specifier) to include Perhaps that expectation doesn't match reality, though. For instance, based on parsing rules for non-literals, I'd expect So, if handling both |
Unary operators, including minus, are processed purely by the parser at the moment, no lexer involved. So their treatment is whitespace agnostic. |
I agree with @petrochenkov. |
It'd be reasonable to support it in fragments as well. |
Another thing that won't match is a macro call, like I dunno, maybe this is not such a good idea after all. Re-reading the RFC in a more skeptical light, I felt like the "Motivation" section could use some more concrete use cases. Maybe this is just failure of imagination on my part, though! I think the likelihood of this feature attracting people who really want constant expressions should be acknowledged as a drawback, too. |
That's fine IMO. Personally I'd like it to match only explicit literals in code to distinguish, for example, between literal and identifier and produce different code for them. Right now this is not possible because all of |
Hi All, I have implemented this feature in my fork of Rust over 1.25.0, under this branch: https://github.com/da-x/rust/tree/literal-fragment Following a quick review by a rustc mentor I may have the free time to provide a full pull request for this. |
BTW |
Macros: Add a 'literal' fragment specifier See: #35625 ```rust macro_rules! test_literal { ($l:literal) => { println!("literal: {}", $l); }; ($e:expr) => { println!("expr: {}", $e); }; } fn main() { let a = 1; test_literal!(a); test_literal!(2); test_literal!(-3); } ``` Output: ``` expr: 1 literal: 2 literal: -3 ``` ToDo: * [x] Feature gate * [x] Basic tests * [x] Tests for range patterns * [x] Tests for attributes * [x] Documentation * [x] Fix for `true`/`false` * [x] Fix for negative number literals
The implementation that I worked on was merged, yay! |
Is there anything stopping an FCP for this? |
@clarcharr It's currently merged in unstable; do you mean an FCP to declare it stable? |
Yes, an FCP to stabilise. Should have clarified. |
@jendrikw Agreed, this should be allowed just like passing any other node types. |
Is there anything else preventing this from being stabilized? The issue above has been fixed. |
This has baked for some time now and I believe it should be ready for stabilization. @rfcbot merge The feature gate test is here: Unstable book: Tests defined here: |
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. |
Reading through the tests, I see that While that makes sense to me, I remember hearing that it wasn't, but was instead a negation operator and a literal. Am I misremembering? |
I agree that it makes sense. I would say that even if |
I think that the nuance with |
@clarcharr there are tests currently implemented that verify that |
I worded that wrong; I meant that only having a |
@scottmcm for procedural macros the input as lexed by the compiler is |
Are literals generated by the compiler counted as true literals? macro_rules! bind {
($type:ty) => {
bind!($type, concat!(stringify!($type), ".html"));
};
($type:ty, $path:literal) => {
println!("Page: {:?}", stringify!($type));
println!("Path: {}", $path);
};
} This fails to match, as the expansion, I guess, happens after the selection. I just naively assumed that would have worked. |
macro_rules! m {
($first:tt $second:tt $third:tt) => {
println!(stringify!($third));
println!(stringify!($second));
println!(stringify!($first));
};
}
fn main() {
m! {
concat!(stringify!($type), ".html")
}
} If you were to use it in expression position, it would be interpreted as an invocation of |
I can understand semantically why it doesn't make sense, I just thought it would have, but that being said, I already changed it to It just made more sense to me as a literal, because according to the compiler, it could generate a literal from it. |
This is true, but there's more to it! fn main() {
println!("{}", env!("PATH")); // OK
// println!("{}", env!(10)); // ERROR expected string literal
println!("{}", env!(concat!("PATH"))); // OK
} Fortunately (or not), this currently cannot be done in user-defined macros. |
I actually just realised that I can't change it to It doesn't look pretty. :( Ninja edit: I've reverted it back to the |
🔔 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. |
@da-x Since you implemented this, would you be willing to write up the stabilization PR? |
@Centril sure |
…etrochenkov Stabilize macro_literal_matcher This followed FCP in rust-lang#35625. Closes rust-lang#35625
This appears to be missing from the Rust reference |
Tracking issue for rust-lang/rfcs#1576.
The text was updated successfully, but these errors were encountered: