Skip to content
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

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

Closed
smklein opened this issue Jan 19, 2021 · 8 comments
Labels
bug Panic, non-idempotency, invalid code, etc.

Comments

@smklein
Copy link
Contributor

smklein commented Jan 19, 2021

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.
@smklein smklein added the bug Panic, non-idempotency, invalid code, etc. label Jan 19, 2021
@smklein
Copy link
Contributor Author

smklein commented Jan 19, 2021

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.

@smklein
Copy link
Contributor Author

smklein commented Jan 19, 2021

Also, I think this is related to #3253

@smklein
Copy link
Contributor Author

smklein commented Jan 19, 2021

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

smklein added a commit to oxidecomputer/hubris that referenced this issue Jan 20, 2021
The conditionally-included modules are not being
automatically formatted due to:
rust-lang/rustfmt#4656
smklein added a commit to oxidecomputer/hubris that referenced this issue Jan 26, 2021
The conditionally-included modules are not being
automatically formatted due to:
rust-lang/rustfmt#4656
smklein added a commit to oxidecomputer/hubris that referenced this issue Jan 26, 2021
The conditionally-included modules are not being
automatically formatted due to:
rust-lang/rustfmt#4656

Although there is a patch to fix this issue upstream, this PR manually fixes these unformatted modules.
@calebcartwright calebcartwright added this to the 1.4.33 milestone Jan 28, 2021
@calebcartwright
Copy link
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

@calebcartwright calebcartwright removed this from the 1.4.33 milestone Jan 28, 2021
JohnTitor added a commit to JohnTitor/rust that referenced this issue Jan 30, 2021
…anxiyn

update rustfmt to v1.4.34

Short summary: Various formatting fixes (several const generic related) and introduction of `imports_granularity` config option

Long summary copied from changelog:

#### Changed
- `merge_imports` configuration has been deprecated in favor of the new `imports_granularity` option. Any existing usage of `merge_imports` will be automatically mapped to the corresponding value on `imports_granularity` with a warning message printed to encourage users to update their config files.

#### Added
- New `imports_granularity` option has been added which succeeds `merge_imports`. This new option supports several additional variants which allow users to merge imports at different levels (crate or module), and even flatten imports to have a single use statement per item. ([PR rust-lang/rustfmt#4634](rust-lang/rustfmt#4634), [PR rust-lang/rustfmt#4639](rust-lang/rustfmt#4639))

See the section on the configuration site for more information
https://rust-lang.github.io/rustfmt/?version=v1.4.33&search=#imports_granularity

#### Fixed
- Fix erroneous removal of `const` keyword on const trait impl ([rust-lang/rustfmt#4084](rust-lang/rustfmt#4084))
- Fix incorrect span usage wit const generics in supertraits ([rust-lang/rustfmt#4204](rust-lang/rustfmt#4204))
- Use correct span for const generic params ([rust-lang/rustfmt#4263](rust-lang/rustfmt#4263))
- Correct span on const generics to include type bounds ([rust-lang/rustfmt#4310](rust-lang/rustfmt#4310))
- Idempotence issue on blocks containing only empty statements ([rust-lang/rustfmt#4627](rust-lang/rustfmt#4627) and [rust-lang#3868](rust-lang/rustfmt#3868))
- Fix issue with semicolon placement on required functions that have a trailing comment that ends in a line-style comment before the semicolon ([rust-lang/rustfmt#4646](rust-lang/rustfmt#4646))
- Avoid shared interned cfg_if symbol since rustfmt can re-initialize the rustc_ast globals on multiple inputs ([rust-lang/rustfmt#4656](rust-lang/rustfmt#4656))
- Don't insert trailing comma on (base-less) rest in struct literals within macros ([rust-lang/rustfmt#4675](rust-lang/rustfmt#4675))
@ytmimi
Copy link
Contributor

ytmimi commented Apr 11, 2022

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

@calebcartwright
Copy link
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.

@ytmimi
Copy link
Contributor

ytmimi commented Apr 12, 2022

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

@calebcartwright
Copy link
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
Labels
bug Panic, non-idempotency, invalid code, etc.
Projects
None yet
Development

No branches or pull requests

3 participants