Skip to content

Commit cce9132

Browse files
committed
track the root UseTree in addition to the leaf
1 parent a722296 commit cce9132

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ impl<'a> Resolver<'a> {
100100
}
101101

102102
fn build_reduced_graph_for_use_tree(&mut self,
103+
root_use_tree: &ast::UseTree,
104+
root_id: NodeId,
103105
use_tree: &ast::UseTree,
104106
id: NodeId,
105107
vis: ty::Visibility,
@@ -182,7 +184,14 @@ impl<'a> Resolver<'a> {
182184
type_ns_only,
183185
};
184186
self.add_import_directive(
185-
module_path, subclass, use_tree.span, id, vis, expansion,
187+
module_path,
188+
subclass,
189+
use_tree.span,
190+
id,
191+
root_use_tree.span,
192+
root_id,
193+
vis,
194+
expansion,
186195
);
187196
}
188197
ast::UseTreeKind::Glob => {
@@ -191,7 +200,14 @@ impl<'a> Resolver<'a> {
191200
max_vis: Cell::new(ty::Visibility::Invisible),
192201
};
193202
self.add_import_directive(
194-
module_path, subclass, use_tree.span, id, vis, expansion,
203+
module_path,
204+
subclass,
205+
use_tree.span,
206+
id,
207+
root_use_tree.span,
208+
root_id,
209+
vis,
210+
expansion,
195211
);
196212
}
197213
ast::UseTreeKind::Nested(ref items) => {
@@ -226,7 +242,7 @@ impl<'a> Resolver<'a> {
226242

227243
for &(ref tree, id) in items {
228244
self.build_reduced_graph_for_use_tree(
229-
tree, id, vis, &prefix, true, item, expansion
245+
root_use_tree, root_id, tree, id, vis, &prefix, true, item, expansion
230246
);
231247
}
232248
}
@@ -249,7 +265,7 @@ impl<'a> Resolver<'a> {
249265
};
250266

251267
self.build_reduced_graph_for_use_tree(
252-
use_tree, item.id, vis, &prefix, false, item, expansion,
268+
use_tree, item.id, use_tree, item.id, vis, &prefix, false, item, expansion,
253269
);
254270
}
255271

@@ -266,10 +282,12 @@ impl<'a> Resolver<'a> {
266282
let binding =
267283
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.arenas);
268284
let directive = self.arenas.alloc_import_directive(ImportDirective {
285+
root_id: item.id,
269286
id: item.id,
270287
parent,
271288
imported_module: Cell::new(Some(module)),
272289
subclass: ImportDirectiveSubclass::ExternCrate(orig_name),
290+
root_span: item.span,
273291
span: item.span,
274292
module_path: Vec::new(),
275293
vis: Cell::new(vis),
@@ -640,10 +658,12 @@ impl<'a> Resolver<'a> {
640658

641659
let (graph_root, arenas) = (self.graph_root, self.arenas);
642660
let macro_use_directive = |span| arenas.alloc_import_directive(ImportDirective {
661+
root_id: item.id,
643662
id: item.id,
644663
parent: graph_root,
645664
imported_module: Cell::new(Some(module)),
646665
subclass: ImportDirectiveSubclass::MacroUse,
666+
root_span: span,
647667
span,
648668
module_path: Vec::new(),
649669
vis: Cell::new(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))),

src/librustc_resolve/resolve_imports.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,36 @@ pub enum ImportDirectiveSubclass<'a> {
5555
/// One import directive.
5656
#[derive(Debug,Clone)]
5757
pub struct ImportDirective<'a> {
58+
/// The id of the `extern crate`, `UseTree` etc that imported this `ImportDirective`.
59+
///
60+
/// In the case where the `ImportDirective` was expanded from a "nested" use tree,
61+
/// this id is the id of the leaf tree. For example:
62+
///
63+
/// ```rust,ignore
64+
/// use foo::bar::{a, b}
65+
/// ```
66+
///
67+
/// If this is the import directive for `foo::bar::a`, we would have the id of the `UseTree`
68+
/// for `a` in this field.
5869
pub id: NodeId,
70+
71+
/// The `id` of the "root" use-kind -- this is always the same as
72+
/// `id` except in the case of "nested" use trees, in which case
73+
/// it will be the `id` of the root use tree. e.g., in the example
74+
/// from `id`, this would be the id of the `use foo::bar`
75+
/// `UseTree` node.
76+
pub root_id: NodeId,
77+
78+
/// Span of this use tree.
79+
pub span: Span,
80+
81+
/// Span of the *root* use tree (see `root_id`).
82+
pub root_span: Span,
83+
5984
pub parent: Module<'a>,
6085
pub module_path: Vec<Ident>,
6186
pub imported_module: Cell<Option<Module<'a>>>, // the resolution of `module_path`
6287
pub subclass: ImportDirectiveSubclass<'a>,
63-
pub span: Span,
6488
pub vis: Cell<ty::Visibility>,
6589
pub expansion: Mark,
6690
pub used: Cell<bool>,
@@ -296,6 +320,8 @@ impl<'a> Resolver<'a> {
296320
subclass: ImportDirectiveSubclass<'a>,
297321
span: Span,
298322
id: NodeId,
323+
root_span: Span,
324+
root_id: NodeId,
299325
vis: ty::Visibility,
300326
expansion: Mark) {
301327
let current_module = self.current_module;
@@ -306,6 +332,8 @@ impl<'a> Resolver<'a> {
306332
subclass,
307333
span,
308334
id,
335+
root_span,
336+
root_id,
309337
vis: Cell::new(vis),
310338
expansion,
311339
used: Cell::new(false),

0 commit comments

Comments
 (0)