- Notifications
You must be signed in to change notification settings - Fork 14.1k
Fix parsing logic in proc_macro::quote #148209
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
base: main
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
114e740 to 02f04b0 Compare This comment has been minimized.
This comment has been minimized.
02f04b0 to 0e341e2 Compare proc_macro::quoteproc_macro::quote | rustbot has assigned @petrochenkov. Use |
| r? @tgross35 |
|
|
proc_macro::quoteproc_macro::quote 8dc03cd to 36c13e9 Compare This comment has been minimized.
This comment has been minimized.
| David knows this much better than I do r? @dtolnay |
| @bors r+ |
| @moatom: 🔑 Insufficient privileges: Not in reviewers |
| @tgross35 thx. I wanted to restart CI just in case because this PR compiles locally. |
| I restarted it. For future reference, you can either force push something or close+reopen to restart CI (bors doesn’t interact with PR CI) |
36c13e9 to 900f0e2 Compare This comment has been minimized.
This comment has been minimized.
| @dtolnay I've resolved all of the problems. Could you please confirm when you have a moment? |
library/proc_macro/src/quote.rs Outdated
| match tree { | ||
| TokenTree::Group(tt) => { | ||
| TokenTree::Group(ref tt) => { | ||
| // Handles repetition by expanding `$( CONTENTS ) SEP_OPT *` to `{ REP_EXPANDED }`. |
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 repetition syntax is supposed to be $(CONTENTS) SEP *, like inside macro_rules. Not $[CONTENTS] SEP * and not ${CONTENTS} SEP *, and especially not $ CONTENTS SEP * with Delimiter::None. Please add tests for these.
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.
Please add tests for these.
I think it is impossible to prepare it for Delimiter::None.
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.
It can be tested by calling quote! from a macro_rules macro.
macro_rules! do_quote { ($dollar:tt $content:expr) => { proc_macro::quote!($dollar $content *) }; } let arr = [0; 2]; eprintln!("{}", do_quote!($ f!($arr)));On current nightly this nonsensically prints f! (0i32) f! (0i32).
library/proc_macro/src/quote.rs Outdated
| ]); | ||
| | ||
| minimal_quote!((@ TokenTree::Group(Group::new(Delimiter::Brace, rep_expanded)))).to_tokens(&mut tokens); | ||
| consume_dollar_group_sep_star(contents.clone(), &mut iter) |
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.
| consume_dollar_group_sep_star(contents.clone(), &mut iter) | |
| consume_dollar_group_sep_star(contents, &mut iter) |
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.
This has not been addressed. You made a change on the same line but it is not the suggested one. There is no reason to clone the value of tt.stream(), which is already an owned TokenStream.
| @dtolnay |
| | ||
| fn main() { | ||
| let arr = [1, 2, 3]; | ||
| let _ = quote! { ${$arr}* }; //~ ERROR the trait bound `[{integer}; 3]: ToTokens` is not satisfied [E0277] |
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.
| let _ = quote! { ${$arr}* }; //~ ERROR the trait bound `[{integer}; 3]: ToTokens` is not satisfied [E0277] | |
| let _ = quote! { $[$arr]* }; //~ ERROR the trait bound `[{integer}; 3]: ToTokens` is not satisfied [E0277] |
library/proc_macro/src/quote.rs Outdated
| match &tree { | ||
| TokenTree::Punct(tt) if tt.as_char() == '$' => { | ||
| if let Some(TokenTree::Ident(id)) = iter.peek() { | ||
| while let Some(ref tree) = iter.next() { |
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.
This is going to miss adjacent metavariables. For example this works:
let a = ['a']; let b = ['b']; eprintln!("{}", proc_macro::quote!($($a . $b)*));but this does not:
eprintln!("{}", proc_macro::quote!($($a $b)*));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.
[note] This was caused by an extra iter.next();: b2ae84c#diff-6ebaa6be050b7cea1d542579c11cde3cba0942a3417c53298001f9db02a8d06dL407
| } | ||
| | ||
| /// Consume a `$( CONTENTS ) SEP *` accordingly. It handles repetition by expanding `$( CONTENTS ) SEP *` to `{ REP_EXPANDED }`. | ||
| fn consume_dollar_group_sep_star( |
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.
This is accepting too many tokens in the "separator" in some cases.
let arr = ['a', 'b']; eprintln!("{}", proc_macro::quote!($($arr) $$ x .. false [] *));'a' $x .. false [] 'b'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.
I introduced is_valid_sep_prefix to resolve this, but there might be a more elegant solution.
abc4763 to ae0617b Compare | r? @dtolnay |
| Requested reviewer is already assigned to this pull request. Please choose another assignee. |
This comment has been minimized.
This comment has been minimized.
ae0617b to 93fd70a Compare This comment has been minimized.
This comment has been minimized.
88f4fbe to 3613f22 Compare 3613f22 to 62534aa Compare | This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
I've now rebased and squashed the commits for smoother communication. |
| consume_dollar_group_sep_star(tt.stream(), &mut iter).to_tokens(&mut tokens); | ||
| continue; | ||
| } | ||
| TokenTree::Group(_) => { |
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.
output_dollar_group_sep seems available here too.
Fix #140238