Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(import_granularity): do not merge aliases #6028

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,9 +845,18 @@ fn merge_use_trees_inner(trees: &mut Vec<UseTree>, use_tree: UseTree, merge_by:
// tree `use_tree` should be merge.
// In other cases `similarity` won't be used, so set it to `0` as a dummy value.
let similarity = if merge_by == SharedPrefix::One {
let tree_depth = tree.path.len();

// Only merge prefixes, not leaves,
// e.g. `foo::i` with `foo::i as j`.
let max_depth = (tree_depth == use_tree.path.len())
.then_some(tree_depth - 1)
.unwrap_or(usize::MAX);

tree.path
.iter()
.zip(&use_tree.path)
.take(max_depth)
.take_while(|(a, b)| a.equal_except_alias(b))
.count()
} else {
Expand Down
1 change: 1 addition & 0 deletions tests/source/5131_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ use bar::d::e;
use bar::d::e as e2;
use qux::h;
use qux::i;
use qux::i as j;
4 changes: 2 additions & 2 deletions tests/target/5131_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ pub use foo::{x, x as x2, y};
use {
bar::{
a,
b::{self, f, g},
b::{self, f, f as f2, g},
c,
d::{e, e as e2},
},
qux::{h, i},
qux::{h, i, i as j},
};
Loading