Skip to content

file-lines can't partially format trait bounds lists #6894

@randomPoison

Description

@randomPoison

If I have a long list of trait bounds in a generic params list, I'd expect to be able to use the --file-lines option to format part of that list without affecting other parts of the function signature (or whatever the surrounding context is). But right now if any attempt to partially format the bounds list will cause the entire list to be reformatted, potentially pulling it all onto one line, modifying unselected lines in the process.

Summary

Consider the following code, where there's a long list of trait bounds and a couple of them are misaligned:

fn whatever<
    A: Clone
        + Copy
        + Debug
        + Display
    + PartialEq
    + Eq
        + PartialOrd
        + Ord
        + Hash,
>() {
}

I would like to use --file-lines to format just lines 6-7 to pull the two misaligned trait bounds in line with the rest, but not otherwise reformat the list.

Expected behavior

fn whatever<
    A: Clone
        + Copy
        + Debug
        + Display
        + PartialEq
        + Eq
        + PartialOrd
        + Ord
        + Hash,
>() {
}

Actual behavior

fn whatever<A: Clone + Copy + Debug + Display + PartialEq + Eq + PartialOrd + Ord + Hash>() {}

Note that because of #6868 the entire function signature is getting reformatted to one line. If that issue were fixed, the output here would look more like the following, where just the bounds list gets reformatted:

fn whatever<
    A: Clone + Copy + Debug + Display + PartialEq + Eq + PartialOrd + Ord + Hash,
>() {
}

That highlights the more fundamental issue here, that the logic for laying out the trait bounds list doesn't account for --file-lines and so can pull everything onto one line, modifying unselected lines in the process.

Configuration

rustfmt cli options used:

rustfmt --unstable-features --file-lines '[{"file":"test.rs","range":[6,7]}]' test.rs

Meta

The relevant code is in join_bounds_inner, which currently doesn't check the --file-lines configuration at all.

There's a few related issues here due to --file-lines not being fully implemented:

  • Right now any attempt to partially format a function signature will reformat the whole thing (tracked by file-lines can't partially format function signatures #6868), so in the example above we see the entire function get rewritten to a single line.
  • Part of file-lines can't partially format where clauses #6872, which tracks partial formatting of where clauses. Fixing this issue is a requirement for fixing that one, since a where clause is made up of a list of trait bounds lists, but I think this is worth splitting off into its own issue because trait bounds lists can show up outside of where clauses.
  • This might need to be added as a blocker for [unstable option] file_lines #3397, though I think there's some room to discuss what the actual desired behavior is here, and that may mean this doesn't need to block stabilization.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICEUO-file_linesUnstable option: file_linesneeds-triageThis issue or PR needs triaging to determine its status. Remove label once sufficiently triaged.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions