-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 using $:literal containing integer to index into a tuple #91166
Conversation
r? @cjgillot (rust-highfive has picked a reviewer for you, use r? to override) |
The implementation seems fine. |
Nominated for lang team discussion. @dtolnay What is this going to do for the ol' fn main() {
let x = (('x',),);
let _ = m!(x.0.0);
} (Where |
This PR keeps the current behavior for |
There's a definitive method to determine whether something interpolated should parse or not - checking how it would work in token-based expansion model (aka "how it would work in proc macro output"), as opposed to AST-based model that is currently used for implementing
Do we want Also see #67062 for the current issues with parsing |
I'm not sure I agree this change would need to be bundled together with eliminating Delimiter::None wrappers around ((…) . $member:ident $($rest:tt)*) => {…};
((…) . $member:literal $($rest:tt)*) => {…}; It doesn't seem great to have to disrupt proc macros in order for macro_rules to get this improvement. |
We had a long conversation about this at the last lang team meeting. My takeaway was that we were open for some kind of change around this area, but weren't sure what form that should be. To attempt to summarize some of the things that came up:
|
Any other matchers are meaningful and must keep their meaning (or at least grouping) when inserted into other token streams. We conservatively emit the |
The most important part to realize is that non-terminal tokens do not exist during parsing. Imagine that we pass pass all tokens through a proc macro before parsing, proc macros do not support AST pieces by design, then all interpolated tokens are turned into their corresponding token streams, possibly including |
I don't see any difference between If they are multi-token then they surely be guarded by Right now we always add the brackets around all non- In theory we can omit the brackets from all single-token cases. Some hybrid schemes are also possible - we omit unnecessary |
See #91166 (comment) on the difference between "unstructured"
No opinion on this, I don't see anything harmful it it except for generally expanding the language surface. |
A As long as nonterminal tokens still exist in the old parser implementation, I'd like to make them behave consistently with the above treatment of |
☔ The latest upstream changes (presumably #93048) made this pull request unmergeable. Please resolve the merge conflicts. |
Currently fails to build: error: unexpected token: `0` --> src/test/ui/macros/macro-interpolation.rs:19:16 | LL | $var . $field | ^^^^^^ ... LL | let _ = field!(tuple.0); | --------------- in this macro invocation | = note: this error originates in the macro `field` (in Nightly builds, run with -Z macro-backtrace for more info) error: macro expansion ignores token `0` and any following --> src/test/ui/macros/macro-interpolation.rs:19:16 | LL | $var . $field | ^^^^^^ ... LL | let _ = field!(tuple.0); | --------------- caused by the macro expansion here | = note: the usage of `field!` is likely invalid in expression context error: aborting due to 2 previous errors
|
Gonna mark this as blocked on at least #92392 to put $:literal on a path to using a single token always on the proc macro side. Personally I'd be fine landing this one first but I respect that folks want some guarantee that the macro_rules behavior and proc macro behavior on literals will be able to stay in sync. |
I was able to work around this not working by duplicating all my macro input tokens: (($expr:expr) ($dot:tt $literal:tt $($rest:tt)*) (. $unusable_literal:literal $($rest:tt)*)) => {
$expr $dot $literal
}; (The full macro: https://github.com/dtolnay/anyhow/blob/1.0.53/src/ensure.rs#L310) #92392 has been closed so I'll close this as well. I still think this should work but for now I'm unblocked so someone else will need to drive this. :) |
Example of the failure prior to this PR: