rust-lang / rust-lang/rustfmt

rusfmt hangs when using cfg-if macro

Open
#4,442 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

rusfmt hangs when using cfg-if macro

To reproduce run rustftm main.rs, where:

// main.rs
cfg_if::cfg_if! {
    if #[cfg(not(feature = "client"))] {
        if #[cfg(feature = "server")] {
        }
    }
}
rustfmt -V
rustfmt 1.4.18-stable (8157a3f 2020-07-15)

From the logs RUST_LOG=trace rustftm main.rs I can see it just keeps logging:

[2020-09-29T14:17:39Z DEBUG rustc_ap_rustc_parse::parser::attr] parse_outer_attributes: self.token=Token { kind: Ident("if", false), span: Span { lo: BytePos(67), hi: BytePos(69), ctxt: #0 } }
[2020-09-29T14:17:39Z DEBUG rustc_ap_rustc_parse::parser::attr] parse_outer_attributes: self.token=Token { kind: Ident("if", false), span: Span { lo: BytePos(67), hi: BytePos(69), ctxt: #0 } }
[2020-09-29T14:17:39Z DEBUG rustc_ap_rustc_parse::parser::attr] parse_outer_attributes: self.token=Token { kind: Ident("if", false), span: Span { lo: BytePos(67), hi: BytePos(69), ctxt: #0 } }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the hang with the provided main.rs cfg-if example using rustfmt 1.4.18 and inspect the repeated parser trace with RUST_LOG=trace. Trace the parser state around the nested cfg attributes and confirm that formatting the example terminates without repeatedly logging the same token.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.