Skip to content

fix: make imports_granularity One preserve aliases - #7007

Open
willbuckner wants to merge 1 commit into
rust-lang:mainfrom
willbuckner:will/imports-granularity-one
Open

fix: make imports_granularity One preserve aliases#7007
willbuckner wants to merge 1 commit into
rust-lang:mainfrom
willbuckner:will/imports-granularity-one

Conversation

@willbuckner

Copy link
Copy Markdown

Fix imports_granularity = "One" to preserve aliases.

In the following example by sivizius:

pub use foo::x;
pub use foo::x as x2;
pub use foo::y;
use bar::a;
use bar::b;
use bar::b::f;
use bar::b::f as f2;
use bar::b::g;
use bar::c;
use bar::d::e;
use bar::d::e as e2;
use qux::h;
use qux::i;
use qux::i as j;

bar::b::f as f2; and qux::i as j; were silently dropped, returning this merged result:

  pub use foo::{x, x as x2, y};
  use {
      bar::{
          a,
          b::{self, f, g},
          c,
          d::{e, e as e2},
      },
      qux::{h, i},
  };

Two import paths that only differ by the alias of their last segment were being treated as equal when merging, keeping only one of the two names. They now get merged into a list containing both, e.g., qux::{h, i, i as j}.

This also fixes two related problems:

  • Merging use qux::h; followed by use qux as Q; produced the invalid use qux as Q::{self as Q, h}; because the merged root kept the alias of the shorter path.
  • The result of merging depends on the order in which the use trees get visited, and once aliases are preserved a single pass over inputs like use a; use a as b; use a::c; did not, produce a stable result. a::{self as b} is now normalized to the equivalent a as b when flattening, and merging is repeated until it reaches a fixed point, so that formatting stays idempotent.

Fixes: #6027

Fix `imports_granularity = "One"` to preserve aliases.

In the following example by sivizius:

```rust
pub use foo::x;
pub use foo::x as x2;
pub use foo::y;
use bar::a;
use bar::b;
use bar::b::f;
use bar::b::f as f2;
use bar::b::g;
use bar::c;
use bar::d::e;
use bar::d::e as e2;
use qux::h;
use qux::i;
use qux::i as j;
```

`bar::b::f as f2`; and `qux::i as j`; were silently dropped, returning
this merged result:

```rust
  pub use foo::{x, x as x2, y};
  use {
      bar::{
          a,
          b::{self, f, g},
          c,
          d::{e, e as e2},
      },
      qux::{h, i},
  };
```

Two import paths that only differ by the alias of their last segment were
being treated as equal when merging, keeping only one of the two names. They
now get merged into a list containing both, e.g., `qux::{h, i, i as j}`.

This also fixes two related problems:

- Merging `use qux::h;` followed by `use qux as Q;` produced the
  invalid `use qux as Q::{self as Q, h};` because the merged root kept
  the alias of the shorter path.
- The result of merging depends on the order in which the `use`
  trees get visited, and once aliases are preserved a single pass over
  inputs like `use a; use a as b; use a::c;` did not, produce a stable
  result. `a::{self as b}` is now normalized to the equivalent
  `a as b` when flattening, and merging is repeated until it reaches a
  fixed point, so that formatting stays idempotent.

Fixes: rust-lang#6027
@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Aug 3, 2026
@ytmimi ytmimi added the UO-imports_granularity Unstable option: imports_granularity label Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: awaiting review from the assignee but also interested parties. UO-imports_granularity Unstable option: imports_granularity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: imports_granularity = "One" deletes aliases

3 participants