Skip to content

Rustfmt creates invalid syntax on extern block containing macro call #5281

@dtolnay

Description

@dtolnay
macro_rules! x {
    ($tt:tt) => {};
}

extern "C" {
    x!(-);
    x!(x);
}

Rustfmt loses the necessary ; after some macro calls, which becomes invalid Rust syntax in the above input. The call x!(x); appears unaffected, regardless of the order of the two calls.

-     x!(-);
+     x!(-)

Self-contained repro as of current master (e0c7b7d):

$ echo 'extern { x!(-); x!(x); }' | cargo run --bin rustfmt
extern "C" {
    x!(-)
    x!(x);
}

$ !! | rustc -
error: macros that expand to items must be delimited with braces or followed by a semicolon
 --> <anon>:2:7
  |
2 |     x!(-)
  |       ^^^
  |
help: change the delimiters to curly braces
  |
2 |     x!{-}
  |       ~ ~
help: add a semicolon
  |
2 |     x!(-);
  |          +

Metadata

Metadata

Assignees

No one assigned

    Labels

    a-macrosbugPanic, 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