Skip to content

Add support for attribute on struct literal field #1462

Closed
@joshlf

Description

@joshlf

rustfmt is currently unable to format code which uses the new (and unstable) attributes on struct fields feature. Demonstration:

$ echo '
> struct Foo {
>     foo: usize,
>     #[cfg(feature = "include-bar")]
>     bar: usize,
> }
> 
> fn new_foo() -> Foo {
>     Foo { 
>         foo: 0,
>         #[cfg(feature = "include-bar")]
>         bar: 0,
>     }
> }
> ' | rustfmt
error: expected identifier, found `#`
  --> stdin:14:9
   |
14 |         #[cfg(feature = "include-bar")]
   |         ^

Interestingly, the same code on the Rust playground is simply formatted to remove the #[cfg(feature = "include-bar")] annotation, producing:

struct Foo {
    foo: usize,
    #[cfg(feature = "include-bar")]
    bar: usize,
}

fn new_foo() -> Foo {
    Foo { foo: 0, bar: 0 }
}

Activity

nrc

nrc commented on May 1, 2017

@nrc
Member

I think this just needs us to update the parser (Syntex)

added a commit that references this issue on Jun 5, 2017
b9d9d4f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @nrc@joshlf

      Issue actions

        Add support for attribute on struct literal field · Issue #1462 · rust-lang/rustfmt