Skip to content

"cfg-if"-included modules ignored if formatted after parsing a top-level macro in another file #4656

Closed
@smklein

Description

@smklein

Describe the bug

Rustfmt generally tries to follow cfg-if-conditional modules, which is great -- this allows parsing modules which are e.g. arch/OS specific. However, this logic operates incorrectly when parsed on the Nth file, for N > 1 where some Mth file (M < N) contains a top-level macro.

This is a bit quirky, and I'm not sure I'm using the best terminology, so I'll use an example:

To Reproduce

format_me_please.rs

// Any invalid formatting will do
pub fn hello   (    ) {                            }

lib.rs:

cfg_if! {
  if #[cfg(target_family = "unix")] {
    mod format_me_please;
  }
}

lib2.rs:

its_a_me_a_macro! {
  // Any contents; this comment will suffice
}

With this setup, the bug can be reproduced on command-line using rustfmt, though it is also possible to repro via cargo fmt:

// This works as expected; lib.rs + format_me_please.rs are formatted.
// The "-v" lets us see that the `format_me_please.rs` file is detected + fixed.
$ rustfmt -v lib.rs

// BUG: This case is unexpected. Basically, lib2.rs and lib.rs are formatted,
// but the `format_me_please.rs` file is not even considered.
$ rustfmt -v lib2.rs lib.rs

Meta

  • rustfmt version: 1.33
  • From where did you install rustfmt?: Rustup, but also compiled from source.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugPanic, non-idempotency, invalid code, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions