Skip to content

Commit

Permalink
track the root UseTree in addition to the leaf
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed May 18, 2018
1 parent a722296 commit cce9132
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
28 changes: 24 additions & 4 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -100,6 +100,8 @@ impl<'a> Resolver<'a> {
}

fn build_reduced_graph_for_use_tree(&mut self,
root_use_tree: &ast::UseTree,
root_id: NodeId,
use_tree: &ast::UseTree,
id: NodeId,
vis: ty::Visibility,
Expand Down Expand Up @@ -182,7 +184,14 @@ impl<'a> Resolver<'a> {
type_ns_only,
};
self.add_import_directive(
module_path, subclass, use_tree.span, id, vis, expansion,
module_path,
subclass,
use_tree.span,
id,
root_use_tree.span,
root_id,
vis,
expansion,
);
}
ast::UseTreeKind::Glob => {
Expand All @@ -191,7 +200,14 @@ impl<'a> Resolver<'a> {
max_vis: Cell::new(ty::Visibility::Invisible),
};
self.add_import_directive(
module_path, subclass, use_tree.span, id, vis, expansion,
module_path,
subclass,
use_tree.span,
id,
root_use_tree.span,
root_id,
vis,
expansion,
);
}
ast::UseTreeKind::Nested(ref items) => {
Expand Down Expand Up @@ -226,7 +242,7 @@ impl<'a> Resolver<'a> {

for &(ref tree, id) in items {
self.build_reduced_graph_for_use_tree(
tree, id, vis, &prefix, true, item, expansion
root_use_tree, root_id, tree, id, vis, &prefix, true, item, expansion
);
}
}
Expand All @@ -249,7 +265,7 @@ impl<'a> Resolver<'a> {
};

self.build_reduced_graph_for_use_tree(
use_tree, item.id, vis, &prefix, false, item, expansion,
use_tree, item.id, use_tree, item.id, vis, &prefix, false, item, expansion,
);
}

Expand All @@ -266,10 +282,12 @@ impl<'a> Resolver<'a> {
let binding =
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.arenas);
let directive = self.arenas.alloc_import_directive(ImportDirective {
root_id: item.id,
id: item.id,
parent,
imported_module: Cell::new(Some(module)),
subclass: ImportDirectiveSubclass::ExternCrate(orig_name),
root_span: item.span,
span: item.span,
module_path: Vec::new(),
vis: Cell::new(vis),
Expand Down Expand Up @@ -640,10 +658,12 @@ impl<'a> Resolver<'a> {

let (graph_root, arenas) = (self.graph_root, self.arenas);
let macro_use_directive = |span| arenas.alloc_import_directive(ImportDirective {
root_id: item.id,
id: item.id,
parent: graph_root,
imported_module: Cell::new(Some(module)),
subclass: ImportDirectiveSubclass::MacroUse,
root_span: span,
span,
module_path: Vec::new(),
vis: Cell::new(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))),
Expand Down
30 changes: 29 additions & 1 deletion src/librustc_resolve/resolve_imports.rs
Expand Up @@ -55,12 +55,36 @@ pub enum ImportDirectiveSubclass<'a> {
/// One import directive.
#[derive(Debug,Clone)]
pub struct ImportDirective<'a> {
/// The id of the `extern crate`, `UseTree` etc that imported this `ImportDirective`.
///
/// In the case where the `ImportDirective` was expanded from a "nested" use tree,
/// this id is the id of the leaf tree. For example:
///
/// ```rust,ignore
/// use foo::bar::{a, b}
/// ```
///
/// If this is the import directive for `foo::bar::a`, we would have the id of the `UseTree`
/// for `a` in this field.
pub id: NodeId,

/// The `id` of the "root" use-kind -- this is always the same as
/// `id` except in the case of "nested" use trees, in which case
/// it will be the `id` of the root use tree. e.g., in the example
/// from `id`, this would be the id of the `use foo::bar`
/// `UseTree` node.
pub root_id: NodeId,

/// Span of this use tree.
pub span: Span,

/// Span of the *root* use tree (see `root_id`).
pub root_span: Span,

pub parent: Module<'a>,
pub module_path: Vec<Ident>,
pub imported_module: Cell<Option<Module<'a>>>, // the resolution of `module_path`
pub subclass: ImportDirectiveSubclass<'a>,
pub span: Span,
pub vis: Cell<ty::Visibility>,
pub expansion: Mark,
pub used: Cell<bool>,
Expand Down Expand Up @@ -296,6 +320,8 @@ impl<'a> Resolver<'a> {
subclass: ImportDirectiveSubclass<'a>,
span: Span,
id: NodeId,
root_span: Span,
root_id: NodeId,
vis: ty::Visibility,
expansion: Mark) {
let current_module = self.current_module;
Expand All @@ -306,6 +332,8 @@ impl<'a> Resolver<'a> {
subclass,
span,
id,
root_span,
root_id,
vis: Cell::new(vis),
expansion,
used: Cell::new(false),
Expand Down

0 comments on commit cce9132

Please sign in to comment.