Skip to content

Generic associated impl trait type with bounds on self formatted to broken code #4911

Closed
@tim3z

Description

@tim3z

Describe the bug

When specifying an impl trait type alias for a generic associated type with a lifetime bound on Self, rustfmt will remove the bound, causing a compiler error, i.e.:

impl SomeTrait for SomeType {
    type SomeGAT<'a> where Self: 'a = impl SomeOtherTrait;
}

will be changed to

impl SomeTrait for SomeType {
    type SomeGAT<'a> = impl SomeOtherTrait;
}

To Reproduce

The following code compiles, but when running rustfmt on it it will remove the Self bound where Self" 'a in the implementation for Foo leading to a compiler error:

#![allow(incomplete_features)]
#![feature(generic_associated_types)]
#![feature(min_type_alias_impl_trait)]

struct Foo<'b>(&'b ());

trait Iterable {
    type Iter<'a>
    where
        Self: 'a;
    fn iter(&self) -> Self::Iter<'_>;
}

impl<'b> Iterable for Foo<'b> {
    type Iter<'a> where Self: 'a = impl Iterator + 'a;

    fn iter(&self) -> Self::Iter<'_> {
        std::iter::once(self.0)
    }
}

Expected behavior

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

Meta

  • rustfmt 1.4.37-nightly (014026d 2021-07-19)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: rustfmt, sublime RustFmt package

Metadata

Metadata

Assignees

No one assigned

    Labels

    2x-port:pendingbugPanic, non-idempotency, invalid code, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions