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

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

Closed
dtolnay opened this issue Mar 29, 2022 · 1 comment · Fixed by #5282
Closed

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

dtolnay opened this issue Mar 29, 2022 · 1 comment · Fixed by #5282
Labels
a-macros bug Panic, non-idempotency, invalid code, etc.

Comments

@dtolnay
Copy link
Member

dtolnay commented Mar 29, 2022

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!(-);
  |          +
@ytmimi ytmimi added a-macros bug Panic, non-idempotency, invalid code, etc. labels Mar 29, 2022
@dtolnay
Copy link
Member Author

dtolnay commented Mar 29, 2022

This is important to https://github.com/dtolnay/cxx, which uses macro calls such as the following where the <>-based ones are affected by this bug:

#[cxx::bridge]
mod ffi {
    extern "C++" {
        include!("path/to/thing.h");    // turns into `#include "path/to/thing.h"` in C++
        include!(<some/systemheader>);   // turns into `#include <some/systemheader>`

        type Thing;
        fn whatever() -> UniquePtr<Thing>;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-macros bug Panic, non-idempotency, invalid code, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants