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

Indentation in macro is increased in each run (non-idempotent) #5778

Closed
JanBeh opened this issue Jun 7, 2023 · 3 comments
Closed

Indentation in macro is increased in each run (non-idempotent) #5778

JanBeh opened this issue Jun 7, 2023 · 3 comments

Comments

@JanBeh
Copy link

JanBeh commented Jun 7, 2023

The following code is given:

macro_rules! m {
    ($x:tt) => {
        type T = $x
        where
            A: B;
    };
}

Running rustfmt results in:

macro_rules! m {
    ($x:tt) => {
        type T = $x
                where
                    A: B;
    };
}

Running it again:

macro_rules! m {
    ($x:tt) => {
        type T = $x
                        where
                            A: B;
    };
}

And so on.

The following smaller example is idempotent, however:

 macro_rules! m {
     ($x:tt) => {
-        type T = $x
+        type $x
         where
             A: B;
     };

I'm using:

rustfmt 1.5.2-nightly (b2b34bd8 2023-06-06)

The above example is a minimal example to reproduce the problem. The full code I used can be found here.

@ytmimi
Copy link
Contributor

ytmimi commented Jun 7, 2023

@JanBeh thanks for reaching out!

This issue stems from rustfmt's lack of support for formatting associated types using the new recommend style where the where clause come after the type name.

For example:

type Assoc<Params> = Type where WhereBounds;
type Assoc<Params> where WhereBounds = Type; // deprecated, prefer the form above

This is related to #5580

As a workaround you might consider using the deprecated style to avoid this issue (which rustfmt handles just fine):

macro_rules! m {
    ($x:tt) => {
        // FIXME: waiting till https:/rust-lang/rustfmt/issues/5778 is resolved.
        type T
        where
            A: B,
        = $x;
    };
}

Alternatively, you could use the #[rustfmt::skip] attribute:

#[rustfmt::skip]
macro_rules! m {
    ($x:tt) => {
        type T = $x
        where
            A: B;
    };
}

@ytmimi ytmimi added bug Panic, non-idempotency, invalid code, etc. a-macros labels Jun 7, 2023
@calebcartwright
Copy link
Member

Thanks for reaching out and creating a minimal repo! I'm going to close this, however, because as Yacin noted it's a duplicate of couple known items tracked elsewhere; in addition to #5580 rustfmt has a bug (#5062/#5044) where if it encounters something it doesn't know how to format within a macro it produces that non-idempotent

@calebcartwright calebcartwright closed this as not planned Won't fix, can't repro, duplicate, stale Jun 7, 2023
@calebcartwright calebcartwright added duplicate and removed bug Panic, non-idempotency, invalid code, etc. a-macros labels Jun 7, 2023
@JanBeh
Copy link
Author

JanBeh commented Jun 7, 2023

Thank you very much for the references and the hint to use the old-style syntax. I figured there were somes issues with non-idempotence, but I wasn't sure which of those (if any) was covered by my case.

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

No branches or pull requests

3 participants