Skip to content

Commit

Permalink
Refactor out BuildReducedGraphVisitor::visit_trait_item.
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Sep 22, 2016
1 parent 9a0e88a commit a344f14
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -33,7 +33,7 @@ use syntax::parse::token;

use syntax::ast::{Block, Crate};
use syntax::ast::{ForeignItem, ForeignItemKind, Item, ItemKind};
use syntax::ast::{Mutability, StmtKind, TraitItemKind};
use syntax::ast::{Mutability, StmtKind, TraitItem, TraitItemKind};
use syntax::ast::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
use syntax::parse::token::keywords;
use syntax::visit::{self, Visitor};
Expand Down Expand Up @@ -289,32 +289,14 @@ impl<'b> Resolver<'b> {

ItemKind::DefaultImpl(..) | ItemKind::Impl(..) => {}

ItemKind::Trait(.., ref items) => {
ItemKind::Trait(..) => {
let def_id = self.definitions.local_def_id(item.id);

// Add all the items within to a new module.
let kind = ModuleKind::Def(Def::Trait(def_id), name);
let module_parent = self.new_module(parent, kind, parent.normal_ancestor_id);
self.define(parent, name, TypeNS, (module_parent, sp, vis));

// Add the names of all the items to the trait info.
for item in items {
let item_def_id = self.definitions.local_def_id(item.id);
let mut is_static_method = false;
let (def, ns) = match item.node {
TraitItemKind::Const(..) => (Def::AssociatedConst(item_def_id), ValueNS),
TraitItemKind::Method(ref sig, _) => {
is_static_method = !sig.decl.has_self();
(Def::Method(item_def_id), ValueNS)
}
TraitItemKind::Type(..) => (Def::AssociatedTy(item_def_id), TypeNS),
TraitItemKind::Macro(_) => panic!("unexpanded macro in resolve!"),
};

self.define(module_parent, item.ident.name, ns, (def, item.span, vis));

self.trait_item_map.insert((item.ident.name, def_id), is_static_method);
}
let module = self.new_module(parent, kind, parent.normal_ancestor_id);
self.define(parent, name, TypeNS, (module, sp, vis));
self.current_module = module;
}
ItemKind::Mac(_) => panic!("unexpanded macro in resolve!"),
}
Expand Down Expand Up @@ -514,4 +496,31 @@ impl<'a, 'b> Visitor for BuildReducedGraphVisitor<'a, 'b> {
fn visit_block(&mut self, block: &Block) {
self.resolver.build_reduced_graph_for_block(block);
}

fn visit_trait_item(&mut self, item: &TraitItem) {
let parent = self.resolver.current_module;
let def_id = parent.def_id().unwrap();

// Add the item to the trait info.
let item_def_id = self.resolver.definitions.local_def_id(item.id);
let mut is_static_method = false;
let (def, ns) = match item.node {
TraitItemKind::Const(..) => (Def::AssociatedConst(item_def_id), ValueNS),
TraitItemKind::Method(ref sig, _) => {
is_static_method = !sig.decl.has_self();
(Def::Method(item_def_id), ValueNS)
}
TraitItemKind::Type(..) => (Def::AssociatedTy(item_def_id), TypeNS),
TraitItemKind::Macro(_) => panic!("unexpanded macro in resolve!"),
};

self.resolver.trait_item_map.insert((item.ident.name, def_id), is_static_method);

let vis = ty::Visibility::Public;
self.resolver.define(parent, item.ident.name, ns, (def, item.span, vis));

self.resolver.current_module = parent.parent.unwrap(); // nearest normal ancestor
visit::walk_trait_item(self, item);
self.resolver.current_module = parent;
}
}

0 comments on commit a344f14

Please sign in to comment.