fix: make imports_granularity One preserve aliases - #7007
Open
willbuckner wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix
imports_granularity = "One"to preserve aliases.In the following example by sivizius:
bar::b::f as f2; andqux::i as j; were silently dropped, returning this merged result: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:
use qux::h;followed byuse qux as Q;produced the invaliduse qux as Q::{self as Q, h};because the merged root kept the alias of the shorter path.usetrees get visited, and once aliases are preserved a single pass over inputs likeuse a; use a as b; use a::c;did not, produce a stable result.a::{self as b}is now normalized to the equivalenta as bwhen flattening, and merging is repeated until it reaches a fixed point, so that formatting stays idempotent.Fixes: #6027