Skip to content

Generic associated type constraints are reformatted into broken code #4802

@lulf

Description

@lulf

Describe the bug

When constraining a generic associated type, rustfmt will remove any parameters for that type. I.e.:

struct MyType<'a, A: MyApi<Assoc<'a> = ()>> {}

will be reformatted to

struct MyType<'a, A: MyApi<Assoc = ()>> {}

To Reproduce

The following code compiles and runs, but when running rustfmt on it it will remove the generics for the associated type:

#![feature(generic_associated_types)]

trait MyApi {
    type Assoc<'a>;
}

struct MyImpl {}

impl MyApi for MyImpl {
    type Assoc<'a> = ();
}

// NOTE: "Assoc<'a> = ()" gets reformatted as "Assoc = ()" when running rustfmt
struct MyType<'a, A: MyApi<Assoc<'a> = ()>> {
    a: A,
    _m: core::marker::PhantomData<&'a A>,
}

fn main() {
    let s = MyImpl {};
    let t = MyType {
        a: s,
        _m: core::marker::PhantomData,
    };
}

Expected behavior

The above code compiles and should not be formatted so it doesn't compile.

Meta

  • rustfmt version: 1.4.37-nightly (0bd2b19 2021-04-03)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: rustfmt, cargo fmt

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugPanic, non-idempotency, invalid code, etc.needs-test

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions