rust icon indicating copy to clipboard operation
rust copied to clipboard

Condense `use` sections

Open nnethercote opened this issue 1 month ago • 14 comments

use items can be broken (or not) into sections in many ways: crate items, self items, std items, rustc_* items, third-party crate items. Also pub vs non-pub, and items with/without attributes (e.g. #[cfg(parallel_compiler)]. rustfmt is very unopinionated about this and, as a result, there is little consistency within the rustc codebase on the right way to do it.

However, for vanilla use items (i.e. no pub or attributes) it never makes sense for use crate::a to be in a different section to use crate::b, or use rustc_foo::a to be in a different section to use rustc_bar::b, etc. This PR fixes all such cases that I could find. I tried to each change in the simple/obvious way -- e.g. if there is a single use std::foo by itself in a section and then three use std::... items in another section then I moved the single one into the group of three -- but there were a few cases where there was no clear move and judgment was needed.

r? @RalfJung

nnethercote avatar May 13 '24 06:05 nnethercote

Some changes occurred in coverage instrumentation.

cc @Zalathar

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras

This PR changes Stable MIR

cc @oli-obk, @celinval, @ouz-a

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred in exhaustiveness checking

cc @Nadrieril

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

rustbot avatar May 13 '24 06:05 rustbot

@bors rollup=never p=1

Because this is highly conflict-prone.

nnethercote avatar May 13 '24 07:05 nnethercote

The interpreter changes LGTM; I don't currently have the capacity to review the entire PR unfortunately.

r? compiler

RalfJung avatar May 13 '24 09:05 RalfJung

Once i tried to use https://rust-lang.github.io/rustfmt/?version=master&search=#group_imports StdExternalCrate but that given truly big diff, so i given up :-(

klensy avatar May 13 '24 09:05 klensy

I don't currently have the capacity to review the entire PR unfortunately.

I have seen you say this on other PRs. I admit I find it surprising. You are regularly in the top three contributors to each Rust release as measured by number of commits, and often the top contributor. Oh well.

r? @saethlin

nnethercote avatar May 13 '24 09:05 nnethercote

All commits in Miri get mirrored over to rustc, so this isn't painting an adequate picture. Most of my work is on Miri.

I'm also trying to reduce the amount of time I spend on Rust, for my sanity's sake. It's not working out too well so far, but certainly I shouldn't increase the scope of what I review. Instead I should focus on contributing where I can make the biggest difference -- Miri and unsafe code things.

RalfJung avatar May 13 '24 09:05 RalfJung

Each individual change looks reasonable to me.

I spotted a few more opportunities to merge same-module imports, but I appreciate the desire to not scope-creep too much, especially for such a rot-prone change. 😅

(EDIT: This is on the assumption that merging same-module imports in this PR is OK. If it turns out to not be, then disregard my suggestions.)

Zalathar avatar May 13 '24 12:05 Zalathar

I am fine with moving entire lines around, but I do not want to see changes that turn this style:

use rustc_crate::a;
use rustc_crate::b;

Into this:

use rustc_crate::{a, b};

The latter style is extremely prone to merge conflicts, because any two PRs that want to add or remove an import from rust_crate will conflict with each other. Importing each item on its own line uses a considerable amount of space at the top of each file for imports, but really does result in less merge conflicts.

saethlin avatar May 13 '24 12:05 saethlin

I am fine with moving entire lines around, but I do not want to see changes that turn this style:

use rustc_crate::a;
use rustc_crate::b;

Into this:

use rustc_crate::{a, b};

Huh, I didn't expect that. Just to make sure I'm understanding, in general your preferred style would be for something like this:

use rustc_span::{sym, Span, SpanDecoder, SpanEncoder, Symbol, DUMMY_SP};

To instead be this:

use rustc_span::sym;
use rustc_span::Span;
use rustc_span::SpanDecoder;
use rustc_span::SpanEncoder;
use rustc_span::Symbol;
use rustc_span::DUMMY_SP;

Is that right? (I understand you're not advocating for proactively splitting up existing lines, but over time as natural use churn occurs is that the style you would prefer to emerge?)

nnethercote avatar May 13 '24 22:05 nnethercote

Is that right? (I understand you're not advocating for proactively splitting up existing lines, but over time as natural use churn occurs is that the style you would prefer to emerge?)

Yes you understand me completely.

saethlin avatar May 13 '24 23:05 saethlin

Huh. The one-import-per-line style is very far from the current state of rustc!

So on one side I have @saethlin asking for no merging of mergeable lines, and on the other side I have @Zalathar pointing out all the places where I missed merging mergeable lines.

rustfmt really missed a trick in allowing so much flexibility here :(

nnethercote avatar May 13 '24 23:05 nnethercote

Yeah. For what it's worth, I prefer the look of imports_granularity = "Crate", but getting kicked out of the queue because I wanted to add an import from the same module as someone else just wins out.

saethlin avatar May 13 '24 23:05 saethlin

:umbrella: The latest upstream changes (presumably #125076) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar May 14 '24 01:05 bors

as if on cue

saethlin avatar May 14 '24 02:05 saethlin

I did have to fix my share of merge conflicts in import lines... these are usually fairly trivial to fix. I don't think it is so bad as to warrant splitting all the imports, which makes them a lot more verbose and harder to parse.

We could do this

use rustc_span::{
    sym,
    Span,
    SpanDecoder,
    SpanEncoder,
    Symbol,
    DUMMY_SP,
};

to reduce verbosity and merge conflicts, but rustfmt won't let us.

RalfJung avatar May 14 '24 05:05 RalfJung

but rustfmt won't let us.

That is imports_layout="Vertical", AFAICT.

nnethercote avatar May 14 '24 06:05 nnethercote

as if on cue

You are right that one-import-line greatly reduces the likelihood of merge commits. But multiple-imports-per-line is the style used in the overwhelming majority of cases in the compiler's existing code. It feels odd to have changes rejected for conforming to the predominant style.

It would be really nice if imports_layout and group_imports were stable and reliable and we could just choose a value for each and let this be handled automatically.

nnethercote avatar May 14 '24 06:05 nnethercote

That is imports_layout="Vertical", AFAICT.

Ah, TIL! Thanks.

RalfJung avatar May 14 '24 06:05 RalfJung

It feels odd to have changes rejected for conforming to the predominant style.

Has the team ever agreed on a style? I'm willing to +1 the majority opinion (as opposed to consensus), I'm just not aware of any discussion of the matter.

saethlin avatar May 14 '24 13:05 saethlin

There is no style guide because rustfmt and tidy theoretically avoid the need for one, though theory and practice don't align entirely, as this PR indicates.

When I say "predominant" style I just mean what the existing code does. When it comes to section ordering, for example, there are a variety of styles and no single one dominates. But within sections, multiple imports per line is overwhelmingly common. Single import per line is extremely rare, and you can see in this PR both @Zalathar and I naturally gravitated to merging imports where possible. And the PR description is premised on multiple imports per line being non-controversial.

nnethercote avatar May 14 '24 21:05 nnethercote

I'm not going to sign off on changes which I think are net-negative on the basis of "it looks like that's how things are being done right now" as opposed to a conscious team decision. I've seen way too many things like this result in an outcome the team does not like because individuals keep going with "it looks like this is how we do things".

If you want someone to sign off on this, re-roll for another reviewer or start a team discussion.

saethlin avatar May 20 '24 14:05 saethlin

I also don't think we should do this. Either we decide on rustfmt settings as a team, or we keep the current chaotic system. But doing a manual reformatting towards some sort of goal is not improving things in my eyes.

oli-obk avatar May 21 '24 06:05 oli-obk