-
Notifications
You must be signed in to change notification settings - Fork 897
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
Reformatting inside of a proc macro call site can change program behavior #3434
Comments
Hmm, so are you using |
No not that - sorry for being unclear! So let's say I have this program that uses my procedural macro fn main () {
let before_rustfmt = my_macro! { <div>
Hello</div>
}.to_string();
} Now let's say I run fn main () {
let after_rustfmt = my_macro! { <div>
Hello</div>
}.to_string();
} Notice that hello has been moved. If For example. Example one might generate this:
While example two might generate
So, in short, if Let me know if I failed to communicate the problem! |
Thank you for your clarification. One way is to extend // Skip formatting everything whose name is `html`
#![rustfmt::skip(html)]
// Or make it explicit that you only want to skip macro calls
#![rustfmt::skip::macro(html)] |
Thanks you for the visual example! I really like How complex would that be implementation wise? |
I don't think implementing this takes that much time, should be a nice weekend project :) |
I see that a PR implementing the skip functionality has been merged, so is this fixed? |
yes this is fixed, sorry the issue got left behind opened |
No, rustfmt does not perform name resolution, so the information from a macro definition is not visible to rustfmt. |
|
Background
I have a procedural macro that parses html.
It looks like
rustfmt
will format the tokens inside of my macroFor example:
The word
Hello
will get moved to the right a bit if I runrustfmt
.Problem
This is a problem because I'm using Spans to generate different code based on the locations of different tokens. So running
rustfmt
changes how the final program behaves.Potential Solution
Don't reformat the tokens inside of a procedural macro call site by default since it can change the behavior of a program
No idea how difficult or easy any of this is so just trying to detail my experience here. Happy to answer any questions or provide any missing context!
The text was updated successfully, but these errors were encountered: