rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
5.98k stars 878 forks source link

Module declared inside macro is not checked #3253

Open mcheshkov opened 5 years ago

mcheshkov commented 5 years ago

When I declare modules like this

#[cfg(unix)]
pub mod stub_hal;

rustfmt will check module, irrespective of condition.

However, when I declare them using cfg-if crate rustfmt doesn't check module.

cfg_if! {
    if #[cfg(unix)] {
        pub mod stub_hal;
    }
}

Is is designed behavior? Or is macro parsing not implemented for now?

That's rustfmt 1.0.1-nightly (be13559 2018-12-10)

topecongiro commented 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.

gnzlbg commented 5 years ago

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.

topecongiro commented 5 years ago

This seems to be not fixed in 1.3.0 😞

calebcartwright commented 5 years ago

@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

SOF3 commented 4 years ago

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.

dylni commented 4 years ago

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:

https://github.com/dylni/os_str_bytes/blob/96b3151bec06d0309f2676e835fb1ed065cb9a53/src/lib.rs#L165-L167

SimonSapin commented 3 years ago

I’ve run into this bug.

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.

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?

SOF3 commented 3 years ago

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.

SimonSapin commented 3 years ago

Right, drawing the line at proc macros makes sense to me. Would same-crate macro_rules also be too costly?

daniel5151 commented 3 years ago

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...

Purpzie commented 2 years ago

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;
}