Skip to content

Produces invalid code for struct-literal like statement in macro #6795

@Person-93

Description

@Person-93

I am using the seq macro from the winnow library.

The macro takes a struct literal, but instead of passing in the values for the fields, you pass in functions which it uses to initialize the fields.

If the name of the function is the same as the name of the field, rustfmt tries to shorten it to struct literal shorthand which is not valid.

fn name<'i>(input: &mut Stream<'i>) -> ModalResult<&'i str> {
    todo!()
}

fn preconditions<'i>(input: &mut Stream<'i>) -> ModalResult<Preconditions<'i>> {
    todo!()
}

// this...
seq!(PrimitiveTask {
    name: name,
    preconditions: preconditions,
});

// ...is shortened to this. It does not compile.
seq!(PrimitiveTask { name, preconditions });

// I am using this as a workaround
seq!(PrimitiveTask {
    name: { name },
    preconditions: { preconditions },
});

The seq macro is a bit complex, so I am not sure what a minimal reproduction would look like.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: macros (procedural macros, macro_rules! macros, etc.)C-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICEI-invalid-codeIssue: formatting causes compile-pass code to compile-failSO-use_field_init_shorthandStable Option: use_field_init_shorthand

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions