Skip to content

Closing braces inside macros are double indented with hard tabs #3068

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

Closed
remagpie opened this issue Sep 29, 2018 · 1 comment · Fixed by #4189
Closed

Closing braces inside macros are double indented with hard tabs #3068

remagpie opened this issue Sep 29, 2018 · 1 comment · Fixed by #4189
Labels
a-macros only-with-option requires a non-default option value to reproduce poor-formatting

Comments

@remagpie
Copy link

rustfmt version: rustfmt 0.99.5-nightly (90692a5 2018-09-26)

When hard_tabs is off, it is formatted like this:

macro_rules! foo {
    ($bar: expr, $t: ty) => {
        $bar(|x| {
            if x {
                None
            }
            else {
                None
            }
        })
    };
}

When hard_tabs is on, it is formatted like this:

macro_rules! foo {
	($bar: expr, $t: ty) => {
		$bar(|x| {
			if x {
				None
				}
			else {
				None
				}
			})
	};
}

Closing braces have one more indentation when hard_tabs is on.

@topecongiro topecongiro added poor-formatting only-with-option requires a non-default option value to reproduce a-macros labels Sep 29, 2018
@vimpunk
Copy link

vimpunk commented Nov 20, 2018

I also have the same issue, I think:
From:

#[macro_export]
macro_rules! define_invoke_proc_macro {
    ($macro_name: ident) => {
        /// Implementation detail of other macros in this crate.
        #[doc(hidden)]
        #[macro_export]
        macro_rules! $macro_name {
            ($proc_macro_name: ident ! $paren: tt) => {
                #[derive($proc_macro_name)]
                #[allow(unused)]
                enum ProceduralMasqueradeDummyType {
                    // The magic happens here.
                    //
                    // We use an `enum` with an explicit discriminant
                    // because that is the only case where a type definition
                    // can contain a (const) expression.
                    //
                    // `(0, "foo").0` evalutes to 0, with the `"foo"` part ignored.
                    //
                    // By the time the `#[proc_macro_derive]` function
                    // implementing `#[derive($proc_macro_name)]` is called,
                    // `$paren` has already been replaced with the input of this inner macro,
                    // but `stringify!` has not been expanded yet.
                    //
                    // This how arbitrary tokens can be inserted
                    // in the input to the `#[proc_macro_derive]` function.
                    //
                    // Later, `stringify!(...)` is expanded into a string literal
                    // which is then ignored.
                    // Using `stringify!` enables passing arbitrary tokens
                    // rather than only what can be parsed as a const expression.
                    Input = (0, stringify! $paren ).0
                }
            }
        }
    }
}

To:

#[macro_export]
macro_rules! define_invoke_proc_macro {
    ($macro_name: ident) => {
        /// Implementation detail of other macros in this crate.
        #[doc(hidden)]
        #[macro_export]
        macro_rules! $macro_name {
                            ($proc_macro_name: ident ! $paren: tt) => {
                                #[derive($proc_macro_name)]
                                #[allow(unused)]
                                enum ProceduralMasqueradeDummyType {
                                    // The magic happens here.
                                    //
                                    // We use an `enum` with an explicit discriminant
                                    // because that is the only case where a type definition
                                    // can contain a (const) expression.
                                    //
                                    // `(0, "foo").0` evalutes to 0, with the `"foo"` part ignored.
                                    //
                                    // By the time the `#[proc_macro_derive]` function
                                    // implementing `#[derive($proc_macro_name)]` is called,
                                    // `$paren` has already been replaced with the input of this inner macro,
                                    // but `stringify!` has not been expanded yet.
                                    //
                                    // This how arbitrary tokens can be inserted
                                    // in the input to the `#[proc_macro_derive]` function.
                                    //
                                    // Later, `stringify!(...)` is expanded into a string literal
                                    // which is then ignored.
                                    // Using `stringify!` enables passing arbitrary tokens
                                    // rather than only what can be parsed as a const expression.
                                    Input = (0, stringify! $paren ).0
                                }
                            }
                        }
    };
}

Running rustfmt again indents the macro definition even more.

I use soft tabs (i.e. spaces for tabs) in my editor, unless that's a specific rustfmt setting I'm unaware of. Pardon if this is an unrelated issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-macros only-with-option requires a non-default option value to reproduce poor-formatting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants