-
Notifications
You must be signed in to change notification settings - Fork 899
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
Format stable macro_rules #2393
Conversation
Also there are various edge cases still not covered in either macro (such as inserting |
@topecongiro Any feedback on this please? Looks like it already started getting conflicts, I'd like to resolve issues if any before it becomes unmergeable. |
Thank you for your contribution, and sorry for the late review. The code LGTM, but I am a bit afraid that we may lose comments between macro definitions. Would you make sure that your code works with comments please? For example, macro_rules! write_html {
// 1
($w:expr, ) => (());
// 2
($w:expr, $e:tt) => (write!($w, "{}", $e));
// 3
($w:expr, $tag:ident [ $($inner:tt)* ] $($rest:tt)*) => {{
write!($w, "<{}>", stringify!($tag));
write_html!($w, $($inner)*);
write!($w, "</{}>", stringify!($tag));
write_html!($w, $($rest)*);
}};
} |
Sure, I'll add some! |
Actually current |
You're right, comments outside of branches do get lost. I'll try to find how to fix that. |
@topecongiro Ok, honestly, after looking at examples for other syntax, I still don't fully understand how rustfmt deals with comments and what I should do here to preserve them. Any tips on where to start? |
Since AST does not include comments, rustfmt needs a data structure called Currently |
3a1a00f
to
a221757
Compare
@topecongiro Okay... this was somewhat hard to figure out due to various issues along the way, but it seems to work correctly and preserve comments now. The only issue I still have is this spurious
Adding macro_rules! configuration_option_enum{
($e:ident: $( $x:ident ),+ $(,)*) => {
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum $e {
$( $x ),+
}
impl_enum_serialize_and_deserialize!($e, $( $x ),+);
}
} which was expanded into
I guess the reason for failure is missing repetition support from your other branch so Rust can't parse UPD Oh wait, it doesn't fail on master because it doesn't attempt to parse macro body... I still don't get why it panics though instead of just returning |
@RReverser AFAIK rustc calls |
I think using |
Thank you! FWIW one hazard I ran into was that I could add However, in future, I think, it might be worth to change |
Anyway, the panic aside, do you think this PR is acceptable in the current form? |
@nrc @topecongiro Ping? |
I am sorry for the late review. I think the code looks good overall. It would be great if you could add more tests to make sure that nothing is going wrong. |
Sure. What would you like to see covered? So far I covered multiple branches including empty match, empty return, comments in various locations and some repetition matching. Can't think of some other special cases to cover off the top of my head (apart from bunch of those that are just not yet implemented for neither version of macro syntax) |
Yeah it will be great if those cases are covered in tests 😉 |
Oh, never mind, you already added tests for those cases in macro_rules.rs. I somehow missed those files... I am sorry for bothering you. |
Would you please rebase agsinst master, and remove or comment out tests for macro def with repeat and add FIXME so that we can merge this PR? |
Reformat codebase with current version to pass self_tests (formats macros without repetitions).
a221757
to
8691c64
Compare
Ok rebased and ran |
Thank you! |
I took an existing logic for Macros 2.0 and adapted for "legacy" (stable)
macro_rules
syntax. At this point it seems to produce quite consistent and expected results.Admittedly the code is quite hacky, but I guess that's what one should expect when attempting to format macros. If not, I'm open to suggestions :)
Issue: #8