Skip to content

Commit a82b008

Browse files
committed
fix: imports_granularity module with path containing self
1 parent 272fb42 commit a82b008

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

src/imports.rs

+38-12
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,17 @@ pub(crate) fn merge_use_trees(use_trees: Vec<UseTree>, merge_by: SharedPrefix) -
190190
continue;
191191
}
192192

193-
for flattened in use_tree.flatten() {
193+
for mut flattened in use_tree.flatten() {
194194
if let Some(tree) = result
195195
.iter_mut()
196196
.find(|tree| tree.share_prefix(&flattened, merge_by))
197197
{
198198
tree.merge(&flattened, merge_by);
199199
} else {
200+
// If this is the first tree with this prefix, handle potential trailing ::self
201+
if merge_by == SharedPrefix::Module {
202+
flattened = flattened.nest_trailing_self();
203+
}
200204
result.push(flattened);
201205
}
202206
}
@@ -208,17 +212,7 @@ pub(crate) fn flatten_use_trees(use_trees: Vec<UseTree>) -> Vec<UseTree> {
208212
use_trees
209213
.into_iter()
210214
.flat_map(UseTree::flatten)
211-
.map(|mut tree| {
212-
// If a path ends in `::self`, rewrite it to `::{self}`.
213-
if let Some(UseSegment::Slf(..)) = tree.path.last() {
214-
let self_segment = tree.path.pop().unwrap();
215-
tree.path.push(UseSegment::List(vec![UseTree::from_path(
216-
vec![self_segment],
217-
DUMMY_SP,
218-
)]));
219-
}
220-
tree
221-
})
215+
.map(UseTree::nest_trailing_self)
222216
.collect()
223217
}
224218

@@ -635,6 +629,18 @@ impl UseTree {
635629
self.span = self.span.to(other.span);
636630
}
637631
}
632+
633+
/// If this tree ends in `::self`, rewrite it to `::{self}`.
634+
fn nest_trailing_self(mut self) -> UseTree {
635+
if let Some(UseSegment::Slf(..)) = self.path.last() {
636+
let self_segment = self.path.pop().unwrap();
637+
self.path.push(UseSegment::List(vec![UseTree::from_path(
638+
vec![self_segment],
639+
DUMMY_SP,
640+
)]));
641+
}
642+
self
643+
}
638644
}
639645

640646
fn merge_rest(
@@ -1311,4 +1317,24 @@ mod test {
13111317
< parse_use_tree("std::cmp::{b, e, g, f}").normalize()
13121318
);
13131319
}
1320+
1321+
#[test]
1322+
fn test_use_tree_nest_trailing_self() {
1323+
assert_eq!(
1324+
parse_use_tree("a::b::self").nest_trailing_self(),
1325+
parse_use_tree("a::b::{self}")
1326+
);
1327+
assert_eq!(
1328+
parse_use_tree("a::b::c").nest_trailing_self(),
1329+
parse_use_tree("a::b::c")
1330+
);
1331+
assert_eq!(
1332+
parse_use_tree("a::b::{c, d}").nest_trailing_self(),
1333+
parse_use_tree("a::b::{c, d}")
1334+
);
1335+
assert_eq!(
1336+
parse_use_tree("a::b::{self, c}").nest_trailing_self(),
1337+
parse_use_tree("a::b::{self, c}")
1338+
);
1339+
}
13141340
}

tests/source/imports_granularity_module.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use a::{b::c, d::e};
44
use a::{f, g::{h, i}};
55
use a::{j::{self, k::{self, l}, m}, n::{o::p, q}};
66
pub use a::{r::s, t};
7+
use b::{c::d, self};
78

89
#[cfg(test)]
910
use foo::{a::b, c::d};

tests/target/imports_granularity_module.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use a::n::o::p;
1010
use a::n::q;
1111
pub use a::r::s;
1212
pub use a::t;
13+
use b::c::d;
14+
use b::{self};
1315

1416
use foo::e;
1517
#[cfg(test)]

0 commit comments

Comments
 (0)