]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc/middle/dead.rs
Rollup merge of #28797 - steveklabnik:rebase_for_gankro, r=alexcrichton
[rust.git] / src / librustc / middle / dead.rs
index 5b7a698eec0add4777486f0665171f04b1812795..6e31b733254bd73f90d370117d01fc62181dc19d 100644 (file)
 // closely. The idea is that all reachable symbols are live, codes called
 // from live codes are live, and everything else is dead.
 
-use ast_map;
+use front::map as ast_map;
+use rustc_front::hir;
+use rustc_front::visit::{self, Visitor};
+
 use middle::{def, pat_util, privacy, ty};
+use middle::def_id::{DefId};
 use lint;
 use util::nodemap::NodeSet;
 
 use std::collections::HashSet;
 use syntax::{ast, codemap};
-use syntax::ast_util::{local_def, is_local};
 use syntax::attr::{self, AttrMetaMethods};
-use syntax::visit::{self, Visitor};
 
 // Any local node that may call something in its body block should be
 // 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: ast::DefId) -> bool {
-    if !is_local(def_id) {
-        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
     }
 }
 
@@ -48,7 +48,7 @@ struct MarkSymbolVisitor<'a, 'tcx: 'a> {
     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> {
@@ -65,11 +65,20 @@ fn new(tcx: &'a ty::ctxt<'tcx>,
         }
     }
 
-    fn check_def_id(&mut self, def_id: ast::DefId) {
-        if should_explore(self.tcx, def_id) {
-            self.worklist.push(def_id.node);
+    fn check_def_id(&mut self, def_id: DefId) {
+        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) {
@@ -86,13 +95,14 @@ 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);
                     }
                 }
@@ -109,22 +119,22 @@ fn lookup_and_handle_method(&mut self, id: ast::NodeId) {
         self.check_def_id(method.def_id);
     }
 
-    fn handle_field_access(&mut self, lhs: &ast::Expr, name: ast::Name) {
+    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: &ast::Expr, idx: usize) {
+    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);
         }
     }
 
-    fn handle_field_pattern_match(&mut self, lhs: &ast::Pat,
-                                  pats: &[codemap::Spanned<ast::FieldPat>]) {
+    fn handle_field_pattern_match(&mut self, lhs: &hir::Pat,
+                                  pats: &[codemap::Spanned<hir::FieldPat>]) {
         let def = self.tcx.def_map.borrow().get(&lhs.id).unwrap().full_def();
         let pat_ty = self.tcx.node_id_to_type(lhs.id);
         let variant = match pat_ty.sty {
@@ -132,10 +142,10 @@ fn handle_field_pattern_match(&mut self, lhs: &ast::Pat,
             _ => self.tcx.sess.span_bug(lhs.span, "non-ADT in struct pattern")
         };
         for pat in pats {
-            if let ast::PatWild(ast::PatWildSingle) = pat.node.pat.node {
+            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);
         }
     }
 
@@ -166,7 +176,7 @@ fn visit_node(&mut self, node: &ast_map::Node) {
         match *node {
             ast_map::NodeItem(item) => {
                 match item.node {
-                    ast::ItemStruct(..) => {
+                    hir::ItemStruct(..) => {
                         self.struct_has_extern_repr = item.attrs.iter().any(|attr| {
                             attr::find_repr_attrs(self.tcx.sess.diagnostic(), attr)
                                 .contains(&attr::ReprExtern)
@@ -174,14 +184,14 @@ fn visit_node(&mut self, node: &ast_map::Node) {
 
                         visit::walk_item(self, &*item);
                     }
-                    ast::ItemEnum(..) => {
-                        self.inherited_pub_visibility = item.vis == ast::Public;
+                    hir::ItemEnum(..) => {
+                        self.inherited_pub_visibility = item.vis == hir::Public;
                         visit::walk_item(self, &*item);
                     }
-                    ast::ItemFn(..)
-                    | ast::ItemTy(..)
-                    | ast::ItemStatic(..)
-                    | ast::ItemConst(..) => {
+                    hir::ItemFn(..)
+                    | hir::ItemTy(..)
+                    | hir::ItemStatic(..)
+                    | hir::ItemConst(..) => {
                         visit::walk_item(self, &*item);
                     }
                     _ => ()
@@ -205,13 +215,13 @@ fn visit_node(&mut self, node: &ast_map::Node) {
 
 impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
 
-    fn visit_struct_def(&mut self, def: &ast::StructDef, _: ast::Ident,
-                        _: &ast::Generics, _: ast::NodeId) {
+    fn visit_struct_def(&mut self, def: &hir::StructDef, _: ast::Name,
+                        _: &hir::Generics, _: ast::NodeId) {
         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| {
             has_extern_repr || inherited_pub_visibility || match f.node.kind {
-                ast::NamedField(_, ast::Public) => true,
+                hir::NamedField(_, hir::Public) => true,
                 _ => false
             }
         });
@@ -220,15 +230,15 @@ fn visit_struct_def(&mut self, def: &ast::StructDef, _: ast::Ident,
         visit::walk_struct_def(self, def);
     }
 
-    fn visit_expr(&mut self, expr: &ast::Expr) {
+    fn visit_expr(&mut self, expr: &hir::Expr) {
         match expr.node {
-            ast::ExprMethodCall(..) => {
+            hir::ExprMethodCall(..) => {
                 self.lookup_and_handle_method(expr.id);
             }
-            ast::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);
             }
-            ast::ExprTupField(ref lhs, idx) => {
+            hir::ExprTupField(ref lhs, idx) => {
                 self.handle_tup_field_access(&**lhs, idx.node);
             }
             _ => ()
@@ -237,7 +247,7 @@ fn visit_expr(&mut self, expr: &ast::Expr) {
         visit::walk_expr(self, expr);
     }
 
-    fn visit_arm(&mut self, arm: &ast::Arm) {
+    fn visit_arm(&mut self, arm: &hir::Arm) {
         if arm.pats.len() == 1 {
             let pat = &*arm.pats[0];
             let variants = pat_util::necessary_variants(&self.tcx.def_map, pat);
@@ -254,10 +264,10 @@ fn visit_arm(&mut self, arm: &ast::Arm) {
         }
     }
 
-    fn visit_pat(&mut self, pat: &ast::Pat) {
+    fn visit_pat(&mut self, pat: &hir::Pat) {
         let def_map = &self.tcx.def_map;
         match pat.node {
-            ast::PatStruct(_, ref fields, _) => {
+            hir::PatStruct(_, ref fields, _) => {
                 self.handle_field_pattern_match(pat, fields);
             }
             _ if pat_util::pat_is_const(def_map, pat) => {
@@ -272,12 +282,17 @@ fn visit_pat(&mut self, pat: &ast::Pat) {
         self.ignore_non_const_paths = false;
     }
 
-    fn visit_path(&mut self, path: &ast::Path, id: ast::NodeId) {
+    fn visit_path(&mut self, path: &hir::Path, id: ast::NodeId) {
         self.lookup_and_handle_definition(&id);
         visit::walk_path(self, path);
     }
 
-    fn visit_item(&mut self, _: &ast::Item) {
+    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.
     }
@@ -317,20 +332,20 @@ struct LifeSeeder {
 }
 
 impl<'v> Visitor<'v> for LifeSeeder {
-    fn visit_item(&mut self, item: &ast::Item) {
+    fn visit_item(&mut self, item: &hir::Item) {
         let allow_dead_code = has_allow_dead_code_or_lang_attr(&item.attrs);
         if allow_dead_code {
             self.worklist.push(item.id);
         }
         match item.node {
-            ast::ItemEnum(ref enum_def, _) if allow_dead_code => {
+            hir::ItemEnum(ref enum_def, _) if allow_dead_code => {
                 self.worklist.extend(enum_def.variants.iter().map(|variant| variant.node.id));
             }
-            ast::ItemTrait(_, _, _, ref trait_items) => {
+            hir::ItemTrait(_, _, _, ref trait_items) => {
                 for trait_item in trait_items {
                     match trait_item.node {
-                        ast::ConstTraitItem(_, Some(_)) |
-                        ast::MethodTraitItem(_, Some(_)) => {
+                        hir::ConstTraitItem(_, Some(_)) |
+                        hir::MethodTraitItem(_, Some(_)) => {
                             if has_allow_dead_code_or_lang_attr(&trait_item.attrs) {
                                 self.worklist.push(trait_item.id);
                             }
@@ -339,18 +354,17 @@ fn visit_item(&mut self, item: &ast::Item) {
                     }
                 }
             }
-            ast::ItemImpl(_, _, _, ref opt_trait, _, ref impl_items) => {
+            hir::ItemImpl(_, _, _, ref opt_trait, _, ref impl_items) => {
                 for impl_item in impl_items {
                     match impl_item.node {
-                        ast::ConstImplItem(..) |
-                        ast::MethodImplItem(..) => {
+                        hir::ConstImplItem(..) |
+                        hir::MethodImplItem(..) => {
                             if opt_trait.is_some() ||
                                     has_allow_dead_code_or_lang_attr(&impl_item.attrs) {
                                 self.worklist.push(impl_item.id);
                             }
                         }
-                        ast::TypeImplItem(_) => {}
-                        ast::MacImplItem(_) => panic!("unexpanded macro")
+                        hir::TypeImplItem(_) => {}
                     }
                 }
             }
@@ -363,7 +377,7 @@ fn visit_item(&mut self, item: &ast::Item) {
 fn create_and_seed_worklist(tcx: &ty::ctxt,
                             exported_items: &privacy::ExportedItems,
                             reachable_symbols: &NodeSet,
-                            krate: &ast::Crate) -> Vec<ast::NodeId> {
+                            krate: &hir::Crate) -> Vec<ast::NodeId> {
     let mut worklist = Vec::new();
 
     // Preferably, we would only need to seed the worklist with reachable
@@ -401,7 +415,7 @@ fn create_and_seed_worklist(tcx: &ty::ctxt,
 fn find_live(tcx: &ty::ctxt,
              exported_items: &privacy::ExportedItems,
              reachable_symbols: &NodeSet,
-             krate: &ast::Crate)
+             krate: &hir::Crate)
              -> Box<HashSet<ast::NodeId>> {
     let worklist = create_and_seed_worklist(tcx, exported_items,
                                             reachable_symbols, krate);
@@ -410,9 +424,9 @@ fn find_live(tcx: &ty::ctxt,
     symbol_visitor.live_symbols
 }
 
-fn get_struct_ctor_id(item: &ast::Item) -> Option<ast::NodeId> {
+fn get_struct_ctor_id(item: &hir::Item) -> Option<ast::NodeId> {
     match item.node {
-        ast::ItemStruct(ref struct_def, _) => struct_def.ctor_id,
+        hir::ItemStruct(ref struct_def, _) => struct_def.ctor_id,
         _ => None
     }
 }
@@ -423,21 +437,21 @@ struct DeadVisitor<'a, 'tcx: 'a> {
 }
 
 impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
-    fn should_warn_about_item(&mut self, item: &ast::Item) -> bool {
+    fn should_warn_about_item(&mut self, item: &hir::Item) -> bool {
         let should_warn = match item.node {
-            ast::ItemStatic(..)
-            | ast::ItemConst(..)
-            | ast::ItemFn(..)
-            | ast::ItemEnum(..)
-            | ast::ItemStruct(..) => true,
+            hir::ItemStatic(..)
+            | hir::ItemConst(..)
+            | hir::ItemFn(..)
+            | hir::ItemEnum(..)
+            | hir::ItemStruct(..) => true,
             _ => false
         };
         let ctor_id = get_struct_ctor_id(item);
         should_warn && !self.symbol_is_live(item.id, ctor_id)
     }
 
-    fn should_warn_about_field(&mut self, node: &ast::StructField_) -> bool {
-        let is_named = node.ident().is_some();
+    fn should_warn_about_field(&mut self, node: &hir::StructField_) -> bool {
+        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)),
@@ -449,7 +463,7 @@ fn should_warn_about_field(&mut self, node: &ast::StructField_) -> bool {
             && !has_allow_dead_code_or_lang_attr(&node.attrs)
     }
 
-    fn should_warn_about_variant(&mut self, variant: &ast::Variant_) -> bool {
+    fn should_warn_about_variant(&mut self, variant: &hir::Variant_) -> bool {
         !self.symbol_is_live(variant.id, None)
             && !has_allow_dead_code_or_lang_attr(&variant.attrs)
     }
@@ -463,8 +477,10 @@ fn should_warn_about_variant(&mut self, variant: &ast::Variant_) -> bool {
     // `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)) {
@@ -475,14 +491,16 @@ fn symbol_is_live(&mut self, id: ast::NodeId,
         // 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(&local_def(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;
+                            }
                         }
                     }
                 }
@@ -509,21 +527,21 @@ fn warn_dead_code(&mut self,
 }
 
 impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
-    fn visit_item(&mut self, item: &ast::Item) {
+    fn visit_item(&mut self, item: &hir::Item) {
         if self.should_warn_about_item(item) {
             self.warn_dead_code(
                 item.id,
                 item.span,
-                item.ident.name,
+                item.name,
                 item.node.descriptive_variant()
             );
         } else {
             match item.node {
-                ast::ItemEnum(ref enum_def, _) => {
+                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");
+                                                variant.node.name, "variant");
                         }
                     }
                 },
@@ -533,55 +551,54 @@ fn visit_item(&mut self, item: &ast::Item) {
         visit::walk_item(self, item);
     }
 
-    fn visit_foreign_item(&mut self, fi: &ast::ForeignItem) {
+    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: &ast::StructField) {
+    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);
     }
 
-    fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) {
+    fn visit_impl_item(&mut self, impl_item: &hir::ImplItem) {
         match impl_item.node {
-            ast::ConstImplItem(_, ref expr) => {
+            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)
             }
-            ast::MethodImplItem(_, ref body) => {
+            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)
             }
-            ast::TypeImplItem(..) |
-            ast::MacImplItem(..) => {}
+            hir::TypeImplItem(..) => {}
         }
     }
 
     // Overwrite so that we don't warn the trait item itself.
-    fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
+    fn visit_trait_item(&mut self, trait_item: &hir::TraitItem) {
         match trait_item.node {
-            ast::ConstTraitItem(_, Some(ref expr)) => {
+            hir::ConstTraitItem(_, Some(ref expr)) => {
                 visit::walk_expr(self, expr)
             }
-            ast::MethodTraitItem(_, Some(ref body)) => {
+            hir::MethodTraitItem(_, Some(ref body)) => {
                 visit::walk_block(self, body)
             }
-            ast::ConstTraitItem(_, None) |
-            ast::MethodTraitItem(_, None) |
-            ast::TypeTraitItem(..) => {}
+            hir::ConstTraitItem(_, None) |
+            hir::MethodTraitItem(_, None) |
+            hir::TypeTraitItem(..) => {}
         }
     }
 }