// explored. For example, if it's a live NodeItem that is a
// function, then we should explore its block to check for codes that
// may need to be marked as live.
-fn should_explore(tcx: &ty::ctxt, def_id: DefId) -> bool {
- if !def_id.is_local() {
- return false;
- }
-
- match tcx.map.find(def_id.node) {
- Some(ast_map::NodeItem(..))
- | Some(ast_map::NodeImplItem(..))
- | Some(ast_map::NodeForeignItem(..))
- | Some(ast_map::NodeTraitItem(..)) => true,
- _ => false
+fn should_explore(tcx: &ty::ctxt, node_id: ast::NodeId) -> bool {
+ match tcx.map.find(node_id) {
+ Some(ast_map::NodeItem(..)) |
+ Some(ast_map::NodeImplItem(..)) |
+ Some(ast_map::NodeForeignItem(..)) |
+ Some(ast_map::NodeTraitItem(..)) =>
+ true,
+ _ =>
+ false
}
}
struct_has_extern_repr: bool,
ignore_non_const_paths: bool,
inherited_pub_visibility: bool,
- ignore_variant_stack: Vec<ast::NodeId>,
+ ignore_variant_stack: Vec<DefId>,
}
impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
}
fn check_def_id(&mut self, def_id: DefId) {
- if should_explore(self.tcx, def_id) {
- self.worklist.push(def_id.node);
+ if let Some(node_id) = self.tcx.map.as_local_node_id(def_id) {
+ if should_explore(self.tcx, node_id) {
+ self.worklist.push(node_id);
+ }
+ self.live_symbols.insert(node_id);
+ }
+ }
+
+ fn insert_def_id(&mut self, def_id: DefId) {
+ if let Some(node_id) = self.tcx.map.as_local_node_id(def_id) {
+ debug_assert!(!should_explore(self.tcx, node_id));
+ self.live_symbols.insert(node_id);
}
- self.live_symbols.insert(def_id.node);
}
fn lookup_and_handle_definition(&mut self, id: &ast::NodeId) {
self.tcx.def_map.borrow().get(id).map(|def| {
match def.full_def() {
def::DefConst(_) | def::DefAssociatedConst(..) => {
- self.check_def_id(def.def_id())
+ self.check_def_id(def.def_id());
}
_ if self.ignore_non_const_paths => (),
def::DefPrimTy(_) => (),
+ def::DefSelfTy(..) => (),
def::DefVariant(enum_id, variant_id, _) => {
self.check_def_id(enum_id);
- if !self.ignore_variant_stack.contains(&variant_id.node) {
+ if !self.ignore_variant_stack.contains(&variant_id) {
self.check_def_id(variant_id);
}
}
fn handle_field_access(&mut self, lhs: &hir::Expr, name: ast::Name) {
if let ty::TyStruct(def, _) = self.tcx.expr_ty_adjusted(lhs).sty {
- self.live_symbols.insert(def.struct_variant().field_named(name).did.node);
+ self.insert_def_id(def.struct_variant().field_named(name).did);
} else {
self.tcx.sess.span_bug(lhs.span, "named field access on non-struct")
}
fn handle_tup_field_access(&mut self, lhs: &hir::Expr, idx: usize) {
if let ty::TyStruct(def, _) = self.tcx.expr_ty_adjusted(lhs).sty {
- self.live_symbols.insert(def.struct_variant().fields[idx].did.node);
+ self.insert_def_id(def.struct_variant().fields[idx].did);
}
}
if let hir::PatWild(hir::PatWildSingle) = pat.node.pat.node {
continue;
}
- self.live_symbols.insert(variant.field_named(pat.node.ident.name).did.node);
+ self.insert_def_id(variant.field_named(pat.node.name).did);
}
}
impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
- fn visit_struct_def(&mut self, def: &hir::StructDef, _: ast::Ident,
- _: &hir::Generics, _: ast::NodeId) {
+ fn visit_variant_data(&mut self, def: &hir::VariantData, _: ast::Name,
+ _: &hir::Generics, _: ast::NodeId, _: codemap::Span) {
let has_extern_repr = self.struct_has_extern_repr;
let inherited_pub_visibility = self.inherited_pub_visibility;
- let live_fields = def.fields.iter().filter(|f| {
+ let live_fields = def.fields().filter(|f| {
has_extern_repr || inherited_pub_visibility || match f.node.kind {
hir::NamedField(_, hir::Public) => true,
_ => false
hir::ExprMethodCall(..) => {
self.lookup_and_handle_method(expr.id);
}
- hir::ExprField(ref lhs, ref ident) => {
- self.handle_field_access(&**lhs, ident.node.name);
+ hir::ExprField(ref lhs, ref name) => {
+ self.handle_field_access(&**lhs, name.node);
}
hir::ExprTupField(ref lhs, idx) => {
self.handle_tup_field_access(&**lhs, idx.node);
visit::walk_path(self, path);
}
+ fn visit_path_list_item(&mut self, path: &hir::Path, item: &hir::PathListItem) {
+ self.lookup_and_handle_definition(&item.node.id());
+ visit::walk_path_list_item(self, path, item);
+ }
+
fn visit_item(&mut self, _: &hir::Item) {
// Do not recurse into items. These items will be added to the
// worklist and recursed into manually if necessary.
}
match item.node {
hir::ItemEnum(ref enum_def, _) if allow_dead_code => {
- self.worklist.extend(enum_def.variants.iter().map(|variant| variant.node.id));
+ self.worklist.extend(enum_def.variants.iter()
+ .map(|variant| variant.node.data.id()));
}
hir::ItemTrait(_, _, _, ref trait_items) => {
for trait_item in trait_items {
fn get_struct_ctor_id(item: &hir::Item) -> Option<ast::NodeId> {
match item.node {
- hir::ItemStruct(ref struct_def, _) => struct_def.ctor_id,
+ hir::ItemStruct(ref struct_def, _) if !struct_def.is_struct() => {
+ Some(struct_def.id())
+ }
_ => None
}
}
}
fn should_warn_about_field(&mut self, node: &hir::StructField_) -> bool {
- let is_named = node.ident().is_some();
+ let is_named = node.name().is_some();
let field_type = self.tcx.node_id_to_type(node.id);
let is_marker_field = match field_type.ty_to_def_id() {
Some(def_id) => self.tcx.lang_items.items().any(|(_, item)| *item == Some(def_id)),
}
fn should_warn_about_variant(&mut self, variant: &hir::Variant_) -> bool {
- !self.symbol_is_live(variant.id, None)
+ !self.symbol_is_live(variant.data.id(), None)
&& !has_allow_dead_code_or_lang_attr(&variant.attrs)
}
// `ctor_id`. On the other hand, in a statement like
// `type <ident> <generics> = <ty>;` where <ty> refers to a struct_ctor,
// DefMap maps <ty> to `id` instead.
- fn symbol_is_live(&mut self, id: ast::NodeId,
- ctor_id: Option<ast::NodeId>) -> bool {
+ fn symbol_is_live(&mut self,
+ id: ast::NodeId,
+ ctor_id: Option<ast::NodeId>)
+ -> bool {
if self.live_symbols.contains(&id)
|| ctor_id.map_or(false,
|ctor| self.live_symbols.contains(&ctor)) {
// method of a private type is used, but the type itself is never
// called directly.
let impl_items = self.tcx.impl_items.borrow();
- match self.tcx.inherent_impls.borrow().get(&DefId::local(id)) {
+ match self.tcx.inherent_impls.borrow().get(&self.tcx.map.local_def_id(id)) {
None => (),
Some(impl_list) => {
for impl_did in impl_list.iter() {
for item_did in impl_items.get(impl_did).unwrap().iter() {
- if self.live_symbols.contains(&item_did.def_id()
- .node) {
- return true;
+ if let Some(item_node_id) =
+ self.tcx.map.as_local_node_id(item_did.def_id()) {
+ if self.live_symbols.contains(&item_node_id) {
+ return true;
+ }
}
}
}
self.warn_dead_code(
item.id,
item.span,
- item.ident.name,
+ item.name,
item.node.descriptive_variant()
);
} else {
hir::ItemEnum(ref enum_def, _) => {
for variant in &enum_def.variants {
if self.should_warn_about_variant(&variant.node) {
- self.warn_dead_code(variant.node.id, variant.span,
- variant.node.name.name, "variant");
+ self.warn_dead_code(variant.node.data.id(), variant.span,
+ variant.node.name, "variant");
}
}
},
fn visit_foreign_item(&mut self, fi: &hir::ForeignItem) {
if !self.symbol_is_live(fi.id, None) {
- self.warn_dead_code(fi.id, fi.span, fi.ident.name, fi.node.descriptive_variant());
+ self.warn_dead_code(fi.id, fi.span, fi.name, fi.node.descriptive_variant());
}
visit::walk_foreign_item(self, fi);
}
fn visit_struct_field(&mut self, field: &hir::StructField) {
if self.should_warn_about_field(&field.node) {
self.warn_dead_code(field.node.id, field.span,
- field.node.ident().unwrap().name, "struct field");
+ field.node.name().unwrap(), "struct field");
}
visit::walk_struct_field(self, field);
hir::ConstImplItem(_, ref expr) => {
if !self.symbol_is_live(impl_item.id, None) {
self.warn_dead_code(impl_item.id, impl_item.span,
- impl_item.ident.name, "associated const");
+ impl_item.name, "associated const");
}
visit::walk_expr(self, expr)
}
hir::MethodImplItem(_, ref body) => {
if !self.symbol_is_live(impl_item.id, None) {
self.warn_dead_code(impl_item.id, impl_item.span,
- impl_item.ident.name, "method");
+ impl_item.name, "method");
}
visit::walk_block(self, body)
}