rust-lang / rust-lang/rustfmt

Strange formatting of complex generics and `impl` types

Open
#5,921 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-generics A-trait-bounds C-bug I-poor-formatting I-vertical-alignment S-has-mcve
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=784781f3d4acde35deaea2ac70d9bcd1

I would expect either

pub fn layer(
    state: Arc<State>,
) -> impl Layer<
    Route,
    Service = impl Clone
    + Service<
        Request<Body>,
        Response = Response,
        Future = impl Send,
        Error = std::convert::Infallible,
    >,
> + Clone {
}

or

pub fn layer(
    state: Arc<State>,
) -> impl Layer<
    Route,
    Service = impl Clone + Service<
        Request<Body>,
        Response = Response,
        Future = impl Send,
        Error = std::convert::Infallible,
    >,
> + Clone {
}

but I get

pub fn layer(
    state: Arc<State>,
) -> impl Layer<
    Route,
    Service = impl Clone
                  + Service<
        Request<Body>,
        Response = Response,
        Future = impl Send,
        Error = std::convert::Infallible,
    >,
> + Clone {
}

What is the + Service< even supposed to be aligned to? This seems wrong.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open the linked Rust Playground reproduction and compare rustfmt's output with the two expected formatting examples in the issue. Trace the formatting behavior for nested generics and impl types, then verify that the reproduced signature no longer receives the unexpected alignment; no specific source file or test is named.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.