Skip to content

Commit 13f7623

Browse files
committed
store the span of the nested part of the use tree in the ast
1 parent 2ec337c commit 13f7623

File tree

17 files changed

+45
-36
lines changed

17 files changed

+45
-36
lines changed

compiler/rustc_ast/src/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2703,7 +2703,7 @@ pub enum UseTreeKind {
27032703
/// `use prefix` or `use prefix as rename`
27042704
Simple(Option<Ident>),
27052705
/// `use prefix::{...}`
2706-
Nested(ThinVec<(UseTree, NodeId)>),
2706+
Nested { items: ThinVec<(UseTree, NodeId)>, span: Span },
27072707
/// `use prefix::*`
27082708
Glob,
27092709
}

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ pub fn noop_visit_use_tree<T: MutVisitor>(use_tree: &mut UseTree, vis: &mut T) {
436436
vis.visit_path(prefix);
437437
match kind {
438438
UseTreeKind::Simple(rename) => visit_opt(rename, |rename| vis.visit_ident(rename)),
439-
UseTreeKind::Nested(items) => {
439+
UseTreeKind::Nested { items, .. } => {
440440
for (tree, id) in items {
441441
vis.visit_use_tree(tree);
442442
vis.visit_id(id);

compiler/rustc_ast/src/visit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,8 @@ pub fn walk_use_tree<'a, V: Visitor<'a>>(
488488
visit_opt!(visitor, visit_ident, rename);
489489
}
490490
UseTreeKind::Glob => {}
491-
UseTreeKind::Nested(ref use_trees) => {
492-
for &(ref nested_tree, nested_id) in use_trees {
491+
UseTreeKind::Nested { ref items, .. } => {
492+
for &(ref nested_tree, nested_id) in items {
493493
try_visit!(visitor.visit_use_tree(nested_tree, nested_id, true));
494494
}
495495
}

compiler/rustc_ast_lowering/src/item.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
134134

135135
fn lower_item_id_use_tree(&mut self, tree: &UseTree, vec: &mut SmallVec<[hir::ItemId; 1]>) {
136136
match &tree.kind {
137-
UseTreeKind::Nested(nested_vec) => {
138-
for &(ref nested, id) in nested_vec {
137+
UseTreeKind::Nested { items, .. } => {
138+
for &(ref nested, id) in items {
139139
vec.push(hir::ItemId {
140140
owner_id: hir::OwnerId { def_id: self.local_def_id(id) },
141141
});
@@ -517,7 +517,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
517517
let path = self.lower_use_path(res, &path, ParamMode::Explicit);
518518
hir::ItemKind::Use(path, hir::UseKind::Glob)
519519
}
520-
UseTreeKind::Nested(ref trees) => {
520+
UseTreeKind::Nested { items: ref trees, .. } => {
521521
// Nested imports are desugared into simple imports.
522522
// So, if we start with
523523
//

compiler/rustc_ast_pretty/src/pprust/state/item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ impl<'a> State<'a> {
712712
}
713713
self.word("*");
714714
}
715-
ast::UseTreeKind::Nested(items) => {
715+
ast::UseTreeKind::Nested { items, .. } => {
716716
if !tree.prefix.segments.is_empty() {
717717
self.print_path(&tree.prefix, false, 0);
718718
self.word("::");
@@ -731,7 +731,7 @@ impl<'a> State<'a> {
731731
self.print_use_tree(&use_tree.0);
732732
if !is_last {
733733
self.word(",");
734-
if let ast::UseTreeKind::Nested(_) = use_tree.0.kind {
734+
if let ast::UseTreeKind::Nested { .. } = use_tree.0.kind {
735735
self.hardbreak();
736736
} else {
737737
self.space();

compiler/rustc_builtin_macros/src/assert/context.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,13 @@ impl<'cx, 'a> Context<'cx, 'a> {
120120
thin_vec![self.cx.attr_nested_word(sym::allow, sym::unused_imports, self.span)],
121121
ItemKind::Use(UseTree {
122122
prefix: self.cx.path(self.span, self.cx.std_path(&[sym::asserting])),
123-
kind: UseTreeKind::Nested(thin_vec![
124-
nested_tree(self, sym::TryCaptureGeneric),
125-
nested_tree(self, sym::TryCapturePrintable),
126-
]),
123+
kind: UseTreeKind::Nested {
124+
items: thin_vec![
125+
nested_tree(self, sym::TryCaptureGeneric),
126+
nested_tree(self, sym::TryCapturePrintable),
127+
],
128+
span: self.span,
129+
},
127130
span: self.span,
128131
}),
129132
),

compiler/rustc_expand/src/expand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1190,8 +1190,8 @@ impl InvocationCollectorNode for P<ast::Item> {
11901190
match &ut.kind {
11911191
ast::UseTreeKind::Glob => {}
11921192
ast::UseTreeKind::Simple(_) => idents.push(ut.ident()),
1193-
ast::UseTreeKind::Nested(nested) => {
1194-
for (ut, _) in nested {
1193+
ast::UseTreeKind::Nested { items, .. } => {
1194+
for (ut, _) in items {
11951195
collect_use_tree_leaves(ut, idents);
11961196
}
11971197
}

compiler/rustc_lint/src/unused.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ declare_lint_pass!(UnusedImportBraces => [UNUSED_IMPORT_BRACES]);
14991499

15001500
impl UnusedImportBraces {
15011501
fn check_use_tree(&self, cx: &EarlyContext<'_>, use_tree: &ast::UseTree, item: &ast::Item) {
1502-
if let ast::UseTreeKind::Nested(ref items) = use_tree.kind {
1502+
if let ast::UseTreeKind::Nested { ref items, .. } = use_tree.kind {
15031503
// Recursively check nested UseTrees
15041504
for (tree, _) in items {
15051505
self.check_use_tree(cx, tree, item);
@@ -1520,7 +1520,7 @@ impl UnusedImportBraces {
15201520
rename.unwrap_or(orig_ident).name
15211521
}
15221522
ast::UseTreeKind::Glob => Symbol::intern("*"),
1523-
ast::UseTreeKind::Nested(_) => return,
1523+
ast::UseTreeKind::Nested { .. } => return,
15241524
};
15251525

15261526
cx.emit_span_lint(

compiler/rustc_parse/src/parser/item.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl<'a> Parser<'a> {
336336
UseTreeKind::Glob => {
337337
e.note("the wildcard token must be last on the path");
338338
}
339-
UseTreeKind::Nested(..) => {
339+
UseTreeKind::Nested { .. } => {
340340
e.note("glob-like brace syntax must be last on the path");
341341
}
342342
_ => (),
@@ -1056,7 +1056,11 @@ impl<'a> Parser<'a> {
10561056
Ok(if self.eat(&token::BinOp(token::Star)) {
10571057
UseTreeKind::Glob
10581058
} else {
1059-
UseTreeKind::Nested(self.parse_use_tree_list()?)
1059+
let lo = self.token.span;
1060+
UseTreeKind::Nested {
1061+
items: self.parse_use_tree_list()?,
1062+
span: lo.to(self.prev_token.span),
1063+
}
10601064
})
10611065
}
10621066

compiler/rustc_resolve/src/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
565565

566566
self.add_import(prefix, kind, use_tree.span, item, root_span, item.id, vis);
567567
}
568-
ast::UseTreeKind::Nested(ref items) => {
568+
ast::UseTreeKind::Nested { ref items, .. } => {
569569
// Ensure there is at most one `self` in the list
570570
let self_spans = items
571571
.iter()

compiler/rustc_resolve/src/check_unused.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
128128
self.unused_import(self.base_id).add(id);
129129
}
130130
}
131-
ast::UseTreeKind::Nested(ref items) => self.check_imports_as_underscore(items),
131+
ast::UseTreeKind::Nested { ref items, .. } => self.check_imports_as_underscore(items),
132132
_ => {}
133133
}
134134
}
@@ -254,7 +254,7 @@ impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
254254
return;
255255
}
256256

257-
if let ast::UseTreeKind::Nested(ref items) = use_tree.kind {
257+
if let ast::UseTreeKind::Nested { ref items, .. } = use_tree.kind {
258258
if items.is_empty() {
259259
self.unused_import(self.base_id).add(id);
260260
}
@@ -292,7 +292,7 @@ fn calc_unused_spans(
292292
UnusedSpanResult::Used
293293
}
294294
}
295-
ast::UseTreeKind::Nested(ref nested) => {
295+
ast::UseTreeKind::Nested { items: ref nested, .. } => {
296296
if nested.is_empty() {
297297
return UnusedSpanResult::Unused { spans: vec![use_tree.span], remove: full_span };
298298
}

compiler/rustc_resolve/src/late.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2332,8 +2332,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
23322332
None => {}
23332333
}
23342334
}
2335-
} else if let UseTreeKind::Nested(use_trees) = &use_tree.kind {
2336-
for (use_tree, _) in use_trees {
2335+
} else if let UseTreeKind::Nested { items, .. } = &use_tree.kind {
2336+
for (use_tree, _) in items {
23372337
self.future_proof_import(use_tree);
23382338
}
23392339
}
@@ -2510,7 +2510,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
25102510
ItemKind::Use(ref use_tree) => {
25112511
let maybe_exported = match use_tree.kind {
25122512
UseTreeKind::Simple(_) | UseTreeKind::Glob => MaybeExported::Ok(item.id),
2513-
UseTreeKind::Nested(_) => MaybeExported::NestedUse(&item.vis),
2513+
UseTreeKind::Nested { .. } => MaybeExported::NestedUse(&item.vis),
25142514
};
25152515
self.resolve_doc_links(&item.attrs, maybe_exported);
25162516

src/tools/clippy/clippy_lints/src/single_component_path_imports.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ impl SingleComponentPathImports {
201201

202202
if segments.is_empty() {
203203
// keep track of `use {some_module, some_other_module};` usages
204-
if let UseTreeKind::Nested(trees) = &use_tree.kind {
205-
for tree in trees {
204+
if let UseTreeKind::Nested { items, .. } = &use_tree.kind {
205+
for tree in items {
206206
let segments = &tree.0.prefix.segments;
207207
if segments.len() == 1 {
208208
if let UseTreeKind::Simple(None) = tree.0.kind {
@@ -229,8 +229,8 @@ impl SingleComponentPathImports {
229229
}
230230

231231
// nested case such as `use self::{module1::Struct1, module2::Struct2}`
232-
if let UseTreeKind::Nested(trees) = &use_tree.kind {
233-
for tree in trees {
232+
if let UseTreeKind::Nested { items, .. } = &use_tree.kind {
233+
for tree in items {
234234
let segments = &tree.0.prefix.segments;
235235
if !segments.is_empty() {
236236
imports_reused_with_self.push(segments[0].ident.name);

src/tools/clippy/clippy_lints/src/unnecessary_self_imports.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ declare_lint_pass!(UnnecessarySelfImports => [UNNECESSARY_SELF_IMPORTS]);
3636
impl EarlyLintPass for UnnecessarySelfImports {
3737
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
3838
if let ItemKind::Use(use_tree) = &item.kind
39-
&& let UseTreeKind::Nested(nodes) = &use_tree.kind
40-
&& let [(self_tree, _)] = &**nodes
39+
&& let UseTreeKind::Nested { items, .. } = &use_tree.kind
40+
&& let [(self_tree, _)] = &**items
4141
&& let [self_seg] = &*self_tree.prefix.segments
4242
&& self_seg.ident.name == kw::SelfLower
4343
&& let Some(last_segment) = use_tree.prefix.segments.last()

src/tools/clippy/clippy_lints/src/unsafe_removed_from_name.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ fn check_use_tree(use_tree: &UseTree, cx: &EarlyContext<'_>, span: Span) {
4949
unsafe_to_safe_check(old_name, new_name, cx, span);
5050
},
5151
UseTreeKind::Simple(None) | UseTreeKind::Glob => {},
52-
UseTreeKind::Nested(ref nested_use_tree) => {
53-
for (use_tree, _) in nested_use_tree {
52+
UseTreeKind::Nested { ref items, .. } => {
53+
for (use_tree, _) in items {
5454
check_use_tree(use_tree, cx, span);
5555
}
5656
},

src/tools/clippy/clippy_utils/src/ast_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
637637
match (l, r) {
638638
(Glob, Glob) => true,
639639
(Simple(l), Simple(r)) => both(l, r, |l, r| eq_id(*l, *r)),
640-
(Nested(l), Nested(r)) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
640+
(Nested { items: l, .. }, Nested { items: r, .. }) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
641641
_ => false,
642642
}
643643
}

src/tools/rustfmt/src/imports.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,9 @@ impl UseTree {
458458
version,
459459
});
460460
}
461-
UseTreeKind::Nested(ref list) => {
461+
UseTreeKind::Nested {
462+
items: ref list, ..
463+
} => {
462464
// Extract comments between nested use items.
463465
// This needs to be done before sorting use items.
464466
let items = itemize_list(

0 commit comments

Comments
 (0)