Skip to content

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

Closed

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.

Activity

  1. added
    C-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICE
    on Jan 19, 2021
  2. smklein commented on Jan 19, 2021

    @smklein
    ContributorAuthor

    I actually have a patch for this bug, but I wanted to get an issue number first so I could reference it in my PR. Will upload shortly!

    TL;DR: Interned symbols have different indices when crossing files; the check for cfg_if is currently a lazily-initialized global variable, but when it becomes a local, this issue is resolved.

  3. added a commit that references this issue on Jan 19, 2021
    ee51488
  4. smklein commented on Jan 19, 2021

    @smklein
    ContributorAuthor

    Also, I think this is related to #3253

  5. smklein commented on Jan 19, 2021

    @smklein
    ContributorAuthor

    Looking at rustfmt 2.0, it seems this issue was resolved by 3b7fd26 , but this PR is still relevant; as this bug currently exists in the 1.x branch

  6. added a commit that references this issue on Jan 28, 2021
    5e14f76
  7. added this to the 1.4.33 milestone on Jan 28, 2021
  8. calebcartwright commented on Jan 28, 2021

    @calebcartwright
    Member

    Have pulled this into the 1.4.33 release. Would you have any interest in adding your test case against the master branch? If not we can go ahead and close this

  9. removed this from the 1.4.33 milestone on Jan 28, 2021
  10. added a commit that references this issue on Jan 30, 2021
  11. ytmimi commented on Apr 11, 2022

    @ytmimi
    Contributor

    @calebcartwright as you mentioned you included this in the 1.4.33 release so I'm assuming this can be closed now

  12. calebcartwright commented on Apr 11, 2022

    @calebcartwright
    Member

    @calebcartwright as you mentioned you included this in the 1.4.33 release so I'm assuming this can be closed now

    I still think a test case would be worthwhile, but sure; test case can be added independently of the open/closed status of this issue.

  13. ytmimi commented on Apr 12, 2022

    @ytmimi
    Contributor

    @calebcartwright I think we've already got issue 4656 tests in the code base. Was there another test you had in mind? I can work to get a PR ready, I'm just not sure which test you had in mind.

  14. calebcartwright commented on Apr 12, 2022

    @calebcartwright
    Member

    Nope that works! Just bad memory on my part then 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICE

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions