-
Notifications
You must be signed in to change notification settings - Fork 896
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
Module declared inside macro is not checked #3253
Comments
Parsing macro is possible only when the macro has valid syntax (e.g. |
FYI this means that we can't use |
…base-dir, r=michaelwoerister Add stream_to_parser_with_base_dir This PR adds `stream_to_parser_with_base_dir`, which creates a parser from a token stream and a base directory. Context: I would like to parse `cfg_if!` macro and get a list of modules defined inside it from rustfmt so that rustfmt can format those modules (cc rust-lang/rustfmt#3253). To do so, I need to create a parser from `TokenStream` and set the directory of `Parser` to the same directory as the parent directory of a file which contains `cfg_if!` invocation. AFAIK there is no way to achieve this, and hence this PR. Alternatively, I could change the visibility of `Parser.directory` from `crate` to `pub` so that the value can be modified after initializing a parser. I don't have a preference over either approach (or others, as long as it works).
…base-dir, r=michaelwoerister Add stream_to_parser_with_base_dir This PR adds `stream_to_parser_with_base_dir`, which creates a parser from a token stream and a base directory. Context: I would like to parse `cfg_if!` macro and get a list of modules defined inside it from rustfmt so that rustfmt can format those modules (cc rust-lang/rustfmt#3253). To do so, I need to create a parser from `TokenStream` and set the directory of `Parser` to the same directory as the parent directory of a file which contains `cfg_if!` invocation. AFAIK there is no way to achieve this, and hence this PR. Alternatively, I could change the visibility of `Parser.directory` from `crate` to `pub` so that the value can be modified after initializing a parser. I don't have a preference over either approach (or others, as long as it works).
…base-dir, r=michaelwoerister Add stream_to_parser_with_base_dir This PR adds `stream_to_parser_with_base_dir`, which creates a parser from a token stream and a base directory. Context: I would like to parse `cfg_if!` macro and get a list of modules defined inside it from rustfmt so that rustfmt can format those modules (cc rust-lang/rustfmt#3253). To do so, I need to create a parser from `TokenStream` and set the directory of `Parser` to the same directory as the parent directory of a file which contains `cfg_if!` invocation. AFAIK there is no way to achieve this, and hence this PR. Alternatively, I could change the visibility of `Parser.directory` from `crate` to `pub` so that the value can be modified after initializing a parser. I don't have a preference over either approach (or others, as long as it works).
…base-dir, r=michaelwoerister Add stream_to_parser_with_base_dir This PR adds `stream_to_parser_with_base_dir`, which creates a parser from a token stream and a base directory. Context: I would like to parse `cfg_if!` macro and get a list of modules defined inside it from rustfmt so that rustfmt can format those modules (cc rust-lang/rustfmt#3253). To do so, I need to create a parser from `TokenStream` and set the directory of `Parser` to the same directory as the parent directory of a file which contains `cfg_if!` invocation. AFAIK there is no way to achieve this, and hence this PR. Alternatively, I could change the visibility of `Parser.directory` from `crate` to `pub` so that the value can be modified after initializing a parser. I don't have a preference over either approach (or others, as long as it works).
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 A useful alternative would be to allow a flag like |
@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.
That explains why rustfmt cannot easily format the tokens in a macro invocation. However, couldn’t it expand macros when looking for |
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 |
Just ran into this bug myself. Is there any suggested workaround / fix on the horizon? It seems I might need to update the CI to manually invoke |
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]
macro_rules! custom_macro {
($(mod $mod_name:ident;)*) => {
// do something here...
}
}
custom_macro! {
mod foo;
mod bar;
mod baz;
} |
cargo-fmt skips files because of rust-lang/rustfmt#3253
1854: Reformat everything r=rtzoeller a=SUPERCILEX Unfortunately, #1748 didn't do the trick because of rust-lang/rustfmt#3253. This PR fully enforces global formatting. Closes #770 Co-authored-by: Alex Saveau <[email protected]>
Would it be possible to mitigate this issue to add support for known/common macros like |
@GuillaumeGomez I believe we already supported resolving modules declared in Lines 176 to 179 in 777e25a
|
Seems to not work on sysinfo as can be seen in sysinfo. But I'm glad to see it's already (at least partially) supported. Do you need help for improving its support maybe? |
@GuillaumeGomez getting some help would be great. I took a stab at this back in #5341, but it hasn't been reviewed yet. If this fixes the issue I don't see why we couldn't move forward with that as a potential solution to improve the Maybe you could build off of what's there and add a test case or two that reflects the structure of the |
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)
The text was updated successfully, but these errors were encountered: