Open mcheshkov opened 5 years ago
Parsing macro is possible only when the macro has valid syntax (e.g. println!
). Most macros, including cfg_if!
, contains invalid syntax in it (e.g. if #[cfg(unix)]
), so rustfmt just ignores it.
FYI this means that we can't use rustfmt
effectively in rust-lang-nursery/{stdsimd, packed_simd}
nor in rust-lang/libc
. This appears to be hard to solve.
This seems to be not fixed in 1.3.0 😞
@topecongiro - I believe I've got a fix for the cfg_if support (at least it's working my local env 😄)
Will open a PR shortly for review
This issue also exists with dirmod
users. Modules are entirely generated by the macro, so it is impossible for rustfmt
to detect them with heuristics like cfg_if!
did.
A useful alternative would be to allow a flag like -r
that formats all files in a directory recursively, no matter explicitly included or not, similar to the suggestion in #2426.
Parsing macro is possible only when the macro has valid syntax (e.g.
println!
).
@topecongiro Does this work for modules? This module in os_str_bytes isn't formatted, but the macro uses valid syntax:
I’ve run into this bug.
Parsing macro is possible only when the macro has valid syntax (e.g.
println!
). Most macros, includingcfg_if!
, contains invalid syntax in it (e.g.if #[cfg(unix)]
), so rustfmt just ignores it.
That explains why rustfmt cannot easily format the tokens in a macro invocation. However, couldn’t it expand macros when looking for mod
items in order to find which other files to format?
That'd be too costly for a formatter that's supposed to only process the lexical level of the crate. In particular, procedural macros require compilation. If the procedural macro fails to compile, that would result in an error, which breaks backward compatibility of rustfmt.
Right, drawing the line at proc macros makes sense to me. Would same-crate macro_rules
also be too costly?
Just ran into this bug myself. gdbstub
uses a declarative macro to define modules for different protocol packets, and I recently noticed that the CI wasn't enforcing formatting in them.
Is there any suggested workaround / fix on the horizon? It seems I might need to update the CI to manually invoke rustfmt
on each file manually...
Currently running into this problem as well.
While it does make sense not to parse every macro like that, what if there was a #[rustfmt::follow_modules]
attribute or something similar? It would go on macros where you want rustfmt to parse and find modules.
#[rustfmt::follow_modules]
macro_rules! custom_macro {
($(mod $mod_name:ident;)*) => {
// do something here...
}
}
custom_macro! {
mod foo;
mod bar;
mod baz;
}
When I declare modules like this
rustfmt will check module, irrespective of condition.
However, when I declare them using cfg-if crate rustfmt doesn't check module.
Is is designed behavior? Or is macro parsing not implemented for now?
That's
rustfmt 1.0.1-nightly (be13559 2018-12-10)